Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content-type fix #659

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ func (c *Client) newRequest(ctx context.Context, method, url string, setters ...
}

func (c *Client) sendRequest(req *http.Request, v Response) error {
req.Header.Set("Accept", "application/json; charset=utf-8")
req.Header.Set("Accept", "application/json")

// Check whether Content-Type is already set, Upload Files API requires
// Content-Type == multipart/form-data
contentType := req.Header.Get("Content-Type")
if contentType == "" {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Content-Type", "application/json")
}

res, err := c.config.HTTPClient.Do(req)
Expand Down
2 changes: 1 addition & 1 deletion embeddings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestAzureEmbeddingEndpoint(t *testing.T) {

server.RegisterHandler(
"/openai/deployments/text-embedding-ada-002/embeddings",
func(w http.ResponseWriter, r *http.Request) {
func(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(openai.EmbeddingResponse{Data: sampleEmbeddings})
fmt.Fprintln(w, string(resBytes))
},
Expand Down
10 changes: 5 additions & 5 deletions files_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ func handleCreateFile(w http.ResponseWriter, r *http.Request) {
func TestDeleteFile(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/files/deadbeef", func(w http.ResponseWriter, r *http.Request) {})
server.RegisterHandler("/v1/files/deadbeef", func(http.ResponseWriter, *http.Request) {})
err := client.DeleteFile(context.Background(), "deadbeef")
checks.NoError(t, err, "DeleteFile error")
}

func TestListFile(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/files", func(w http.ResponseWriter, r *http.Request) {
server.RegisterHandler("/v1/files", func(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(openai.FilesList{})
fmt.Fprintln(w, string(resBytes))
})
Expand All @@ -105,7 +105,7 @@ func TestListFile(t *testing.T) {
func TestGetFile(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/files/deadbeef", func(w http.ResponseWriter, r *http.Request) {
server.RegisterHandler("/v1/files/deadbeef", func(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(openai.File{})
fmt.Fprintln(w, string(resBytes))
})
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestGetFileContentReturnError(t *testing.T) {
}`
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/files/deadbeef/content", func(w http.ResponseWriter, r *http.Request) {
server.RegisterHandler("/v1/files/deadbeef/content", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, wantErrorResp)
})
Expand All @@ -178,7 +178,7 @@ func TestGetFileContentReturnError(t *testing.T) {
func TestGetFileContentReturnTimeoutError(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/files/deadbeef/content", func(w http.ResponseWriter, r *http.Request) {
server.RegisterHandler("/v1/files/deadbeef/content", func(http.ResponseWriter, *http.Request) {
time.Sleep(10 * time.Nanosecond)
})
ctx := context.Background()
Expand Down
10 changes: 5 additions & 5 deletions image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestImageFormBuilderFailures(t *testing.T) {
_, err := client.CreateEditImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateImage should return error if form builder fails")

mockBuilder.mockCreateFormFile = func(name string, file *os.File) error {
mockBuilder.mockCreateFormFile = func(name string, _ *os.File) error {
if name == "mask" {
return mockFailedErr
}
Expand All @@ -69,12 +69,12 @@ func TestImageFormBuilderFailures(t *testing.T) {
_, err = client.CreateEditImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateImage should return error if form builder fails")

mockBuilder.mockCreateFormFile = func(name string, file *os.File) error {
mockBuilder.mockCreateFormFile = func(string, *os.File) error {
return nil
}

var failForField string
mockBuilder.mockWriteField = func(fieldname, value string) error {
mockBuilder.mockWriteField = func(fieldname, _ string) error {
if fieldname == failForField {
return mockFailedErr
}
Expand Down Expand Up @@ -125,12 +125,12 @@ func TestVariImageFormBuilderFailures(t *testing.T) {
_, err := client.CreateVariImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateVariImage should return error if form builder fails")

mockBuilder.mockCreateFormFile = func(name string, file *os.File) error {
mockBuilder.mockCreateFormFile = func(string, *os.File) error {
return nil
}

var failForField string
mockBuilder.mockWriteField = func(fieldname, value string) error {
mockBuilder.mockWriteField = func(fieldname, _ string) error {
if fieldname == failForField {
return mockFailedErr
}
Expand Down
4 changes: 2 additions & 2 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Message struct {
ThreadID string `json:"thread_id"`
Role string `json:"role"`
Content []MessageContent `json:"content"`
FileIds []string `json:"file_ids"`
FileIds []string `json:"file_ids"` //nolint:revive //backwards-compatibility
AssistantID *string `json:"assistant_id,omitempty"`
RunID *string `json:"run_id,omitempty"`
Metadata map[string]any `json:"metadata"`
Expand Down Expand Up @@ -54,7 +54,7 @@ type ImageFile struct {
type MessageRequest struct {
Role string `json:"role"`
Content string `json:"content"`
FileIds []string `json:"file_ids,omitempty"`
FileIds []string `json:"file_ids,omitempty"` //nolint:revive // backwards-compatibility
Metadata map[string]any `json:"metadata,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func handleGetModelEndpoint(w http.ResponseWriter, _ *http.Request) {
func TestGetModelReturnTimeoutError(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/models/text-davinci-003", func(w http.ResponseWriter, r *http.Request) {
server.RegisterHandler("/v1/models/text-davinci-003", func(http.ResponseWriter, *http.Request) {
time.Sleep(10 * time.Nanosecond)
})
ctx := context.Background()
Expand Down
2 changes: 1 addition & 1 deletion run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Run struct {
Model string `json:"model"`
Instructions string `json:"instructions,omitempty"`
Tools []Tool `json:"tools"`
FileIDS []string `json:"file_ids"`
FileIDS []string `json:"file_ids"` //nolint:revive // backwards-compatibility
Metadata map[string]any `json:"metadata"`
Usage Usage `json:"usage,omitempty"`

Expand Down
2 changes: 1 addition & 1 deletion speech.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (c *Client) CreateSpeech(ctx context.Context, request CreateSpeechRequest)
}
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/audio/speech", string(request.Model)),
withBody(request),
withContentType("application/json; charset=utf-8"),
withContentType("application/json"),
)
if err != nil {
return
Expand Down
14 changes: 7 additions & 7 deletions stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestCompletionsStreamWrongModel(t *testing.T) {
func TestCreateCompletionStream(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, r *http.Request) {
server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")

// Send test responses
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestCreateCompletionStream(t *testing.T) {
func TestCreateCompletionStreamError(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, r *http.Request) {
server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")

// Send test responses
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestCreateCompletionStreamError(t *testing.T) {
func TestCreateCompletionStreamRateLimitError(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, r *http.Request) {
server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(429)

Expand Down Expand Up @@ -182,7 +182,7 @@ func TestCreateCompletionStreamRateLimitError(t *testing.T) {
func TestCreateCompletionStreamTooManyEmptyStreamMessagesError(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, r *http.Request) {
server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")

// Send test responses
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestCreateCompletionStreamTooManyEmptyStreamMessagesError(t *testing.T) {
func TestCreateCompletionStreamUnexpectedTerminatedError(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, r *http.Request) {
server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")

// Send test responses
Expand Down Expand Up @@ -263,7 +263,7 @@ func TestCreateCompletionStreamUnexpectedTerminatedError(t *testing.T) {
func TestCreateCompletionStreamBrokenJSONError(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, r *http.Request) {
server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")

// Send test responses
Expand Down Expand Up @@ -305,7 +305,7 @@ func TestCreateCompletionStreamBrokenJSONError(t *testing.T) {
func TestCreateCompletionStreamReturnTimeoutError(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/completions", func(w http.ResponseWriter, r *http.Request) {
server.RegisterHandler("/v1/completions", func(http.ResponseWriter, *http.Request) {
time.Sleep(10 * time.Nanosecond)
})
ctx := context.Background()
Expand Down
Loading