forked from ollama/ollama
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
llm: do not silently fail for supplied, but invalid formats (ollama#8130
) Changes in ollama#8002 introduced fixes for bugs with mangling JSON Schemas. It also fixed a bug where the server would silently fail when clients requested invalid formats. It also, unfortunately, introduced a bug where the server would reject requests with an empty format, which should be allowed. The change in ollama#8127 updated the code to allow the empty format, but also reintroduced the regression where the server would silently fail when the format was set, but invalid. This commit fixes both regressions. The server does not reject the empty format, but it does reject invalid formats. It also adds tests to help us catch regressions in the future. Also, the updated code provides a more detailed error message when a client sends a non-empty, but invalid format, echoing the invalid format in the response. This commits also takes the opportunity to remove superfluous linter checks.
- Loading branch information
Showing
3 changed files
with
96 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package llm | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ollama/ollama/api" | ||
"golang.org/x/sync/semaphore" | ||
) | ||
|
||
func TestLLMServerCompletionFormat(t *testing.T) { | ||
// This test was written to fix an already deployed issue. It is a bit | ||
// of a mess, and but it's good enough, until we can refactoring the | ||
// Completion method to be more testable. | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
s := &llmServer{ | ||
sem: semaphore.NewWeighted(1), // required to prevent nil panic | ||
} | ||
|
||
checkInvalid := func(format string) { | ||
t.Helper() | ||
err := s.Completion(ctx, CompletionRequest{ | ||
Options: new(api.Options), | ||
Format: []byte(format), | ||
}, nil) | ||
|
||
want := fmt.Sprintf("invalid format: %q; expected \"json\" or a valid JSON Schema", format) | ||
if err == nil || !strings.Contains(err.Error(), want) { | ||
t.Fatalf("err = %v; want %q", err, want) | ||
} | ||
} | ||
|
||
checkInvalid("X") // invalid format | ||
checkInvalid(`"X"`) // invalid JSON Schema | ||
|
||
cancel() // prevent further processing if request makes it past the format check | ||
|
||
checkCanceled := 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"}`, ``, `""`} | ||
for _, valid := range valids { | ||
err := s.Completion(ctx, CompletionRequest{ | ||
Options: new(api.Options), | ||
Format: []byte(valid), | ||
}, nil) | ||
checkCanceled(err) | ||
} | ||
|
||
err := s.Completion(ctx, CompletionRequest{ | ||
Options: new(api.Options), | ||
Format: nil, // missing format | ||
}, nil) | ||
checkCanceled(err) | ||
} |