Table of Contents

Class Grammar

Namespace
LMKit.TextGeneration.Sampling
Assembly
LM-Kit.NET.dll

Represents a grammar used in text generation models to define and enforce grammar rules during the generation process. The Grammar class enables constrained output generation by specifying the allowed syntax and structure of the generated text. This allows developers to ensure that the model's output adheres to a predefined format, such as JSON, arithmetic expressions, or custom-defined grammars.

Benefits of using the Grammar class include:

  • Enforcing syntactic correctness in the generated output.
  • Restricting the output to a specific format or language.
  • Reducing the likelihood of invalid or nonsensical outputs.
  • Facilitating the extraction and parsing of generated data.
public sealed class Grammar : IDisposable
Inheritance
Grammar
Implements
Inherited Members

Constructors

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.

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.

Methods

CreateGrammarFromStringList(IEnumerable<string>)

Creates a Grammar instance based on a list of specified string values. This method generates a grammar definition that constrains text generation to produce one of the specified strings.

CreateJsonGrammarFromExtractionElements(IEnumerable<TextExtractionElement>)

Creates a new instance of the Grammar class based on a collection of TextExtractionElement objects. Each TextExtractionElement represents a field or value to be extracted from a JSON structure.

CreateJsonGrammarFromFields(IEnumerable<string>, IEnumerable<ElementType>)

Creates a Grammar instance for constrained text generation based on a collection of JSON field names and their corresponding data types. This method generates a grammar that enforces the generation of JSON objects containing the specified fields with the specified data types, ensuring that the output adheres to the defined structure and types.

CreateJsonGrammarFromJsonScheme(string)

Creates a Grammar instance for constrained text generation based on a provided JSON schema. This method generates a grammar that enforces the generation of JSON structures conforming to the specified schema, ensuring that the output adheres to the defined structure and data types.

CreateJsonGrammarFromTextFields(IEnumerable<string>)

Creates a Grammar instance for constrained text generation based on a collection of JSON text field names. This method generates a grammar that enforces the generation of JSON objects containing the specified fields with string values, ensuring that the output adheres to the defined structure.

Dispose()

Disposes of the Grammar instance and releases all associated resources.