From d6c77397f65cec595b0ffca5c28ec5bf9786c731 Mon Sep 17 00:00:00 2001 From: Adam Shannon Date: Wed, 7 Aug 2024 20:51:28 -0500 Subject: [PATCH] fix: support ThreadMessage.Content as an array FileIDs on ThreadMessage isn't supported anymore, but Content can be an array. Fixes: https://github.com/sashabaranov/go-openai/issues/773 --- run_test.go | 10 ++++++++-- thread.go | 20 +++++++++++++++----- thread_test.go | 20 ++++++++++++++++---- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/run_test.go b/run_test.go index cdf99db05..3aa513cca 100644 --- a/run_test.go +++ b/run_test.go @@ -204,6 +204,7 @@ func TestRun(t *testing.T) { _, err = client.CancelRun(ctx, threadID, runID) checks.NoError(t, err, "CancelRun error") + prompt := "Hello, World!" _, err = client.CreateThreadAndRun(ctx, openai.CreateThreadAndRunRequest{ RunRequest: openai.RunRequest{ AssistantID: assistantID, @@ -211,8 +212,13 @@ func TestRun(t *testing.T) { Thread: openai.ThreadRequest{ Messages: []openai.ThreadMessage{ { - Role: openai.ThreadMessageRoleUser, - Content: "Hello, World!", + Role: openai.ThreadMessageRoleUser, + Content: []openai.ThreadMessageContent{ + { + Type: "text", + Text: prompt, + }, + }, }, }, }, diff --git a/thread.go b/thread.go index bc08e2bcb..e8ddf8983 100644 --- a/thread.go +++ b/thread.go @@ -88,11 +88,21 @@ const ( ) type ThreadMessage struct { - Role ThreadMessageRole `json:"role"` - Content string `json:"content"` - FileIDs []string `json:"file_ids,omitempty"` - Attachments []ThreadAttachment `json:"attachments,omitempty"` - Metadata map[string]any `json:"metadata,omitempty"` + Role ThreadMessageRole `json:"role"` + Content []ThreadMessageContent `json:"content"` + Attachments []ThreadAttachment `json:"attachments,omitempty"` + Metadata map[string]any `json:"metadata,omitempty"` +} + +type ThreadMessageContent struct { + Type string `json:"type"` + ImageFile *ThreadMessageImageFile `json:"image_file,omitempty"` + Text string `json:"text,omitempty"` +} + +type ThreadMessageImageFile struct { + FileID string `json:"file_id"` + Detail *string `json:"detail,omitempty"` } type ThreadAttachment struct { diff --git a/thread_test.go b/thread_test.go index 1ac0f3c0e..28dc9fa16 100644 --- a/thread_test.go +++ b/thread_test.go @@ -70,11 +70,17 @@ func TestThread(t *testing.T) { ctx := context.Background() + prompt := "Hello, World!" _, err := client.CreateThread(ctx, openai.ThreadRequest{ Messages: []openai.ThreadMessage{ { - Role: openai.ThreadMessageRoleUser, - Content: "Hello, World!", + Role: openai.ThreadMessageRoleUser, + Content: []openai.ThreadMessageContent{ + { + Type: "text", + Text: prompt, + }, + }, }, }, }) @@ -153,11 +159,17 @@ func TestAzureThread(t *testing.T) { ctx := context.Background() + prompt := "Hello, World!" _, err := client.CreateThread(ctx, openai.ThreadRequest{ Messages: []openai.ThreadMessage{ { - Role: openai.ThreadMessageRoleUser, - Content: "Hello, World!", + Role: openai.ThreadMessageRoleUser, + Content: []openai.ThreadMessageContent{ + { + Type: "text", + Text: prompt, + }, + }, }, }, })