|
liblloyal 1.0.0
Composable primitives for llama.cpp inference
|
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 * | 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 200 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 186 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 150 of file grammar.hpp.
|
inline |
Free a grammar sampler.
| smpl | Sampler to free (safe to call with nullptr) |
Definition at line 172 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 45 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 105 of file grammar.hpp.