|
| 1 | +--- |
| 2 | +outline: deep |
| 3 | +--- |
1 | 4 | # Using Grammar
|
2 | 5 | Use this to enforce a model to generate response in a specific format of text, like `JSON` for example.
|
3 | 6 |
|
@@ -69,11 +72,11 @@ console.log(JSON.parse(a2));
|
69 | 72 | The [`llama.createGrammarForJsonSchema(...)`](../api/classes/Llama.md#creategrammarforjsonschema) creates a [`LlamaJsonSchemaGrammar`](../api/classes/LlamaJsonSchemaGrammar)
|
70 | 73 | from a GBNF grammar generated a based on the [JSON schema](https://json-schema.org/learn/getting-started-step-by-step) you provide.
|
71 | 74 |
|
72 |
| -It only supports [a small subset of the JSON schema spec](../api/type-aliases/GbnfJsonSchema.md), |
| 75 | +It only supports [a subset of the JSON schema spec](../api/type-aliases/GbnfJsonSchema.md), |
73 | 76 | but it's enough to generate useful JSON objects using a text generation model.
|
74 | 77 |
|
75 |
| -Many features of [JSON schema spec](https://json-schema.org/learn/getting-started-step-by-step) are not supported here on purpose, |
76 |
| -as those features don't align well with the way models generate text and are prone to [hallucinations](https://en.wikipedia.org/wiki/Hallucination_(artificial_intelligence)). |
| 78 | +Some features of [JSON schema spec](https://json-schema.org/learn/getting-started-step-by-step) are not supported on purpose, |
| 79 | +as those features don't align well with the way models generate text, and are too prone to [hallucinations](https://en.wikipedia.org/wiki/Hallucination_(artificial_intelligence)). |
77 | 80 | Workarounds for the missing features that you can implement with the supported set of features often lead to improved generation quality.
|
78 | 81 |
|
79 | 82 | To see what subset of the JSON schema spec is supported, see the [`GbnfJsonSchema` type](../api/type-aliases/GbnfJsonSchema.md) and follow its sub-types.
|
@@ -134,6 +137,41 @@ console.log(
|
134 | 137 | );
|
135 | 138 | ```
|
136 | 139 |
|
| 140 | +### Reducing Hallucinations When Using JSON Schema Grammar {#reducing-json-schema-hallucinations} |
| 141 | +When forcing a model to follow a specific JSON schema in its response, the model isn't aware of the entire schema being enforced on it. |
| 142 | +To avoid hallucinations, you need to inform the model in some way what are your expectations from its response. |
| 143 | + |
| 144 | +To do that, you can: |
| 145 | +* Explain to the model what you expect in the prompt itself. |
| 146 | + <br /> |
| 147 | + You can do that by giving a brief explanation of what you expect, |
| 148 | + or by dumping the entire JSON schema in the prompt (which can eat up a lot of tokens, thus is not recommended). |
| 149 | +* Force the model to output self-explanatory keys as part of its response, so it can then generate values for those keys. |
| 150 | +* Use a combination of both. |
| 151 | + |
| 152 | +The technique used in [the above example](#json-schema) forces the model to output the given keys, and then lets the model generate the values for those keys: |
| 153 | +1. The model is forced to generate the text `{"positiveWordsInUserMessage": [`, and then we let it finish the syntax of the JSON array with only strings. |
| 154 | +2. When it finishes the array, we force it to <br />generate the text <span>`, "userMessagePositivityScoreFromOneToTen": `</span>, and then we let it generate a number. |
| 155 | +3. Finally, we force it to generate the text `, "nameOfUser": `, and then we let it generate either a string or `null`. |
| 156 | + |
| 157 | +This technique allows us to get the desired result without explaining to the model what we want in advance. |
| 158 | +While this method works great in this example, it may not work as well in other cases that need some explanation. |
| 159 | + |
| 160 | +For example, let's say we force the model to generate an array with at least 2 items and at most 5 items; |
| 161 | +if we don't provide any prior explanation for this requirement (either by using a self-explanatory key name or in the prompt), |
| 162 | +then the model won't be able to "plan" the entire content of the array in advance, |
| 163 | +which can lead it to generate inconsistent and unevenly spread items. |
| 164 | +It can also make the model repeat the existing value in different forms or make up wrong values, |
| 165 | +just so it can follow the enforced schema. |
| 166 | + |
| 167 | +The key takeaway is that to reduce hallucinations and achieve great results when using a JSON schema grammar, |
| 168 | +you need to ensure you inform the model of your expectations in some way. |
| 169 | + |
| 170 | +::: tip NOTE |
| 171 | +When using [function calling](./function-calling.md), the model is always aware of the entire schema being enforced on it, |
| 172 | +so there's no need to explain the schema in the prompt. |
| 173 | +::: |
| 174 | + |
137 | 175 | ## Creating Your Own Grammar {#custom-grammar}
|
138 | 176 | To create your own grammar, read the [GBNF guide](https://github.com/ggerganov/llama.cpp/blob/f5fe98d11bdf9e7797bcfb05c0c3601ffc4b9d26/grammars/README.md) to create a GBNF grammar file.
|
139 | 177 |
|
|
0 commit comments