//! Shared by the HTTP integration tests: a byte-level tokenizer with the template's special tokens. #![allow(dead_code)] use std::{ fs, path::{Path, PathBuf}, sync::Arc, }; use serde_json::{Map, json}; use strata_control::tokenizer::Tokenizer; pub const SPECIALS: [&str; 7] = [ "<|im_start|>", "<|im_end|>", "", "", "<|vision_start|>", "<|image_pad|>", "<|vision_end|>", ]; pub const PAD: u32 = 256 + 5; pub const START: u32 = 256 + 4; pub const END: u32 = 256 + 6; pub fn scratch(name: &str) -> PathBuf { let dir = std::env::temp_dir().join(format!("strata-vision-http-{}-{name}", std::process::id())); let _ = fs::remove_dir_all(&dir); fs::create_dir_all(&dir).unwrap(); dir } pub fn tokenizer(dir: &Path) -> Arc { let root = dir.join("tokenizer"); fs::create_dir(&root).unwrap(); let mut vocabulary = Map::new(); for (id, ch) in strata_control::tokenizer::byte_alphabet() .0 .iter() .enumerate() { vocabulary.insert(ch.to_string(), json!(id)); } for (offset, token) in SPECIALS.iter().enumerate() { vocabulary.insert((*token).into(), json!(256 + offset)); } let total = 256 + SPECIALS.len(); fs::write( root.join("vocab.json"), serde_json::to_vec(&vocabulary).unwrap(), ) .unwrap(); fs::write(root.join("merges.txt"), "").unwrap(); let mut types = vec![1u8; total]; types[256..].fill(3); fs::write( root.join("token_type.json"), serde_json::to_vec(&types).unwrap(), ) .unwrap(); fs::write( root.join("tokenizer.json"), serde_json::to_vec(&json!({ "model": "gpt2", "pre": "qwen35", "vocab_size": total, "n_merges": 0, "pre_pattern": leleka_tokenizer::QWEN35_PATTERN, "special_ids": {"tokenizer.ggml.eos_token_id": 257} })) .unwrap(), ) .unwrap(); Arc::new(Tokenizer::from_assets(&root).unwrap()) }