liblloyal 1.0.0
Composable primitives for llama.cpp inference
Loading...
Searching...
No Matches
lloyal::grammar Namespace Reference

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.
 

Function Documentation

◆ accept()

void lloyal::grammar::accept ( llama_sampler *  smpl,
llama_token  token 
)
inline

Accept a token into grammar state.

Advances the grammar parser state.

Parameters
smplGrammar sampler
tokenToken to accept
Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/grammar.hpp.

Definition at line 200 of file grammar.hpp.

◆ apply()

void lloyal::grammar::apply ( llama_sampler *  smpl,
llama_token_data_array *  cur_p 
)
inline

Apply grammar constraint to candidates.

Modifies candidates in-place, masking tokens that violate grammar.

Parameters
smplGrammar sampler
cur_pCandidate array (modified in-place)
Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/grammar.hpp.

Definition at line 186 of file grammar.hpp.

◆ clone_sampler()

llama_sampler * lloyal::grammar::clone_sampler ( llama_sampler *  smpl)
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.

Parameters
smplSource sampler to clone
Returns
New sampler with same state, or nullptr if input was null

OWNERSHIP: Caller owns returned sampler and must call llama_sampler_free()

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/grammar.hpp.

Definition at line 150 of file grammar.hpp.

◆ free_sampler()

void lloyal::grammar::free_sampler ( llama_sampler *  smpl)
inline

Free a grammar sampler.

Parameters
smplSampler to free (safe to call with nullptr)
Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/grammar.hpp.

Definition at line 172 of file grammar.hpp.

◆ from_json_schema()

std::string lloyal::grammar::from_json_schema ( const std::string &  schema_json)
inline

Convert JSON schema to GBNF (Grammar BNF) format.

Parameters
schema_jsonJSON schema string (e.g., {"type": "object", "properties": {...}})
Returns
GBNF grammar string compatible with llama_sampler_init_grammar()
Exceptions
std::runtime_erroron parse error or conversion failure

EXAMPLE: std::string schema = R"({"type": "object", "properties": {"name": {"type": "string"}}})"; std::string gbnf = grammar::from_json_schema(schema);

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/grammar.hpp.

Definition at line 45 of file grammar.hpp.

◆ init_sampler()

llama_sampler * lloyal::grammar::init_sampler ( const llama_model *  model,
const std::string &  grammar_str,
const std::string &  root_rule = "root" 
)
inline

Initialize a grammar sampler from GBNF grammar string.

Convenience wrapper that handles vocab extraction from model.

Parameters
modelLlama model (for vocab extraction)
grammar_strGBNF grammar string (from from_json_schema or hand-written)
root_ruleRoot rule name (default: "root")
Returns
Initialized grammar sampler, or nullptr on failure

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);

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/grammar.hpp.

Definition at line 105 of file grammar.hpp.