|
liblloyal 1.0.0
Branched Inference for llama.cpp
|
Functions | |
| std::string | from_json_schema (const std::string &schema_json) |
| Convert JSON schema to GBNF (Grammar BNF) format. | |
| llama_sampler * | init_sampler (const llama_model *model, const std::string &grammar_str, const std::string &root_rule="root") |
| Initialize a grammar sampler from GBNF grammar string. | |
| llama_sampler * | init_lazy_sampler (const llama_model *model, const std::string &grammar_str, const std::vector< std::string > &trigger_patterns, const std::vector< llama_token > &trigger_tokens, const std::string &root_rule="root") |
| Initialize a lazy grammar sampler from GBNF grammar string. | |
| llama_sampler * | clone_sampler (llama_sampler *smpl) |
| Clone a grammar sampler (for fork/branching). | |
| void | free_sampler (llama_sampler *smpl) |
| Free a grammar sampler. | |
| void | apply (llama_sampler *smpl, llama_token_data_array *cur_p) |
| Apply grammar constraint to candidates. | |
| void | accept (llama_sampler *smpl, llama_token token) |
| Accept a token into grammar state. | |
|
inline |
Accept a token into grammar state.
Advances the grammar parser state.
| smpl | Grammar sampler |
| token | Token to accept |
Definition at line 262 of file grammar.hpp.
|
inline |
Apply grammar constraint to candidates.
Modifies candidates in-place, masking tokens that violate grammar.
| smpl | Grammar sampler |
| cur_p | Candidate array (modified in-place) |
Definition at line 248 of file grammar.hpp.
|
inline |
Clone a grammar sampler (for fork/branching).
Creates a deep copy of the sampler including its parser state. Use when forking a stepper to preserve grammar position.
| smpl | Source sampler to clone |
OWNERSHIP: Caller owns returned sampler and must call llama_sampler_free()
Definition at line 212 of file grammar.hpp.
|
inline |
Free a grammar sampler.
| smpl | Sampler to free (safe to call with nullptr) |
Definition at line 234 of file grammar.hpp.
|
inline |
Convert JSON schema to GBNF (Grammar BNF) format.
| schema_json | JSON schema string (e.g., {"type": "object", "properties": {...}}) |
| std::runtime_error | on parse error or conversion failure |
EXAMPLE: std::string schema = R"({"type": "object", "properties": {"name": {"type": "string"}}})"; std::string gbnf = grammar::from_json_schema(schema);
Definition at line 46 of file grammar.hpp.
|
inline |
Initialize a lazy grammar sampler from GBNF grammar string.
Generation runs unconstrained until a trigger pattern or token fires, at which point the grammar activates and constrains subsequent tokens. Used for tool-call generation: model writes freely until <tool_call>, then grammar forces valid XML structure.
| model | Llama model (for vocab extraction) |
| grammar_str | GBNF grammar string |
| trigger_patterns | Regex patterns that activate the grammar |
| trigger_tokens | Token IDs that activate the grammar |
| root_rule | Root rule name (default: "root") |
OWNERSHIP: Caller owns returned sampler and must call llama_sampler_free()
Definition at line 157 of file grammar.hpp.
|
inline |
Initialize a grammar sampler from GBNF grammar string.
Convenience wrapper that handles vocab extraction from model.
| model | Llama model (for vocab extraction) |
| grammar_str | GBNF grammar string (from from_json_schema or hand-written) |
| root_rule | Root rule name (default: "root") |
OWNERSHIP: Caller owns returned sampler and must call llama_sampler_free()
EXAMPLE: std::string gbnf = grammar::from_json_schema(schema); llama_sampler* sampler = grammar::init_sampler(model, gbnf); // ... use sampler ... llama_sampler_free(sampler);
Definition at line 106 of file grammar.hpp.