You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

llama.grammars.grammar.md 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Grammar
  2. Namespace: LLama.Grammars
  3. A grammar is a set of [GrammarRule](./llama.grammars.grammarrule.md)s for deciding which characters are valid next. Can be used to constrain
  4. output to certain formats - e.g. force the model to output JSON
  5. ```csharp
  6. public sealed class Grammar
  7. ```
  8. Inheritance [Object](https://docs.microsoft.com/en-us/dotnet/api/system.object) → [Grammar](./llama.grammars.grammar.md)
  9. ## Properties
  10. ### **StartRuleIndex**
  11. Index of the initial rule to start from
  12. ```csharp
  13. public ulong StartRuleIndex { get; set; }
  14. ```
  15. #### Property Value
  16. [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  17. ### **Rules**
  18. The rules which make up this grammar
  19. ```csharp
  20. public IReadOnlyList<GrammarRule> Rules { get; }
  21. ```
  22. #### Property Value
  23. [IReadOnlyList&lt;GrammarRule&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ireadonlylist-1)<br>
  24. ## Constructors
  25. ### **Grammar(IReadOnlyList&lt;GrammarRule&gt;, UInt64)**
  26. Create a new grammar from a set of rules
  27. ```csharp
  28. public Grammar(IReadOnlyList<GrammarRule> rules, ulong startRuleIndex)
  29. ```
  30. #### Parameters
  31. `rules` [IReadOnlyList&lt;GrammarRule&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ireadonlylist-1)<br>
  32. The rules which make up this grammar
  33. `startRuleIndex` [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)<br>
  34. Index of the initial rule to start from
  35. #### Exceptions
  36. [ArgumentOutOfRangeException](https://docs.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception)<br>
  37. ## Methods
  38. ### **CreateInstance()**
  39. Create a `SafeLLamaGrammarHandle` instance to use for parsing
  40. ```csharp
  41. public SafeLLamaGrammarHandle CreateInstance()
  42. ```
  43. #### Returns
  44. [SafeLLamaGrammarHandle](./llama.native.safellamagrammarhandle.md)<br>
  45. ### **Parse(String, String)**
  46. Parse a string of GGML BNF into a Grammar
  47. ```csharp
  48. public static Grammar Parse(string gbnf, string startRule)
  49. ```
  50. #### Parameters
  51. `gbnf` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  52. The string to parse
  53. `startRule` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  54. Name of the start rule of this grammar
  55. #### Returns
  56. [Grammar](./llama.grammars.grammar.md)<br>
  57. A Grammar which can be converted into a SafeLLamaGrammarHandle for sampling
  58. #### Exceptions
  59. [GrammarFormatException](./llama.exceptions.grammarformatexception.md)<br>
  60. Thrown if input is malformed
  61. ### **ToString()**
  62. ```csharp
  63. public string ToString()
  64. ```
  65. #### Returns
  66. [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>