You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- in CLI, with [examples/json_schema_to_grammar.py](../examples/json_schema_to_grammar.py)
127
127
- in JavaScript with [json-schema-to-grammar.mjs](../examples/server/public/json-schema-to-grammar.mjs) (this is used by the [server](../examples/server)'s Web UI)
128
128
129
-
Take a look at [tests](../../tests/test-json-schema-to-grammar.cpp) to see which features are likely supported (you'll also find usage examples in https://github.com/ggerganov/llama.cpp/pull/5978, https://github.com/ggerganov/llama.cpp/pull/6659 & https://github.com/ggerganov/llama.cpp/pull/6555).
129
+
Take a look at [tests](../tests/test-json-schema-to-grammar.cpp) to see which features are likely supported (you'll also find usage examples in https://github.com/ggerganov/llama.cpp/pull/5978, https://github.com/ggerganov/llama.cpp/pull/6659 & https://github.com/ggerganov/llama.cpp/pull/6555).
130
+
131
+
```bash
132
+
llama-cli \
133
+
-hfr bartowski/Phi-3-medium-128k-instruct-GGUF \
134
+
-hff Phi-3-medium-128k-instruct-Q8_0.gguf \
135
+
-j '{
136
+
"type": "array",
137
+
"items": {
138
+
"type": "object",
139
+
"properties": {
140
+
"name": {
141
+
"type": "string",
142
+
"minLength": 1,
143
+
"maxLength": 100
144
+
},
145
+
"age": {
146
+
"type": "integer",
147
+
"minimum": 0,
148
+
"maximum": 150
149
+
}
150
+
},
151
+
"required": ["name", "age"],
152
+
"additionalProperties": false
153
+
},
154
+
"minItems": 10,
155
+
"maxItems": 100
156
+
}' \
157
+
-p 'Generate a {name, age}[] JSON array with famous actors of all ages.'
item-age-kv ::= "\"age\"" space ":" space item-age
175
+
item-name ::= "\"" char{1,100} "\"" space
176
+
item-name-kv ::= "\"name\"" space ":" space item-name
177
+
root ::= "[" space item ("," space item){9,99} "]" space
178
+
space ::= | " " | "\n" [ \t]{0,20}
179
+
```
180
+
181
+
</details>
182
+
183
+
Here is also a list of known limitations (contributions welcome):
184
+
185
+
- Unsupported features are skipped silently. It is currently advised to use the command-line Python converter (see above) to see any warnings, and to inspect the resulting grammar / test it w/ [llama-gbnf-validator](../examples/gbnf-validator/gbnf-validator.cpp).
186
+
- Can't mix `properties` w/ `anyOf` / `oneOf` in the same type (https://github.com/ggerganov/llama.cpp/issues/7703)
187
+
-[prefixItems](https://json-schema.org/draft/2020-12/json-schema-core#name-prefixitems) is broken (but [items](https://json-schema.org/draft/2020-12/json-schema-core#name-items) works)
188
+
-`minimum`, `exclusiveMinimum`, `maximum`, `exclusiveMaximum`: only supported for `"type": "integer"` for now, not `number`
189
+
- Nested `$ref`s are broken (https://github.com/ggerganov/llama.cpp/issues/8073)
190
+
-[pattern](https://json-schema.org/draft/2020-12/json-schema-validation#name-pattern)s must start with `^` and end with `$`
191
+
- Remote `$ref`s not supported in the C++ version (Python & JavaScript versions fetch https refs)
> By default, `object`s accept [additional properties](https://json-schema.org/understanding-json-schema/reference/object#additionalproperties), which you might not want / not expect, and which will make sampling slower (not just because of the extra tokens, but also generates a slower grammar).
207
+
> You can set `"additionalProperties": false` on the schema of any object to ensure only properties listed in `properties` are generated (not needed for non-`object` types, e.g. `array` or `string`).
208
+
209
+
If you're using [Pydantic](https://pydantic.dev/) to generate schemas, you can disable additional properties with the `extra` config on each model class:
210
+
211
+
```python
212
+
# pip install pydantic
213
+
import json
214
+
from typing import Annotated, List
215
+
from pydantic import BaseModel, Extra, Field
216
+
classQAPair(BaseModel):
217
+
classConfig:
218
+
extra ='forbid'# triggers additionalProperties: false in the JSON schema
key-facts ::= "[" space (key-facts-item ("," space key-facts-item)*)? "]" space
302
+
key-facts-item ::= "\"" "- " key-facts-item-1{5,} "\"" space
303
+
key-facts-item-1 ::= dot
304
+
key-facts-kv ::= "\"key_facts\"" space ":" space key-facts
305
+
question-answers ::= "[" space (question-answers-item ("," space question-answers-item)*)? "]" space
306
+
question-answers-item ::= "[" space question-answers-item-item ("," space question-answers-item-item){4,} "]" space
307
+
question-answers-item-item ::= QAPair
308
+
question-answers-kv ::= "\"question_answers\"" space ":" space question-answers
309
+
root ::= "{" space key-facts-kv "," space question-answers-kv "}" space
310
+
space ::= | " " | "\n" [ \t]{0,20}
311
+
string ::= "\"" char* "\"" space
312
+
```
313
+
314
+
</details>
315
+
316
+
If you're using [Zod](https://zod.dev/), you can make your objects explicitly strict w/ `z.object(...).strict()` or `z.strictObject(...)`.
317
+
318
+
Note however that [zod-to-json-schema](https://github.com/StefanTerdell/zod-to-json-schema) currently always seems to set `"additionalProperties": false` anyway (even w/ zod schemas on which `nonstrict()` / `passthrough()` was called).
0 commit comments