aprender-core 0.50.0

Next-generation machine learning library in pure Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

impl Default for ChatMLTemplate {
    fn default() -> Self {
        Self::new()
    }
}

impl ChatTemplateEngine for ChatMLTemplate {
    fn format_message(&self, role: &str, content: &str) -> Result<String, AprenderError> {
        // Sanitize user content to prevent prompt injection (GH-204)
        let safe_content = if role == "user" {
            sanitize_user_content(content)
        } else {
            content.to_string()
        };
        Ok(format!("<|im_start|>{role}\n{safe_content}<|im_end|>\n"))
    }

    fn format_conversation(&self, messages: &[ChatMessage]) -> Result<String, AprenderError> {
        use std::fmt::Write;
        let mut result = String::new();

        for msg in messages {
            // Sanitize user content to prevent prompt injection (GH-204)
            // System messages are trusted; user messages are untrusted
            let safe_content = if msg.role == "user" {
                sanitize_user_content(&msg.content)
            } else {
                msg.content.clone()
            };
            let _ = write!(
                result,
                "<|im_start|>{}\n{}<|im_end|>\n",
                msg.role, safe_content
            );
        }

        // Add generation prompt
        result.push_str("<|im_start|>assistant\n");

        Ok(result)
    }

    fn special_tokens(&self) -> &SpecialTokens {
        &self.special_tokens
    }

    fn format(&self) -> TemplateFormat {
        TemplateFormat::ChatML
    }

    fn supports_system_prompt(&self) -> bool {
        true
    }
}

/// LLaMA 2 Template (TinyLlama, Vicuna, LLaMA 2)
///
/// Format: `<s>[INST] <<SYS>>\n{system}\n<</SYS>>\n\n{user} [/INST]`
///
/// # Example
///
/// ```
/// use aprender::text::chat_template::{ChatMessage, Llama2Template, ChatTemplateEngine};
///
/// let template = Llama2Template::new();
/// let messages = vec![ChatMessage::user("Hello!")];
/// let output = template.format_conversation(&messages).expect("format conversation should succeed");
/// assert!(output.contains("[INST]"));
/// ```
#[derive(Debug, Clone)]
pub struct Llama2Template {
    special_tokens: SpecialTokens,
}

impl Llama2Template {
    /// Create a new LLaMA 2 template
    #[must_use]
    pub fn new() -> Self {
        Self {
            special_tokens: SpecialTokens {
                bos_token: Some("<s>".to_string()),
                eos_token: Some("</s>".to_string()),
                inst_start: Some("[INST]".to_string()),
                inst_end: Some("[/INST]".to_string()),
                sys_start: Some("<<SYS>>".to_string()),
                sys_end: Some("<</SYS>>".to_string()),
                ..Default::default()
            },
        }
    }
}

impl Default for Llama2Template {
    fn default() -> Self {
        Self::new()
    }
}

impl ChatTemplateEngine for Llama2Template {
    fn format_message(&self, role: &str, content: &str) -> Result<String, AprenderError> {
        // Sanitize user content to prevent prompt injection (GH-204)
        let safe_content = if role == "user" {
            sanitize_user_content(content)
        } else {
            content.to_string()
        };
        match role {
            "system" => Ok(format!("<<SYS>>\n{safe_content}\n<</SYS>>\n\n")),
            "user" => Ok(format!("[INST] {safe_content} [/INST]")),
            "assistant" => Ok(format!(" {safe_content}</s>")),
            _ => Ok(safe_content),
        }
    }

