Namespace: LLama.Grammars
A grammar is a set of GrammarRules for deciding which characters are valid next. Can be used to constrain
output to certain formats - e.g. force the model to output JSON
public sealed class Grammar
Index of the initial rule to start from
public ulong StartRuleIndex { get; set; }
The rules which make up this grammar
public IReadOnlyList<GrammarRule> Rules { get; }
Create a new grammar from a set of rules
public Grammar(IReadOnlyList<GrammarRule> rules, ulong startRuleIndex)
rules IReadOnlyList<GrammarRule>
The rules which make up this grammar
startRuleIndex UInt64
Index of the initial rule to start from
Create a SafeLLamaGrammarHandle instance to use for parsing
public SafeLLamaGrammarHandle CreateInstance()
Parse a string of GGML BNF into a Grammar
public static Grammar Parse(string gbnf, string startRule)
gbnf String
The string to parse
startRule String
Name of the start rule of this grammar
Grammar
A Grammar which can be converted into a SafeLLamaGrammarHandle for sampling
GrammarFormatException
Thrown if input is malformed
public string ToString()