This repository was archived by the owner on Jul 4, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +49
-1
lines changed
Expand file tree Collapse file tree 3 files changed +49
-1
lines changed Original file line number Diff line number Diff 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+
95110Table of parameters
96111
97112| Parameter | Type | Description |
Original file line number Diff line number Diff 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) {
Original file line number Diff line number Diff line change 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)?
You can’t perform that action at this time.
0 commit comments