    fn format_conversation(&self, messages: &[ChatMessage]) -> Result<String, AprenderError> {
        let mut result = String::from("<s>");
        let mut system_prompt: Option<String> = None;
        // Canonical Llama-2 wraps each `[INST] ... [/INST] {answer} </s>` exchange in
        // its own `<s>...</s>`. A fresh `<s>` is therefore emitted before a user turn
        // ONLY when that user turn opens a new block immediately after a closed
        // assistant turn (which ended in `</s>`). The leading `<s>` (pushed above)
        // already covers the very first block. Tracking "after an assistant turn"
        // (not merely "index > 0 and not currently in a user turn") avoids emitting a
        // spurious second `<s>` when index 0 is a system message — that bug produced a
        // double-BOS (`<s><s>...`) at the start of every system-prompted conversation,
        // which the model never sees during training and which degrades generation.
        let mut after_assistant = false;

        for msg in messages {
            match msg.role.as_str() {
                "system" => {
                    system_prompt = Some(msg.content.clone());
                }
                "user" => {
                    // Sanitize user content to prevent prompt injection (GH-204)
                    let safe_content = sanitize_user_content(&msg.content);
                    if after_assistant {
                        result.push_str("<s>");
                    }
                    result.push_str("[INST] ");

                    // Include system prompt with first user message
                    if let Some(sys) = system_prompt.take() {
                        result.push_str("<<SYS>>\n");
                        result.push_str(&sys);
                        result.push_str("\n<</SYS>>\n\n");
                    }

                    result.push_str(&safe_content);
                    result.push_str(" [/INST]");
                    after_assistant = false;
                }
                "assistant" => {
                    result.push(' ');
                    result.push_str(&msg.content);
                    result.push_str("</s>");
                    after_assistant = true;
                }
                _ => {}
            }
        }

        Ok(result)
    }

    fn special_tokens(&self) -> &SpecialTokens {
        &self.special_tokens
    }

    fn format(&self) -> TemplateFormat {
        TemplateFormat::Llama2
    }

    fn supports_system_prompt(&self) -> bool {
        true
    }
}

/// Mistral Template (Mistral, Mixtral)
///
/// Format: `<s>[INST] {user} [/INST]` (no system prompt support)
///
/// # Example
///
/// ```
/// use aprender::text::chat_template::{ChatMessage, MistralTemplate, ChatTemplateEngine};
///
/// let template = MistralTemplate::new();
/// assert!(!template.supports_system_prompt());
/// ```
#[derive(Debug, Clone)]
pub struct MistralTemplate {
    special_tokens: SpecialTokens,
}

impl MistralTemplate {
    /// Create a new Mistral template
    #[must_use]
    pub fn new() -> Self {
        Self {
            special_tokens: SpecialTokens {
                bos_token: Some("<s>".to_string()),
                eos_token: Some("</s>".to_string()),
                inst_start: Some("[INST]".to_string()),
                inst_end: Some("[/INST]".to_string()),
                ..Default::default()
            },
        }
    }
}

impl Default for MistralTemplate {
    fn default() -> Self {
        Self::new()
    }
}

impl ChatTemplateEngine for MistralTemplate {
    fn format_message(&self, role: &str, content: &str) -> Result<String, AprenderError> {
        // Sanitize user content to prevent prompt injection (GH-204)
        let safe_content = if role == "user" {
            sanitize_user_content(content)
        } else {
            content.to_string()
        };
        match role {
            "user" => Ok(format!("[INST] {safe_content} [/INST]")),
            "assistant" => Ok(format!(" {safe_content}</s>")),
            "system" => {
                // Mistral doesn't support system prompts, prepend to first user message
                Ok(format!("{safe_content}\n\n"))
            }
            _ => Ok(safe_content),
        }
    }

    fn format_conversation(&self, messages: &[ChatMessage]) -> Result<String, AprenderError> {
        let mut result = String::from("<s>");

        for msg in messages {
            match msg.role.as_str() {
                "user" => {
                    // Sanitize user content to prevent prompt injection (GH-204)
                    let safe_content = sanitize_user_content(&msg.content);
                    result.push_str("[INST] ");
                    result.push_str(&safe_content);
                    result.push_str(" [/INST]");
                }
                "assistant" => {
                    result.push(' ');
                    result.push_str(&msg.content);
                    result.push_str("</s>");
                }
                // Mistral doesn't support system prompts - ignored
                _ => {}
            }
        }

        Ok(result)
    }

