Skip to main content

Module grammar

Module grammar 

Source
Expand description

Stateful GBNF grammar + DRY samplers.

ik_llama.cpp exposes two stateful sampler objects that do not fit the stateless llama_sample_* array API modelled in crate::sampling (they own C-side state that advances as tokens are accepted):

  • LlamaGrammar — a GBNF grammar constraint. Unlike the stock llama-cpp-2 anchor (which parses GBNF in Rust and calls the now commented-out llama_grammar_init), ik parses the grammar C-side via llama_sampler_init_grammar(vocab, grammar_str, root). The returned grammar retains a pointer to the model’s vocab, so it is lifetime-tied to the LlamaModel it was built from.
  • LlamaDrySampler — the DRY (“Don’t Repeat Yourself”) repetition sampler. It processes its sequence breakers against the vocab only at init, so it does not retain the model.

Both own a raw C object and free it on Drop. Typical use inside a generation loop: LlamaGrammar::apply / LlamaDrySampler::apply mutate the candidate array in place before you draw a token; then LlamaGrammar::accept_token / LlamaDrySampler::accept advance the sampler state with the token you drew.

Structs§

DryParams
Parameters for the DRY (“Don’t Repeat Yourself”) sampler.
LlamaDrySampler
The stateful DRY repetition sampler.
LlamaGrammar
A stateful GBNF grammar constraint.

Enums§

DryInitError
Error building a LlamaDrySampler.
GrammarInitError
Error building a LlamaGrammar.
JsonSchemaError
Error converting a JSON Schema into a GBNF grammar via json_schema_to_grammar (the common feature).

Functions§

json_schema_to_grammar
Convert a JSON Schema (given as a JSON string) into a GBNF grammar string, ready to pass to LlamaGrammar::new.