You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 30, 2024. It is now read-only.
fix/LLM API: return helpful error message on missing model (#64474)
Previously, the `/.api/llm/chat/completions` API returned an error
message saying a model was unsupported if it used the wrong syntax. Now,
we only validate that the request is using the new modelref syntax and
otherwise delegate to the underlying
`/.api/completions/stream` endpoint to return the error message. This
error message at least mentions the default model, which I've found very
helpful when debugging why things are not working as expected.
```
unsupported chat model "openai::2024-02-01::gpt-4o" (default "anthropic::unknown::claude-3-sonnet-20240229"
```
<!-- PR description tips:
https://www.notion.so/sourcegraph/Write-a-good-pull-request-description-610a7fd3e613496eb76f450db5a49b6e
-->
## Test plan
See updated unit test.
Also manually tested locally
```
❯ curl 'https://sourcegraph.test:3443/.api/llm/chat/completions' \
-H 'Content-Type: application/json' \
-H "Authorization: token $HURL_token" \
--data-raw '{
"maxTokensToSample": 4000,
"messages": [
{
"role": "user",
"content": "Respond with \"no\" and nothing else."
}
],
"model": "openai::2024-02-01::gpt-4o",
"temperature": 0,
"topK": -1,
"topP": -1,
"stream": false
}'
failed to forward request to apiHandler: handler returned unexpected status code: got 400 want 200, response body: unsupported chat model "openai::2024-02-01::gpt-4o" (default "anthropic::unknown::claude-3-sonnet-20240229")
```
<!-- REQUIRED; info at
https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles
-->
## Changelog
<!-- OPTIONAL; info at
https://www.notion.so/sourcegraph/Writing-a-changelog-entry-dd997f411d524caabf0d8d38a24a878c
-->
returnfmt.Sprintf("requested model '%s' failed validation: %s. Expected format '${ProviderID}::${APIVersionID}::${ModelID}'. To fix this problem, send a request to `GET /.api/llm/models` to see the list of supported models.", chatCompletionRequest.Model, err)
97
85
}
98
-
didYouMean:=""
99
-
ifclosestModelRef!="" {
100
-
didYouMean=fmt.Sprintf(" (similar to %s)", closestModelRef)
101
-
}
102
-
returnfmt.Sprintf("model %s is not supported%s", chatCompletionRequest.Model, didYouMean)
// For now, we reject requests when the model is not using the new ModelRef format.
58
58
assert.Equal(t, http.StatusBadRequest, rr.Code)
59
59
60
-
// Assert that we give a helpful error message nudging the user to use modelref instead of the old syntax.
61
-
assert.Equal(t, "model anthropic/claude-3-haiku-20240307 is not supported (similar to anthropic::unknown::claude-3-haiku-20240307)\n", rr.Body.String())
60
+
assert.Equal(t, "requested model 'anthropic/claude-3-haiku-20240307' failed validation: modelRef syntax error. Expected format '${ProviderID}::${APIVersionID}::${ModelID}'. To fix this problem, send a request to `GET /.api/llm/models` to see the list of supported models.\n", rr.Body.String())
61
+
})
62
+
63
+
t.Run("/.api/llm/chat/completions (400 model is invalid model)", func(t*testing.T) {
// For now, we reject requests when the model is not using the new ModelRef format.
69
+
assert.Equal(t, http.StatusInternalServerError, rr.Code) // Should be 400 Bad Request, see CODY-3318
70
+
assert.Equal(t, "failed to forward request to apiHandler: handler returned unexpected status code: got 400 want 200, response body: the requested chat model is not available (\"anthropic::unknown::claude-gpt\", onProTier=true)\n", rr.Body.String())
0 commit comments