    fn special_tokens(&self) -> &SpecialTokens {
        &self.special_tokens
    }

    fn format(&self) -> TemplateFormat {
        TemplateFormat::Mistral
    }

    fn supports_system_prompt(&self) -> bool {
        false // Mistral doesn't support system prompts
    }
}

/// Phi Template (Phi-2, Phi-3)
///
/// Format: `Instruct: {content}\nOutput:`
///
/// # Example
///
/// ```
/// use aprender::text::chat_template::{ChatMessage, PhiTemplate, ChatTemplateEngine};
///
/// let template = PhiTemplate::new();
/// let messages = vec![ChatMessage::user("Hello!")];
/// let output = template.format_conversation(&messages).expect("format conversation should succeed");
/// assert!(output.contains("Instruct:"));
/// ```
#[derive(Debug, Clone)]
pub struct PhiTemplate {
    special_tokens: SpecialTokens,
}

impl PhiTemplate {
    /// Create a new Phi template
    #[must_use]
    pub fn new() -> Self {
        Self {
            special_tokens: SpecialTokens::default(),
        }
    }
}

impl Default for PhiTemplate {
    fn default() -> Self {
        Self::new()
    }
}

impl ChatTemplateEngine for PhiTemplate {
    fn format_message(&self, role: &str, content: &str) -> Result<String, AprenderError> {
        // Sanitize user content to prevent prompt injection (GH-204)
        let safe_content = if role == "user" {
            sanitize_user_content(content)
        } else {
            content.to_string()
        };
        match role {
            "user" => Ok(format!("Instruct: {safe_content}\n")),
            "assistant" => Ok(format!("Output: {safe_content}\n")),
            "system" => Ok(format!("{safe_content}\n")),
            _ => Ok(safe_content),
        }
    }

    fn format_conversation(&self, messages: &[ChatMessage]) -> Result<String, AprenderError> {
        let mut result = String::new();

        for msg in messages {
            match msg.role.as_str() {
                "system" => {
                    result.push_str(&msg.content);
                    result.push('\n');
                }
                "user" => {
                    // Sanitize user content to prevent prompt injection (GH-204)
                    let safe_content = sanitize_user_content(&msg.content);
                    result.push_str("Instruct: ");
                    result.push_str(&safe_content);
                    result.push('\n');
                }
                "assistant" => {
                    result.push_str("Output: ");
                    result.push_str(&msg.content);
                    result.push('\n');
                }
                _ => {}
            }
        }

        // Add generation prompt
        result.push_str("Output:");

        Ok(result)
    }

    fn special_tokens(&self) -> &SpecialTokens {
        &self.special_tokens
    }

    fn format(&self) -> TemplateFormat {
        TemplateFormat::Phi
    }

    fn supports_system_prompt(&self) -> bool {
        true
    }
}

/// Alpaca Template
///
/// Format: `### Instruction:\n{content}\n\n### Response:`
///
/// # Example
///
/// ```
/// use aprender::text::chat_template::{ChatMessage, AlpacaTemplate, ChatTemplateEngine};
///
/// let template = AlpacaTemplate::new();
/// let messages = vec![ChatMessage::user("Hello!")];
/// let output = template.format_conversation(&messages).expect("format conversation should succeed");
/// assert!(output.contains("### Instruction:"));
/// ```
#[derive(Debug, Clone)]
pub struct AlpacaTemplate {
    special_tokens: SpecialTokens,
}

impl AlpacaTemplate {
    /// Create a new Alpaca template
    #[must_use]
    pub fn new() -> Self {
        Self {
            special_tokens: SpecialTokens::default(),
        }
    }
}

impl Default for AlpacaTemplate {
    fn default() -> Self {
        Self::new()
    }
}