From e6f476c361b618f3981d437a6ae6d3ba24e7454d Mon Sep 17 00:00:00 2001 From: Phil Scott Date: Thu, 13 Feb 2025 14:05:54 -0500 Subject: [PATCH] Updates json gbnf to match llama.cpp example Example had a tendency to start just outputting whitespace until it ran out of tokens without a few of the constraints they added around whitespace generation --- LLama.Examples/Assets/json.gbnf | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/LLama.Examples/Assets/json.gbnf b/LLama.Examples/Assets/json.gbnf index a01c4efd7..413b3af6c 100644 --- a/LLama.Examples/Assets/json.gbnf +++ b/LLama.Examples/Assets/json.gbnf @@ -1,5 +1,3 @@ -# https://github.com/ggerganov/llama.cpp/blob/8183159cf3def112f6d1fe94815fce70e1bffa12/grammars/json.gbnf - root ::= object value ::= object | array | string | number | ("true" | "false" | "null") ws @@ -17,11 +15,11 @@ array ::= string ::= "\"" ( - [^"\\] | - "\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes + [^"\\\x7F\x00-\x1F] | + "\\" (["\\bfnrt] | "u" [0-9a-fA-F]{4}) # escapes )* "\"" ws -number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws +number ::= ("-"? ([0-9] | [1-9] [0-9]{0,15})) ("." [0-9]+)? ([eE] [-+]? [0-9] [1-9]{0,15})? ws # Optional space: by convention, applied in this grammar after literal chars when allowed -ws ::= ([ \t\n] ws)? \ No newline at end of file +ws ::= | " " | "\n" [ \t]{0,20} \ No newline at end of file