Skip to content

Commit

Permalink
Merge pull request #348 from tohrnii/gbnf-support
Browse files Browse the repository at this point in the history
add support for gbnf grammar
  • Loading branch information
tikikun authored Jan 16, 2024
2 parents 55743c3 + 7e34dbb commit 6dfb716
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ curl http://localhost:3928/v1/chat/completions \
}'
```

***OPTIONAL***: You can constrain the sampling using GBNF grammars by providing path to a grammar file
```bash title="Nitro Inference With Grammar"
curl http://localhost:3928/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "user",
"content": "Who won the world series in 2020?"
},
],
"grammar_file": "/path/to/grammarfile"
}'
```

Table of parameters

| Parameter | Type | Description |
Expand Down
10 changes: 9 additions & 1 deletion controllers/llamaCPP.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,15 @@ void llamaCPP::chatCompletion(
(*jsonBody).get("frequency_penalty", 0).asFloat();
data["presence_penalty"] = (*jsonBody).get("presence_penalty", 0).asFloat();
const Json::Value &messages = (*jsonBody)["messages"];

std::string grammar_file = (*jsonBody).get("grammar_file", "").asString();
std::ifstream file(grammar_file);
if (!file) {
LOG_ERROR << "Grammar file not found";
} else {
std::stringstream grammarBuf;
grammarBuf << file.rdbuf();
data["grammar"] = grammarBuf.str();
}
if (!llama.multimodal) {

for (const auto &message : messages) {
Expand Down
25 changes: 25 additions & 0 deletions examples/grammars/json.gbnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
root ::= object
value ::= object | array | string | number | ("true" | "false" | "null") ws

object ::=
"{" ws (
string ":" ws value
("," ws string ":" ws value)*
)? "}" ws

array ::=
"[" ws (
value
("," ws value)*
)? "]" ws

string ::=
"\"" (
[^"\\] |
"\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
)* "\"" ws

number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws

# Optional space: by convention, applied in this grammar after literal chars when allowed
ws ::= ([ \t\n] ws)?

0 comments on commit 6dfb716

Please sign in to comment.