llama-cpp-4 0.7.0

llama.cpp bindings for Rust
Documentation
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
463
464
465
466
467
468
469
470
471
472
473
474
//! End-to-end tests for the chat / tool-calling layer.
//!
//! These drive llama.cpp's real Jinja engine and PEG parsers through the shim,
//! so they need a model — but not a *capable* one. The template is supplied as
//! an override, which is what lets a 260K-parameter story model exercise
//! Hermes-style tool calling: the template and parser are pure text processing,
//! independent of what the weights can actually produce.

mod support;

use llama_cpp_4::chat::{
    format_name, json_schema_to_grammar, ChatApplyParams, ChatTemplates, ReasoningFormat,
    ToolChoice,
};

use support::model::load_model;

/// The real Hermes-2-Pro tool-use template, vendored with llama.cpp.
///
/// It has to be a template upstream *recognises*: `common_chat_templates_apply`
/// only reaches the specialized per-family parsers (which emit a GBNF grammar
/// and lazy triggers) when it matches a known template source. An ad-hoc
/// lookalike falls through to the generic autoparser, which produces a PEG
/// parser and no grammar — still correct, but it would not exercise the path
/// that makes `tool_choice: required` binding.
const HERMES_TOOL_TEMPLATE: &str = include_str!(
    "../../llama-cpp-sys-4/llama.cpp/models/templates/NousResearch-Hermes-2-Pro-Llama-3-8B-tool_use.jinja"
);

const WEATHER_TOOL: &str = r#"[{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get the current weather for a city.",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"]
        }
    }
}]"#;

fn templates() -> Option<ChatTemplates> {
    let (model, _) = load_model()?;
    match ChatTemplates::from_model(&model, Some(HERMES_TOOL_TEMPLATE)) {
        Ok(t) => Some(t),
        Err(e) => panic!("could not build chat templates: {e}"),
    }
}

#[test]
fn templates_report_explicit_override() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    assert!(
        tmpls.was_explicit(),
        "an override template must report as explicit"
    );
}

#[test]
fn templates_report_caps_as_json_object() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let caps = tmpls.caps_json().expect("caps");
    assert!(caps.starts_with('{'), "caps must be a JSON object: {caps}");
}

/// The template must actually render the messages into the prompt — otherwise
/// everything downstream is operating on an empty string, which is the failure
/// mode the server example hit (`prompt_tokens: 0`).
#[test]
fn apply_renders_messages_into_the_prompt() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let applied = tmpls
        .apply(&ChatApplyParams::new(
            r#"[{"role":"user","content":"Weather in Tokyo?"}]"#,
        ))
        .expect("apply");
    assert!(
        applied.prompt.contains("Weather in Tokyo?"),
        "prompt did not include the user turn: {}",
        applied.prompt
    );
    assert!(
        applied.prompt.contains("assistant"),
        "generation prompt missing: {}",
        applied.prompt
    );
}

