mullama 0.3.0

Comprehensive Rust bindings for llama.cpp with memory-safe API and advanced features
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Modelfile Parser for Mullama
//!
//! Supports both Ollama-compatible Modelfile syntax and extended Mullamafile features.
//!
//! ## Ollama-Compatible Keys (Modelfile)
//!
//! ```dockerfile
//! FROM llama3.2:1b
//! PARAMETER temperature 0.7
//! PARAMETER num_ctx 8192
//! SYSTEM """
//! You are a helpful assistant.
//! """
//! TEMPLATE """
//! {{ .System }}
//! {{ .Prompt }}
//! """
//! MESSAGE user Hello!
//! MESSAGE assistant Hi there!
//! ```
//!
//! ## Mullama Extensions (Mullamafile)
//!
//! ```dockerfile
//! FROM hf:meta-llama/Llama-3.2-1B-Instruct-GGUF:Q4_K_M
//!
//! # Standard Modelfile keys
//! PARAMETER temperature 0.7
//! PARAMETER num_ctx 8192
//!
//! SYSTEM """
//! You are a helpful assistant.
//! """
//!
//! # Mullama-specific extensions
//! ADAPTER ./my-lora-adapter.safetensors
//! GPU_LAYERS 32
//! FLASH_ATTENTION true
//! VISION_PROJECTOR ./mmproj.gguf
//!
//! # Metadata
//! LICENSE MIT
//! AUTHOR "Your Name"
//! ```

mod parser;
mod types;

