server: improving grammar for JSON

This commit is contained in:
ParthSareen 2025-04-02 16:58:22 -07:00
parent a77d19454e
commit a0022981c7

View File

@ -636,13 +636,13 @@ func (s *llmServer) WaitUntilRunning(ctx context.Context) error {
}
var grammarJSON = `
root ::= object
value ::= object | array | string | number | ("true" | "false" | "null") ws
root ::= s object
value ::= ws (object | array | string | number | ("true" | "false" | "null")) ws
object ::=
"{" ws (
string ":" ws value
("," ws string ":" ws value)*
)? "}" ws
)? "}"
array ::=
"[" ws (
value
@ -651,12 +651,12 @@ array ::=
string ::=
"\"" (
[^"\\\x7F\x00-\x1F] |
"\\" (["\\/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)?
`
"\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F])
)* "\""
number ::= "-"? ("0" | [1-9] [0-9]*) ("." [0-9]+)? ([eE] [-+]? [0-9]+)?
ws ::= [ \t \n \r]*
s ::= [ \n \t]
t ::= [ \t \r]*`
const maxBufferSize = 512 * format.KiloByte
@ -694,6 +694,8 @@ func (s *llmServer) Completion(ctx context.Context, req CompletionRequest, fn fu
break
case `"json"`:
req.Grammar = grammarJSON
slog.Info("using JSON grammar")
slog.Info(req.Grammar)
default:
if req.Format[0] != '{' {
return fmt.Errorf("invalid format: %q; expected \"json\" or a valid JSON Schema object", req.Format)