#[test]
fn apply_omits_generation_prompt_when_asked() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let applied = tmpls
        .apply(
            &ChatApplyParams::new(r#"[{"role":"user","content":"hi"}]"#)
                .with_add_generation_prompt(false),
        )
        .expect("apply");
    let with_prompt = tmpls
        .apply(&ChatApplyParams::new(r#"[{"role":"user","content":"hi"}]"#))
        .expect("apply");
    assert!(
        applied.prompt.len() < with_prompt.prompt.len(),
        "omitting the generation prompt should shorten the render:\n{}\nvs\n{}",
        applied.prompt,
        with_prompt.prompt
    );
}

/// Tools must reach the rendered prompt, and the template must be recognised as
/// a tool-calling format rather than falling back to content-only.
#[test]
fn apply_with_tools_produces_a_tool_format() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let applied = tmpls
        .apply(
            &ChatApplyParams::new(r#"[{"role":"user","content":"Weather in Tokyo?"}]"#)
                .with_tools(WEATHER_TOOL),
        )
        .expect("apply");

    assert!(
        applied.prompt.contains("get_weather"),
        "tool definition missing from prompt: {}",
        applied.prompt
    );
    let name = applied.format_name().expect("format name");
    assert!(!name.is_empty(), "format has no name");
}

/// `tool_choice: required` is the case the hand-rolled server path could not
/// implement: upstream enforces it with a grammar, not a prompt. If this is
/// empty, nothing is actually forcing a call.
#[test]
fn tool_choice_required_yields_a_grammar() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let applied = tmpls
        .apply(
            &ChatApplyParams::new(r#"[{"role":"user","content":"Weather in Tokyo?"}]"#)
                .with_tools(WEATHER_TOOL)
                .with_tool_choice(ToolChoice::Required),
        )
        .expect("apply");

    assert!(
        !applied.grammar.is_empty(),
        "tool_choice=required produced no grammar"
    );
    assert!(
        applied.grammar.contains("get_weather"),
        "grammar does not mention the tool: {}",
        applied.grammar
    );
}

/// The point of the whole exercise: with `tool_choice: auto` the grammar must
/// be *lazy* and carry triggers. A non-lazy grammar applied from token zero is
/// what stops a thinking model ever opening its reasoning block — the exact
/// reason the hand-rolled server path gave up on grammar forcing.
#[test]
fn auto_tool_choice_yields_a_lazy_grammar_with_triggers() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let applied = tmpls
        .apply(
            &ChatApplyParams::new(r#"[{"role":"user","content":"Weather in Tokyo?"}]"#)
                .with_tools(WEATHER_TOOL)
                .with_tool_choice(ToolChoice::Auto),
        )
        .expect("apply");

    assert!(
        applied.grammar_lazy,
        "auto tool choice must produce a lazy grammar, else reasoning is blocked"
    );
    assert!(
        !applied.grammar_triggers.is_empty(),
        "a lazy grammar with no triggers can never activate: {}",
        applied.grammar_triggers_json
    );
    // The trigger is what the model must emit before the grammar engages.
    assert!(
        applied
            .grammar_triggers
            .iter()
            .any(|t| t.value.contains("tool_call")),
        "expected a <tool_call> trigger, got {:?}",
        applied.grammar_triggers
    );
}

/// `required` must NOT be lazy: there is nothing to wait for when a call is
/// mandatory from the first token.
#[test]
fn required_tool_choice_is_not_lazy() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let applied = tmpls
        .apply(
            &ChatApplyParams::new(r#"[{"role":"user","content":"Weather?"}]"#)
                .with_tools(WEATHER_TOOL)
                .with_tool_choice(ToolChoice::Required),
        )
        .expect("apply");
    assert!(
        !applied.grammar_lazy,
        "required tool choice should constrain from the start"
    );
}

/// `tool_choice: none` must not force a call, even with tools present.
#[test]
fn tool_choice_none_does_not_force_a_call() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let applied = tmpls
        .apply(
            &ChatApplyParams::new(r#"[{"role":"user","content":"hi"}]"#)
                .with_tools(WEATHER_TOOL)
                .with_tool_choice(ToolChoice::None),
        )
        .expect("apply");
    assert!(
        applied.grammar.is_empty() || !applied.grammar.contains("get_weather"),
        "tool_choice=none should not force a tool call: {}",
        applied.grammar
    );
}

/// A JSON Schema on the request must become a grammar constraining the answer —
/// this is `response_format: json_schema`.
#[test]
fn json_schema_constrains_the_response() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let applied = tmpls
        .apply(
            &ChatApplyParams::new(r#"[{"role":"user","content":"describe a city"}]"#)
                .with_json_schema(
                    r#"{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}"#,
                ),
        )
        .expect("apply");
    assert!(
        applied.grammar.contains("city"),
        "schema did not constrain the grammar: {}",
        applied.grammar
    );
}

/// The round trip that matters: apply, then parse a tool call back out. The
/// parser comes from the template, so it understands this family's syntax.
#[test]
fn parse_extracts_a_tool_call_from_model_output() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let applied = tmpls
        .apply(
            &ChatApplyParams::new(r#"[{"role":"user","content":"Weather in Tokyo?"}]"#)
                .with_tools(WEATHER_TOOL)
                .with_tool_choice(ToolChoice::Required),
        )
        .expect("apply");

    let output = r#"<tool_call>
{"name": "get_weather", "arguments": {"city": "Tokyo"}}
</tool_call>"#;
    let parsed = applied.parse(output, false).expect("parse");

    assert!(
        parsed.contains("get_weather"),
        "tool call not recovered from output: {parsed}"
    );
    assert!(
        parsed.contains("Tokyo"),
        "tool arguments not recovered: {parsed}"
    );
}

/// Plain prose must come back as content, not be mistaken for a call.
#[test]
fn parse_returns_plain_content_when_no_tool_is_called() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let applied = tmpls
        .apply(&ChatApplyParams::new(
            r#"[{"role":"user","content":"say hi"}]"#,
        ))
        .expect("apply");
    let parsed = applied.parse("Hello there.", false).expect("parse");
    assert!(
        parsed.contains("Hello there."),
        "content not preserved: {parsed}"
    );
}

/// A truncated buffer mid-stream must not be an error — streaming callers parse
/// on every delta.
#[test]
fn parse_tolerates_partial_output() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let applied = tmpls
        .apply(
            &ChatApplyParams::new(r#"[{"role":"user","content":"Weather?"}]"#)
                .with_tools(WEATHER_TOOL),
        )
        .expect("apply");
    let partial = r#"<tool_call>
{"name": "get_weat"#;
    applied
        .parse(partial, true)
        .expect("partial parse must not fail");
}

/// Reasoning must be separable from content — the whole reason lazy grammars
/// exist. With `enable_thinking`, a `<think>` block must not end up in
/// `content`.
#[test]
fn parse_splits_reasoning_from_content() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let applied = tmpls
        .apply(
            &ChatApplyParams::new(r#"[{"role":"user","content":"think then answer"}]"#)
                .with_reasoning_format(ReasoningFormat::DeepSeek),
        )
        .expect("apply");

    // Whether the split happens depends on the template advertising thinking;
    // what must always hold is that parsing succeeds and preserves the answer.
    let parsed = applied
        .parse("<think>weighing it up</think>The answer.", false)
        .expect("parse");
    assert!(
        parsed.contains("The answer."),
        "answer lost while handling reasoning: {parsed}"
    );
}

/// Malformed message JSON must surface as an error, not a panic or UB — the
/// underlying C++ throws.
#[test]
fn apply_rejects_malformed_messages() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    assert!(tmpls.apply(&ChatApplyParams::new("{not json")).is_err());
}

#[test]
fn apply_rejects_malformed_tools() {
    let Some(tmpls) = templates() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    assert!(tmpls
        .apply(&ChatApplyParams::new(r#"[{"role":"user","content":"hi"}]"#).with_tools("[[["))
        .is_err());
}

/// A grammar built from a schema must be usable by the sampler, which is the
/// only thing that makes structured output binding rather than advisory.
#[test]
fn schema_grammar_is_accepted_by_the_sampler() {
    use llama_cpp_4::sampling::LlamaSampler;

    let Some((model, _)) = load_model() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let gbnf = json_schema_to_grammar(
        r#"{"type":"object","properties":{"n":{"type":"integer"}},"required":["n"]}"#,
        false,
    )
    .expect("schema to grammar");

    // Constructing the sampler is the check: llama.cpp parses the GBNF here and
    // would reject a malformed grammar.
    let _sampler = LlamaSampler::grammar(&model, &gbnf, "root");
}

/// A *string*-valued object schema, which is what real callers send. The
/// integer-only case above does not exercise GBNF's `char`/`string` rules.
#[test]
fn string_schema_grammar_is_accepted_by_the_sampler() {
    use llama_cpp_4::sampling::LlamaSampler;

    let Some((model, _)) = load_model() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let gbnf = json_schema_to_grammar(
        r#"{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}"#,
        false,
    )
    .expect("schema to grammar");
    let _sampler = LlamaSampler::grammar(&model, &gbnf, "root");
}

#[test]
fn format_name_is_stable_for_content_only() {
    // 0 is COMMON_CHAT_FORMAT_CONTENT_ONLY, the default every template falls
    // back to; it must always name itself.
    let name = format_name(0).expect("format name");
    assert!(!name.is_empty());
}

/// A model with no chat template of its own must still yield usable templates:
/// llama.cpp falls back to a builtin `ChatML` program. Callers therefore need no
/// fallback path of their own — `examples/server` relies on this to accept any
/// model that loads, and the test checkpoint here ships no template at all.
#[test]
fn templates_fall_back_to_a_builtin_for_a_model_without_one() {
    let Some((model, _)) = load_model() else {
        eprintln!("SKIP: no test model available");
        return;
    };
    let tmpls = ChatTemplates::from_model(&model, None)
        .expect("a model with no template must still get the builtin fallback");
    let source = tmpls.source(None).expect("template source");
    assert!(!source.is_empty(), "fallback template is empty");
    assert!(
        !tmpls.was_explicit(),
        "a fallback is not an explicit override"
    );

    // It must actually render, not just exist.
    let applied = tmpls
        .apply(&ChatApplyParams::new(
            r#"[{"role":"user","content":"ping"}]"#,
        ))
        .expect("fallback template must render");
    assert!(
        applied.prompt.contains("ping"),
        "fallback did not render the turn: {}",
        applied.prompt
    );
}