From 668a11834a91b5fb5045ea7253dd7ccefd807a7e Mon Sep 17 00:00:00 2001 From: gamiken Date: Fri, 19 Jul 2024 15:34:16 +0900 Subject: [PATCH 1/3] feat: Add DeleteMessage function to API client --- client_test.go | 3 +++ messages.go | 24 ++++++++++++++++++++++++ messages_test.go | 18 ++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/client_test.go b/client_test.go index e49da9b3..fe2f1e9f 100644 --- a/client_test.go +++ b/client_test.go @@ -348,6 +348,9 @@ func TestClientReturnsRequestBuilderErrors(t *testing.T) { {"ModifyMessage", func() (any, error) { return client.ModifyMessage(ctx, "", "", nil) }}, + {"DeleteMessage", func() (any, error) { + return client.DeleteMessage(ctx, "", "") + }}, {"RetrieveMessageFile", func() (any, error) { return client.RetrieveMessageFile(ctx, "", "", "") }}, diff --git a/messages.go b/messages.go index 6af11844..1fddd631 100644 --- a/messages.go +++ b/messages.go @@ -73,6 +73,14 @@ type MessageFilesList struct { httpHeader } +type MessageDeletionStatus struct { + ID string `json:"id"` + Object string `json:"object"` + Deleted bool `json:"deleted"` + + httpHeader +} + // CreateMessage creates a new message. func (c *Client) CreateMessage(ctx context.Context, threadID string, request MessageRequest) (msg Message, err error) { urlSuffix := fmt.Sprintf("/threads/%s/%s", threadID, messagesSuffix) @@ -186,3 +194,19 @@ func (c *Client) ListMessageFiles( err = c.sendRequest(req, &files) return } + +// DeleteMessage deletes a message.. +func (c *Client) DeleteMessage( + ctx context.Context, + threadID, messageID string, +) (status MessageDeletionStatus, err error) { + urlSuffix := fmt.Sprintf("/threads/%s/%s/%s", threadID, messagesSuffix, messageID) + req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix), + withBetaAssistantVersion(c.config.AssistantVersion)) + if err != nil { + return + } + + err = c.sendRequest(req, &status) + return +} diff --git a/messages_test.go b/messages_test.go index a18be20b..ce10f28f 100644 --- a/messages_test.go +++ b/messages_test.go @@ -115,6 +115,13 @@ func TestMessages(t *testing.T) { Metadata: nil, }) fmt.Fprintln(w, string(resBytes)) + case http.MethodDelete: + resBytes, _ := json.Marshal(openai.MessageDeletionStatus{ + ID: messageID, + Object: "thread.message.deleted", + Deleted: true, + }) + fmt.Fprintln(w, string(resBytes)) default: t.Fatalf("unsupported messages http method: %s", r.Method) } @@ -225,6 +232,17 @@ func TestMessages(t *testing.T) { t.Fatalf("expected message metadata to get modified") } + msgDel, err := client.DeleteMessage(ctx, threadID, messageID) + checks.NoError(t, err, "DeleteMessage error") + if msgDel.ID != messageID { + t.Fatalf("unexpected message id: '%s'", msg.ID) + } + if !msgDel.Deleted { + t.Fatalf("expected deleted is true") + } + _, err = client.DeleteMessage(ctx, threadID, "not_exist_id") + checks.HasError(t, err, "DeleteMessage error") + // message files var msgFile openai.MessageFile msgFile, err = client.RetrieveMessageFile(ctx, threadID, messageID, fileID) From 5404aaab0ceff256d6a03e474fd65c7aecb77487 Mon Sep 17 00:00:00 2001 From: gamiken Date: Fri, 19 Jul 2024 16:46:00 +0900 Subject: [PATCH 2/3] fix: linter nolint : Deprecated method split function: cognitive complexity 21 --- fine_tunes.go | 2 +- messages_test.go | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/fine_tunes.go b/fine_tunes.go index ca840781..74b47bf3 100644 --- a/fine_tunes.go +++ b/fine_tunes.go @@ -115,7 +115,7 @@ func (c *Client) CreateFineTune(ctx context.Context, request FineTuneRequest) (r // This API will be officially deprecated on January 4th, 2024. // OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go. func (c *Client) CancelFineTune(ctx context.Context, fineTuneID string) (response FineTune, err error) { - req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/fine-tunes/"+fineTuneID+"/cancel")) + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/fine-tunes/"+fineTuneID+"/cancel")) //nolint:lll //this method is deprecated if err != nil { return } diff --git a/messages_test.go b/messages_test.go index ce10f28f..fb135e22 100644 --- a/messages_test.go +++ b/messages_test.go @@ -8,20 +8,17 @@ import ( "testing" "github.com/sashabaranov/go-openai" + "github.com/sashabaranov/go-openai/internal/test" "github.com/sashabaranov/go-openai/internal/test/checks" ) var emptyStr = "" -// TestMessages Tests the messages endpoint of the API using the mocked server. -func TestMessages(t *testing.T) { +func registerServer(t *testing.T, server *test.ServerTest) { threadID := "thread_abc123" messageID := "msg_abc123" fileID := "file_abc123" - client, server, teardown := setupOpenAITestServer() - defer teardown() - server.RegisterHandler( "/v1/threads/"+threadID+"/messages/"+messageID+"/files/"+fileID, func(w http.ResponseWriter, r *http.Request) { @@ -183,7 +180,18 @@ func TestMessages(t *testing.T) { } }, ) +} + +// TestMessages Tests the messages endpoint of the API using the mocked server. +func TestMessages(t *testing.T) { + threadID := "thread_abc123" + messageID := "msg_abc123" + fileID := "file_abc123" + + client, server, teardown := setupOpenAITestServer() + defer teardown() + registerServer(t, server) ctx := context.Background() // static assertion of return type From 8facd6e65b6b08056aa32b20db0b5b7e38fbd932 Mon Sep 17 00:00:00 2001 From: gamiken Date: Thu, 22 Aug 2024 10:56:46 +0900 Subject: [PATCH 3/3] rename func name for unit-test --- messages_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/messages_test.go b/messages_test.go index fb135e22..71ceb4d3 100644 --- a/messages_test.go +++ b/messages_test.go @@ -14,7 +14,7 @@ import ( var emptyStr = "" -func registerServer(t *testing.T, server *test.ServerTest) { +func setupServerForTestMessage(t *testing.T, server *test.ServerTest) { threadID := "thread_abc123" messageID := "msg_abc123" fileID := "file_abc123" @@ -191,7 +191,7 @@ func TestMessages(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown() - registerServer(t, server) + setupServerForTestMessage(t, server) ctx := context.Background() // static assertion of return type