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
`llama.cpp` can also convert JSON schemas to grammars either ahead of time or at each request, see below.
98
+
97
99
## Troubleshooting
98
100
99
101
Grammars currently have performance gotchas (see https://github.com/ggerganov/llama.cpp/issues/4218).
@@ -103,3 +105,40 @@ Grammars currently have performance gotchas (see https://github.com/ggerganov/ll
103
105
A common pattern is to allow repetitions of a pattern `x` up to N times.
104
106
105
107
While semantically correct, the syntax `x? x? x?.... x?` (with N repetitions) may result in extremely slow sampling. Instead, you can write `x{0,N}` (or `(x (x (x ... (x)?...)?)?)?` w/ N-deep nesting in earlier llama.cpp versions).
108
+
109
+
## Using GBNF grammars
110
+
111
+
You can use GBNF grammars:
112
+
113
+
- In the [server](../examples/server)'s completion endpoints, passed as the `grammar` body field
114
+
- In the [main](../examples/main) CLI, passed as the `--grammar` & `--grammar-file` flags
115
+
- With the [gbnf-validator](../examples/gbnf-validator) tool, to test them against strings.
116
+
117
+
## JSON Schemas → GBNF
118
+
119
+
`llama.cpp` supports converting a subset of https://json-schema.org/ to GBNF grammars:
120
+
121
+
- In the [server](../examples/server):
122
+
- For any completion endpoints, passed as the `json_schema` body field
123
+
- For the `/chat/completions` endpoint, passed inside the `result_format` body field (e.g. `{"type", "json_object", "schema": {"items": {}}}`)
124
+
- In the [main](../examples/main) CLI, passed as the `--json` / `-j` flag
125
+
- To convert to a grammar ahead of time:
126
+
- in CLI, with [json_schema_to_grammar.py](../examples/json_schema_to_grammar.py)
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
+
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
+
Here is also a non-exhaustive list of **unsupported** features:
132
+
133
+
-`additionalProperties`: to be fixed in https://github.com/ggerganov/llama.cpp/pull/7840
0 commit comments