From a0022981c7ca41eabc333d7b241b63af6835a469 Mon Sep 17 00:00:00 2001 From: ParthSareen Date: Wed, 2 Apr 2025 16:58:22 -0700 Subject: [PATCH] server: improving grammar for JSON --- llm/server.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/llm/server.go b/llm/server.go index e6046db60..1568d5f8a 100644 --- a/llm/server.go +++ b/llm/server.go @@ -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)