Llama::Sampler::Grammar
Inherits Llama::Sampler::Base / Reference / Object
Grammar sampler
The Grammar sampler constrains token generation to follow a formal grammar defined in GBNF format. This is useful for generating structured text like JSON, XML, or code that must follow specific syntax rules.
Example:
# Define a simple grammar for a numbered list
grammar = %q{
root ::= list
list ::= item+
item ::= number " " text "\n"
number ::= "1" | "2" | "3" | "4" | "5"
text ::= [a-zA-Z ]+
}
sampler = Llama::Sampler::Grammar.new(model.vocab, grammar, "root")