Unverified Commit 2ddc32d5 authored by Blake Mizerany's avatar Blake Mizerany Committed by GitHub
Browse files

llm: do not error on "null" format (#8139)

This fixes another regression in the previous commit that fixed other
known bugs.
parent 2cde4b88
...@@ -700,20 +700,24 @@ func (s *llmServer) Completion(ctx context.Context, req CompletionRequest, fn fu ...@@ -700,20 +700,24 @@ func (s *llmServer) Completion(ctx context.Context, req CompletionRequest, fn fu
} }
if len(req.Format) > 0 { if len(req.Format) > 0 {
switch { switch string(req.Format) {
case bytes.Equal(req.Format, []byte(`""`)): case `null`, `""`:
// fallthrough // Field was set, but "missing" a value. We accept
case bytes.Equal(req.Format, []byte(`"json"`)): // these as "not set".
break
case `"json"`:
request["grammar"] = grammarJSON request["grammar"] = grammarJSON
case bytes.HasPrefix(req.Format, []byte("{")): default:
if req.Format[0] != '{' {
return fmt.Errorf("invalid format: %q; expected \"json\" or a valid JSON Schema object", req.Format)
}
// User provided a JSON schema // User provided a JSON schema
g := llama.SchemaToGrammar(req.Format) g := llama.SchemaToGrammar(req.Format)
if g == nil { if g == nil {
return fmt.Errorf("invalid JSON schema in format") return fmt.Errorf("invalid JSON schema in format")
} }
request["grammar"] = string(g) request["grammar"] = string(g)
default:
return fmt.Errorf("invalid format: %q; expected \"json\" or a valid JSON Schema", req.Format)
} }
} }
......
...@@ -39,25 +39,34 @@ func TestLLMServerCompletionFormat(t *testing.T) { ...@@ -39,25 +39,34 @@ func TestLLMServerCompletionFormat(t *testing.T) {
cancel() // prevent further processing if request makes it past the format check cancel() // prevent further processing if request makes it past the format check
checkCanceled := func(err error) { checkValid := func(err error) {
t.Helper() t.Helper()
if !errors.Is(err, context.Canceled) { if !errors.Is(err, context.Canceled) {
t.Fatalf("Completion: err = %v; expected context.Canceled", err) t.Fatalf("Completion: err = %v; expected context.Canceled", err)
} }
} }
valids := []string{`"json"`, `{"type":"object"}`, ``, `""`} valids := []string{
// "missing"
``,
`""`,
`null`,
// JSON
`"json"`,
`{"type":"object"}`,
}
for _, valid := range valids { for _, valid := range valids {
err := s.Completion(ctx, CompletionRequest{ err := s.Completion(ctx, CompletionRequest{
Options: new(api.Options), Options: new(api.Options),
Format: []byte(valid), Format: []byte(valid),
}, nil) }, nil)
checkCanceled(err) checkValid(err)
} }
err := s.Completion(ctx, CompletionRequest{ err := s.Completion(ctx, CompletionRequest{
Options: new(api.Options), Options: new(api.Options),
Format: nil, // missing format Format: nil, // missing format
}, nil) }, nil)
checkCanceled(err) checkValid(err)
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment