Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 6dfb716

Browse files
authored
Merge pull request #348 from tohrnii/gbnf-support
add support for gbnf grammar
2 parents 55743c3 + 7e34dbb commit 6dfb716

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ curl http://localhost:3928/v1/chat/completions \
9292
}'
9393
```
9494

95+
***OPTIONAL***: You can constrain the sampling using GBNF grammars by providing path to a grammar file
96+
```bash title="Nitro Inference With Grammar"
97+
curl http://localhost:3928/v1/chat/completions \
98+
-H "Content-Type: application/json" \
99+
-d '{
100+
"messages": [
101+
{
102+
"role": "user",
103+
"content": "Who won the world series in 2020?"
104+
},
105+
],
106+
"grammar_file": "/path/to/grammarfile"
107+
}'
108+
```
109+
95110
Table of parameters
96111

97112
| Parameter | Type | Description |

controllers/llamaCPP.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,15 @@ void llamaCPP::chatCompletion(
194194
(*jsonBody).get("frequency_penalty", 0).asFloat();
195195
data["presence_penalty"] = (*jsonBody).get("presence_penalty", 0).asFloat();
196196
const Json::Value &messages = (*jsonBody)["messages"];
197-
197+
std::string grammar_file = (*jsonBody).get("grammar_file", "").asString();
198+
std::ifstream file(grammar_file);
199+
if (!file) {
200+
LOG_ERROR << "Grammar file not found";
201+
} else {
202+
std::stringstream grammarBuf;
203+
grammarBuf << file.rdbuf();
204+
data["grammar"] = grammarBuf.str();
205+
}
198206
if (!llama.multimodal) {
199207

200208
for (const auto &message : messages) {

examples/grammars/json.gbnf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
root ::= object
2+
value ::= object | array | string | number | ("true" | "false" | "null") ws
3+
4+
object ::=
5+
"{" ws (
6+
string ":" ws value
7+
("," ws string ":" ws value)*
8+
)? "}" ws
9+
10+
array ::=
11+
"[" ws (
12+
value
13+
("," ws value)*
14+
)? "]" ws
15+
16+
string ::=
17+
"\"" (
18+
[^"\\] |
19+
"\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
20+
)* "\"" ws
21+
22+
number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws
23+
24+
# Optional space: by convention, applied in this grammar after literal chars when allowed
25+
ws ::= ([ \t\n] ws)?

0 commit comments

Comments
 (0)