Grammar
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
Properties
StartRuleIndex
Index of the initial rule to start from
public ulong StartRuleIndex { get; set; }
Property Value
Rules
The rules which make up this grammar
public IReadOnlyList<GrammarRule> Rules { get; }
Property Value
Constructors
Grammar(IReadOnlyList<GrammarRule>, UInt64)
Create a new grammar from a set of rules
public Grammar(IReadOnlyList<GrammarRule> rules, ulong startRuleIndex)
Parameters
rules
IReadOnlyList<GrammarRule>
The rules which make up this grammar
startRuleIndex
UInt64
Index of the initial rule to start from
Exceptions
Methods
CreateInstance()
Create a SafeLLamaGrammarHandle
instance to use for parsing
public SafeLLamaGrammarHandle CreateInstance()
Returns
Parse(String, String)
Parse a string of GGML BNF into a Grammar
public static Grammar Parse(string gbnf, string startRule)
Parameters
gbnf
String
The string to parse
startRule
String
Name of the start rule of this grammar
Returns
Grammar
A Grammar which can be converted into a SafeLLamaGrammarHandle for sampling
Exceptions
GrammarFormatException
Thrown if input is malformed
ToString()
public string ToString()