This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Embeddings: fail fast when embedding empty string (#53787)
The OpenAI API fails when given an input array with any empty strings: ``` ❯ curl https://api.openai.com/v1/embeddings \ -H "Content-Type: application/json" \ -H "Authorization: Bearer xxxxx" \ -d '{ "input": ["a", ""], "model": "text-embedding-ada-002" }' { "error": { "message": "'$.input' is invalid. Please check the API reference: https://platform.openai.com/docs/api-reference.", "type": "invalid_request_error", "param": null, "code": null } } ``` When using Cody Gateway, we have three layers of retries: 20 retries from ExternalDoer in Cody Gateway, 20 retries from ExternalDoer in `embeddings`, and 3 retries from `GetEmbeddingsWithRetry` for a total of _1200 retries_. Instead of doing that, this just lets us fail fast. We should separately fix the massive number of retries.
- Loading branch information
1 parent
7850383
commit d015b2b
Showing
6 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
enterprise/cmd/cody-gateway/internal/httpapi/embeddings/BUILD.bazel
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
19 changes: 19 additions & 0 deletions
19
enterprise/cmd/cody-gateway/internal/httpapi/embeddings/openai_test.go
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,19 @@ | ||
package embeddings | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/sourcegraph/sourcegraph/internal/codygateway" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestOpenAI(t *testing.T) { | ||
t.Run("errors on empty embedding string", func(t *testing.T) { | ||
client := NewOpenAIClient("") | ||
_, _, err := client.GenerateEmbeddings(context.Background(), codygateway.EmbeddingsRequest{ | ||
Input: []string{"a", ""}, // empty string is invalid | ||
}) | ||
require.ErrorContains(t, err, "empty string") | ||
}) | ||
} |
11 changes: 11 additions & 0 deletions
11
enterprise/internal/embeddings/embed/client/openai/BUILD.bazel
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
18 changes: 18 additions & 0 deletions
18
enterprise/internal/embeddings/embed/client/openai/client_test.go
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,18 @@ | ||
package openai | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/sourcegraph/sourcegraph/internal/conf/conftypes" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestOpenAI(t *testing.T) { | ||
t.Run("errors on empty embedding string", func(t *testing.T) { | ||
client := NewClient(&conftypes.EmbeddingsConfig{}) | ||
invalidTexts := []string{"a", ""} // empty string is invalid | ||
_, err := client.GetEmbeddingsWithRetries(context.Background(), invalidTexts, 10) | ||
require.ErrorContains(t, err, "empty string") | ||
}) | ||
} |