Browse Source
llm: do not error on "null" format (#8139)
This fixes another regression in the previous commit that fixed other
known bugs.
jmorganca/vendor-081b29bd
v0.5.4
Blake Mizerany
2 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with
24 additions and
11 deletions
-
llm/server.go
-
llm/server_test.go
|
|
|
@ -700,20 +700,24 @@ func (s *llmServer) Completion(ctx context.Context, req CompletionRequest, fn fu |
|
|
|
} |
|
|
|
|
|
|
|
if len(req.Format) > 0 { |
|
|
|
switch { |
|
|
|
case bytes.Equal(req.Format, []byte(`""`)): |
|
|
|
// fallthrough
|
|
|
|
case bytes.Equal(req.Format, []byte(`"json"`)): |
|
|
|
switch string(req.Format) { |
|
|
|
case `null`, `""`: |
|
|
|
// Field was set, but "missing" a value. We accept
|
|
|
|
// these as "not set".
|
|
|
|
break |
|
|
|
case `"json"`: |
|
|
|
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
|
|
|
|
g := llama.SchemaToGrammar(req.Format) |
|
|
|
if g == nil { |
|
|
|
return fmt.Errorf("invalid JSON schema in format") |
|
|
|
} |
|
|
|
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) { |
|
|
|
|
|
|
|
cancel() // prevent further processing if request makes it past the format check
|
|
|
|
|
|
|
|
checkCanceled := func(err error) { |
|
|
|
checkValid := func(err error) { |
|
|
|
t.Helper() |
|
|
|
if !errors.Is(err, context.Canceled) { |
|
|
|
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 { |
|
|
|
err := s.Completion(ctx, CompletionRequest{ |
|
|
|
Options: new(api.Options), |
|
|
|
Format: []byte(valid), |
|
|
|
}, nil) |
|
|
|
checkCanceled(err) |
|
|
|
checkValid(err) |
|
|
|
} |
|
|
|
|
|
|
|
err := s.Completion(ctx, CompletionRequest{ |
|
|
|
Options: new(api.Options), |
|
|
|
Format: nil, // missing format
|
|
|
|
}, nil) |
|
|
|
checkCanceled(err) |
|
|
|
checkValid(err) |
|
|
|
} |
|
|
|
|