Files

62 lines
2.0 KiB
Rust

//! 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 leleka_control_plane::tokenizer::Tokenizer;
use serde_json::{Map, json};
pub const SPECIALS: [&str; 7] =
["<|im_start|>", "<|im_end|>", "<think>", "</think>", "<|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
}
fn write_vocabulary(root: &Path) {
let mut vocabulary = Map::new();
for (id, ch) in leleka_control_plane::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));
}
fs::write(root.join("vocab.json"), serde_json::to_vec(&vocabulary).unwrap()).unwrap();
fs::write(root.join("merges.txt"), "").unwrap();
}
fn write_token_metadata(root: &Path) {
let total = 256 + SPECIALS.len();
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();
}
pub fn tokenizer(dir: &Path) -> Arc<Tokenizer> {
let root = dir.join("tokenizer");
fs::create_dir(&root).unwrap();
write_vocabulary(&root);
write_token_metadata(&root);
Arc::new(Tokenizer::from_assets(&root).unwrap())
}