Constructor Grammar
- Namespace
- LMKit.TextGeneration.Sampling
- Assembly
- LM-Kit.NET.dll
Grammar(string, string)
Creates a new instance of the Grammar class by parsing a GGML BNF (Backus-Naur Form) grammar definition string. This allows for the creation of custom grammars to constrain the output of text generation models, ensuring that generated text adheres to the specified grammar rules. For more information on GGML BNF, see the GGML BNF documentation.
public Grammar(string gbnf, string startRule = "root")
Parameters
gbnf
stringA string containing the grammar definitions in GGML BNF format.
startRule
stringThe name of the start rule for this grammar. Defaults to "root".
Exceptions
- GrammarParsingException
Thrown if the input string is malformed or cannot be parsed into a valid grammar.
Grammar(PredefinedGrammar)
Creates a new instance of the Grammar class using a predefined grammar type. This enables constrained output generation based on standard grammars like JSON, arithmetic expressions, or lists. Using predefined grammars simplifies the process of enforcing output structure without the need to define custom grammars.
public Grammar(Grammar.PredefinedGrammar type)
Parameters
type
Grammar.PredefinedGrammarThe type of predefined grammar to create.
Exceptions
- ArgumentException
Thrown if the specified
type
is not recognized.