1use crate::{KuteError, PayloadKind, Result, Version, VocabularyId};
2use serde::{Deserialize, Serialize};
3use std::collections::BTreeMap;
4
5pub const GPT4_PATTERN: &str = r"'(?i:[sdmt]|ll|ve|re)|[^\r\n\p{L}\p{N}]?+\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]++[\r\n]*|\s*[\r\n]|\s+(?!\S)|\s+";
7
8#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
10#[serde(rename_all = "snake_case")]
11pub enum Pretokenizer {
12 #[default]
14 None,
15 Gpt4,
17 Regex(String),
19}
20
21impl Pretokenizer {
22 pub fn pattern(&self) -> Option<&str> {
23 match self {
24 Pretokenizer::None => None,
25 Pretokenizer::Gpt4 => Some(GPT4_PATTERN),
26 Pretokenizer::Regex(p) => Some(p.as_str()),
27 }
28 }
29}
30
31#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
35pub struct TrainingConfig {
36 pub vocabulary_id: VocabularyId,
37 pub vocabulary_version: Version,
38 pub vocab_size: u32,
40 #[serde(default = "one")]
42 pub min_frequency: u64,
43 #[serde(default)]
44 pub pretokenizer: Pretokenizer,
45 #[serde(default)]
47 pub reserved_tokens: Vec<String>,
48 #[serde(default)]
50 pub max_token_length: Option<usize>,
51 #[serde(default = "default_window")]
53 pub train_window: usize,
54 #[serde(default)]
56 pub dictionary: Vec<String>,
57 #[serde(default)]
59 pub max_corpus_units: Option<u64>,
60 #[serde(default)]
62 pub seed: u64,
63 #[serde(default)]
64 pub description: Option<String>,
65 #[serde(default)]
66 pub license: Option<String>,
67 #[serde(default = "yes", skip_serializing_if = "is_true")]
71 pub embed_test_vectors: bool,
72 #[serde(default, skip_serializing_if = "Option::is_none")]
77 pub stream_bytes: Option<usize>,
78}
79
80fn yes() -> bool {
81 true
82}
83
84fn is_true(b: &bool) -> bool {
85 *b
86}
87
88fn one() -> u64 {
89 1
90}
91
92fn default_window() -> usize {
93 256
94}
95
96impl TrainingConfig {
97 pub fn new(vocabulary_id: VocabularyId, vocab_size: u32) -> Self {
98 Self {
99 vocabulary_id,
100 vocabulary_version: Version::new(0, 1, 0),
101 vocab_size,
102 min_frequency: 1,
103 pretokenizer: Pretokenizer::None,
104 reserved_tokens: Vec::new(),
105 max_token_length: None,
106 train_window: default_window(),
107 dictionary: Vec::new(),
108 max_corpus_units: None,
109 seed: 0,
110 description: None,
111 license: None,
112 embed_test_vectors: true,
113 stream_bytes: None,
114 }
115 }
116
117 pub fn validate(&self) -> Result<()> {
118 const MAX_VOCAB: u32 = 4_000_000;
119 if self.vocab_size == 0 || self.vocab_size > MAX_VOCAB {
120 return Err(KuteError::InvalidConfig(format!("vocab_size must be between 1 and {MAX_VOCAB}")));
121 }
122 if self.min_frequency == 0 {
123 return Err(KuteError::InvalidConfig("min_frequency must be at least 1".into()));
124 }
125 if self.train_window < 2 || self.train_window > 1 << 20 {
126 return Err(KuteError::InvalidConfig("train_window must be between 2 and 1048576".into()));
127 }
128 if self.stream_bytes.is_some_and(|n| !(4096..=1 << 30).contains(&n)) {
129 return Err(KuteError::InvalidConfig("stream_bytes must be between 4096 and 1073741824".into()));
130 }
131 if self.max_token_length == Some(0) {
132 return Err(KuteError::InvalidConfig("max_token_length must be at least 1".into()));
133 }
134 let mut seen = std::collections::BTreeSet::new();
135 for t in &self.reserved_tokens {
136 if t.is_empty() || t.len() > 256 {
137 return Err(KuteError::InvalidConfig("reserved tokens must be 1 to 256 bytes".into()));
138 }
139 if !seen.insert(t) {
140 return Err(KuteError::InvalidConfig(format!("reserved token {t:?} is listed twice")));
141 }
142 }
143 if let Pretokenizer::Regex(p) = &self.pretokenizer {
144 if p.is_empty() || p.len() > 4096 {
145 return Err(KuteError::InvalidConfig("pretokenizer regex must be 1 to 4096 bytes".into()));
146 }
147 }
148 Ok(())
149 }
150}
151
152#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
154pub struct AdapterConfig {
155 #[serde(flatten)]
156 pub options: BTreeMap<String, String>,
157}
158
159impl AdapterConfig {
160 pub fn with(mut self, key: &str, value: &str) -> Self {
161 self.options.insert(key.to_string(), value.to_string());
162 self
163 }
164
165 pub fn get(&self, key: &str) -> Option<&str> {
166 self.options.get(key).map(String::as_str)
167 }
168}
169
170#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
172pub struct TokenizerCapabilities {
173 pub algorithm: String,
174 pub lossless: bool,
176 pub trainable: bool,
177 pub deterministic: bool,
178 pub accepts: Vec<PayloadKind>,
179 pub rejects_unknown: bool,
181 pub notes: String,
182}