pub use parser::{find_modelfile, ModelfileParser};
pub use types::{
    Capabilities, ExecutionRecord, Message, Modelfile, ModelfileError, ParameterValue,
    ThinkingConfig, ToolFormat,
};

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_basic_modelfile() {
        let content = r#"
FROM llama3.2:1b
PARAMETER temperature 0.7
PARAMETER num_ctx 4096
SYSTEM "You are a helpful assistant."
"#;

        let parser = ModelfileParser::new();
        let modelfile = parser.parse_str(content).unwrap();

        assert_eq!(modelfile.from, "llama3.2:1b");
        assert_eq!(modelfile.temperature(), Some(0.7));
        assert_eq!(modelfile.num_ctx(), Some(4096));
        assert_eq!(
            modelfile.system,
            Some("You are a helpful assistant.".to_string())
        );
    }

    #[test]
    fn test_parse_multiline_system() {
        let content = r#"
FROM llama3.2:1b
SYSTEM """
You are a helpful assistant.
You answer questions clearly.
"""
"#;

        let parser = ModelfileParser::new();
        let modelfile = parser.parse_str(content).unwrap();

        assert!(modelfile.system.is_some());
        assert!(modelfile
            .system
            .as_ref()
            .unwrap()
            .contains("You are a helpful assistant."));
        assert!(modelfile
            .system
            .as_ref()
            .unwrap()
            .contains("You answer questions clearly."));
    }

    #[test]
    fn test_parse_single_line_triple_quoted_system() {
        let content = r#"
FROM llama3.2:1b
SYSTEM """You are a helpful assistant."""
"#;

        let parser = ModelfileParser::new();
        let modelfile = parser.parse_str(content).unwrap();

        assert_eq!(
            modelfile.system,
            Some("You are a helpful assistant.".to_string())
        );
    }

    #[test]
    fn test_parse_multiline_template_with_inline_close() {
        let content = r#"
FROM llama3.2:1b
TEMPLATE """Line one
Line two"""
"#;

        let parser = ModelfileParser::new();
        let modelfile = parser.parse_str(content).unwrap();

        assert_eq!(modelfile.template, Some("Line one\nLine two".to_string()));
    }

    #[test]
    fn test_parse_mullama_extensions() {
        let content = r#"
FROM hf:meta-llama/Llama-3.2-1B-Instruct-GGUF:Q4_K_M
GPU_LAYERS 32
FLASH_ATTENTION true
VISION_PROJECTOR ./mmproj.gguf
ADAPTER ./lora.safetensors
"#;

        let parser = ModelfileParser::new();
        let modelfile = parser.parse_str(content).unwrap();

        assert!(modelfile.is_huggingface());
        assert_eq!(modelfile.gpu_layers, Some(32));
        assert_eq!(modelfile.flash_attention, Some(true));
        assert!(modelfile.vision_projector.is_some());
        assert!(modelfile.adapter.is_some());
    }

    #[test]
    fn test_ollama_compat_rejects_extensions() {
        let content = r#"
FROM llama3.2:1b
GPU_LAYERS 32
"#;

        let parser = ModelfileParser::ollama_compatible();
        let result = parser.parse_str(content);

        assert!(result.is_err());
    }

    #[test]
    fn test_parse_messages() {
        let content = r#"
FROM llama3.2:1b
MESSAGE user "Hello!"
MESSAGE assistant "Hi there! How can I help?"
"#;

        let parser = ModelfileParser::new();
        let modelfile = parser.parse_str(content).unwrap();

        assert_eq!(modelfile.messages.len(), 2);
        assert_eq!(modelfile.messages[0].role, "user");
        assert_eq!(modelfile.messages[0].content, "Hello!");
        assert_eq!(modelfile.messages[1].role, "assistant");
    }

    #[test]
    fn test_missing_from() {
        let content = r#"
PARAMETER temperature 0.7
"#;

        let parser = ModelfileParser::new();
        let result = parser.parse_str(content);

        assert!(matches!(result, Err(ModelfileError::MissingFrom)));
    }

    #[test]
    fn test_serialize_modelfile() {
        let mut modelfile = Modelfile::from_model("llama3.2:1b");
        modelfile.set_parameter("temperature", ParameterValue::Float(0.7));
        modelfile.system = Some("You are helpful.".to_string());
        modelfile.gpu_layers = Some(32);

        let output = modelfile.to_string();

        assert!(output.contains("FROM llama3.2:1b"));
        assert!(output.contains("PARAMETER temperature 0.7"));
        assert!(output.contains("SYSTEM"));
        assert!(output.contains("GPU_LAYERS 32"));
    }

    #[test]
    fn test_is_alias() {
        let m1 = Modelfile::from_model("llama3.2:1b");
        assert!(m1.is_alias());

        let m2 = Modelfile::from_model("hf:TheBloke/Llama-2-7B-GGUF");
        assert!(m2.is_huggingface());
        assert!(!m2.is_alias());

        let m3 = Modelfile::from_model("./model.gguf");
        assert!(m3.is_local_path());
        assert!(!m3.is_alias());
    }

    #[test]
    fn test_parse_thinking_config() {
        let content = r#"
FROM qwq:32b
THINKING start "<think>"
THINKING end "</think>"
THINKING enabled true
CAPABILITY thinking true
"#;

        let parser = ModelfileParser::new();
        let modelfile = parser.parse_str(content).unwrap();

        assert!(modelfile.thinking.is_some());
        let thinking = modelfile.thinking.as_ref().unwrap();
        assert_eq!(thinking.start_token, "<think>");
        assert_eq!(thinking.end_token, "</think>");
        assert!(thinking.enabled);
        assert!(modelfile.capabilities.thinking);
    }

    #[test]
    fn test_parse_multiple_stop_sequences() {
        let content = r#"
FROM qwen2.5:7b
PARAMETER stop "<|im_end|>"
PARAMETER stop "<|endoftext|>"
"#;

        let parser = ModelfileParser::new();
        let modelfile = parser.parse_str(content).unwrap();

        assert_eq!(modelfile.stop_sequences.len(), 2);
        assert!(modelfile.stop_sequences.contains(&"<|im_end|>".to_string()));
        assert!(modelfile
            .stop_sequences
            .contains(&"<|endoftext|>".to_string()));
    }

    #[test]
    fn test_parse_capabilities() {
        let content = r#"
FROM llama3.2:1b
CAPABILITY json true
CAPABILITY tools true
CAPABILITY thinking false
CAPABILITY vision true
"#;

        let parser = ModelfileParser::new();
        let modelfile = parser.parse_str(content).unwrap();

        assert!(modelfile.capabilities.json);
        assert!(modelfile.capabilities.tools);
        assert!(!modelfile.capabilities.thinking);
        assert!(modelfile.capabilities.vision);
    }

    #[test]
    fn test_parse_toolformat() {
        let content = r#"
FROM qwen2.5:7b
TOOLFORMAT style "qwen"
TOOLFORMAT call_start "<tool_call>"
TOOLFORMAT call_end "</tool_call>"
TOOLFORMAT result_start "<tool_response>"
TOOLFORMAT result_end "</tool_response>"
"#;

        let parser = ModelfileParser::new();
        let modelfile = parser.parse_str(content).unwrap();

        assert!(modelfile.tool_format.is_some());
        let tf = modelfile.tool_format.as_ref().unwrap();
        assert_eq!(tf.style, "qwen");
        assert_eq!(tf.call_start, "<tool_call>");
        assert_eq!(tf.call_end, "</tool_call>");
        assert_eq!(tf.result_start, "<tool_response>");
        assert_eq!(tf.result_end, "</tool_response>");
    }

    #[test]
    fn test_serialize_thinking_and_capabilities() {
        let mut modelfile = Modelfile::from_model("qwq:32b");
        modelfile.thinking = Some(ThinkingConfig {
            start_token: "<think>".to_string(),
            end_token: "</think>".to_string(),
            enabled: true,
        });
        modelfile.capabilities.thinking = true;
        modelfile.stop_sequences.push("<|im_end|>".to_string());

        let output = modelfile.to_string();

        assert!(output.contains("THINKING start \"<think>\""));
        assert!(output.contains("THINKING end \"</think>\""));
        assert!(output.contains("THINKING enabled true"));
        assert!(output.contains("CAPABILITY thinking true"));
        assert!(output.contains("PARAMETER stop \"<|im_end|>\""));
    }

    #[test]
    fn test_parse_revision_from_hf_spec() {
        let content = r#"
FROM hf:meta-llama/Llama-3.2-1B-Instruct-GGUF@abc123def
PARAMETER temperature 0.7
"#;

        let parser = ModelfileParser::new();
        let modelfile = parser.parse_str(content).unwrap();

        assert_eq!(modelfile.from, "hf:meta-llama/Llama-3.2-1B-Instruct-GGUF");
        assert_eq!(modelfile.revision, Some("abc123def".to_string()));
        assert_eq!(
            modelfile.full_model_ref(),
            "hf:meta-llama/Llama-3.2-1B-Instruct-GGUF@abc123def"
        );
    }

    #[test]
    fn test_parse_digest() {
        let content = r#"
FROM hf:org/model
DIGEST sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
"#;

        let parser = ModelfileParser::new();
        let modelfile = parser.parse_str(content).unwrap();

        assert_eq!(
            modelfile.digest,
            Some(
                "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
                    .to_string()
            )
        );
    }

    #[test]
    fn test_parse_digest_without_prefix() {
        let content = r#"
FROM hf:org/model
DIGEST e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
"#;

        let parser = ModelfileParser::new();
        let modelfile = parser.parse_str(content).unwrap();

        // Should auto-prefix with sha256:
        assert_eq!(
            modelfile.digest,
            Some(
                "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
                    .to_string()
            )
        );
    }

    #[test]
    fn test_serialize_with_revision_and_digest() {
        let mut modelfile = Modelfile::from_model("hf:org/repo");
        modelfile.revision = Some("main".to_string());
        modelfile.digest = Some("sha256:abc123".to_string());

        let output = modelfile.to_string();

        assert!(output.contains("FROM hf:org/repo@main"));
        assert!(output.contains("DIGEST sha256:abc123"));
    }

    #[test]
    fn test_execution_record_id_generation() {
        let id1 = ExecutionRecord::generate_id();
        let id2 = ExecutionRecord::generate_id();

        assert!(id1.starts_with("exec_"));
        assert!(id2.starts_with("exec_"));
        // IDs should be unique (technically could collide but extremely unlikely)
        assert_ne!(id1, id2);
    }

    #[test]
    fn test_execution_record_config_hash() {
        let hash1 = ExecutionRecord::hash_config(0.7, 0.9, 40, 4096, &["<|im_end|>".to_string()]);
        let hash2 = ExecutionRecord::hash_config(0.7, 0.9, 40, 4096, &["<|im_end|>".to_string()]);
        let hash3 = ExecutionRecord::hash_config(0.8, 0.9, 40, 4096, &["<|im_end|>".to_string()]);

        // Same config = same hash
        assert_eq!(hash1, hash2);
        // Different config = different hash
        assert_ne!(hash1, hash3);
    }

    #[test]
    fn test_execution_record_log_line() {
        let record = ExecutionRecord {
            id: "exec_test123".to_string(),
            timestamp: 1700000000000,
            model_digest: "sha256:abcdef123456".to_string(),
            model_ref: "hf:org/model".to_string(),
            revision: None,
            config_hash: "abc123".to_string(),
            backend_version: "1.0.0".to_string(),
            gpu_info: None,
            context_size: 4096,
            gpu_layers: 32,
            temperature: 0.7,
            prompt_tokens: 100,
            completion_tokens: 50,
            duration_ms: 1500,
            success: true,
            error: None,
        };

        let log_line = record.to_log_line();

        assert!(log_line.contains("\"id\":\"exec_test123\""));
        assert!(log_line.contains("\"success\":true"));
        assert!(log_line.contains("\"tokens\":150"));
    }

    #[test]
    fn test_local_path_no_revision_extraction() {
        // Local paths with @ should NOT have revision extracted
        let content = r#"
FROM ./models/my@special-model.gguf
"#;

        let parser = ModelfileParser::new();
        let modelfile = parser.parse_str(content).unwrap();

        // Should keep the full path, no revision extraction
        assert_eq!(modelfile.from, "./models/my@special-model.gguf");
        assert_eq!(modelfile.revision, None);
    }
}