Skip to content

Commit 6d02119

Browse files
authored
feat: Support Delete Message API (sashabaranov#799)
* feat: Add DeleteMessage function to API client * fix: linter nolint : Deprecated method split function: cognitive complexity 21 * rename func name for unit-test
1 parent d86425a commit 6d02119

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

Diff for: client_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ func TestClientReturnsRequestBuilderErrors(t *testing.T) {
348348
{"ModifyMessage", func() (any, error) {
349349
return client.ModifyMessage(ctx, "", "", nil)
350350
}},
351+
{"DeleteMessage", func() (any, error) {
352+
return client.DeleteMessage(ctx, "", "")
353+
}},
351354
{"RetrieveMessageFile", func() (any, error) {
352355
return client.RetrieveMessageFile(ctx, "", "", "")
353356
}},

Diff for: fine_tunes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (c *Client) CreateFineTune(ctx context.Context, request FineTuneRequest) (r
115115
// This API will be officially deprecated on January 4th, 2024.
116116
// OpenAI recommends to migrate to the new fine tuning API implemented in fine_tuning_job.go.
117117
func (c *Client) CancelFineTune(ctx context.Context, fineTuneID string) (response FineTune, err error) {
118-
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/fine-tunes/"+fineTuneID+"/cancel"))
118+
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/fine-tunes/"+fineTuneID+"/cancel")) //nolint:lll //this method is deprecated
119119
if err != nil {
120120
return
121121
}

Diff for: messages.go

+24
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ type MessageFilesList struct {
7373
httpHeader
7474
}
7575

76+
type MessageDeletionStatus struct {
77+
ID string `json:"id"`
78+
Object string `json:"object"`
79+
Deleted bool `json:"deleted"`
80+
81+
httpHeader
82+
}
83+
7684
// CreateMessage creates a new message.
7785
func (c *Client) CreateMessage(ctx context.Context, threadID string, request MessageRequest) (msg Message, err error) {
7886
urlSuffix := fmt.Sprintf("/threads/%s/%s", threadID, messagesSuffix)
@@ -186,3 +194,19 @@ func (c *Client) ListMessageFiles(
186194
err = c.sendRequest(req, &files)
187195
return
188196
}
197+
198+
// DeleteMessage deletes a message..
199+
func (c *Client) DeleteMessage(
200+
ctx context.Context,
201+
threadID, messageID string,
202+
) (status MessageDeletionStatus, err error) {
203+
urlSuffix := fmt.Sprintf("/threads/%s/%s/%s", threadID, messagesSuffix, messageID)
204+
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix),
205+
withBetaAssistantVersion(c.config.AssistantVersion))
206+
if err != nil {
207+
return
208+
}
209+
210+
err = c.sendRequest(req, &status)
211+
return
212+
}

Diff for: messages_test.go

+31-5
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@ import (
88
"testing"
99

1010
"github.com/sashabaranov/go-openai"
11+
"github.com/sashabaranov/go-openai/internal/test"
1112
"github.com/sashabaranov/go-openai/internal/test/checks"
1213
)
1314

1415
var emptyStr = ""
1516

16-
// TestMessages Tests the messages endpoint of the API using the mocked server.
17-
func TestMessages(t *testing.T) {
17+
func setupServerForTestMessage(t *testing.T, server *test.ServerTest) {
1818
threadID := "thread_abc123"
1919
messageID := "msg_abc123"
2020
fileID := "file_abc123"
2121

22-
client, server, teardown := setupOpenAITestServer()
23-
defer teardown()
24-
2522
server.RegisterHandler(
2623
"/v1/threads/"+threadID+"/messages/"+messageID+"/files/"+fileID,
2724
func(w http.ResponseWriter, r *http.Request) {
@@ -115,6 +112,13 @@ func TestMessages(t *testing.T) {
115112
Metadata: nil,
116113
})
117114
fmt.Fprintln(w, string(resBytes))
115+
case http.MethodDelete:
116+
resBytes, _ := json.Marshal(openai.MessageDeletionStatus{
117+
ID: messageID,
118+
Object: "thread.message.deleted",
119+
Deleted: true,
120+
})
121+
fmt.Fprintln(w, string(resBytes))
118122
default:
119123
t.Fatalf("unsupported messages http method: %s", r.Method)
120124
}
@@ -176,7 +180,18 @@ func TestMessages(t *testing.T) {
176180
}
177181
},
178182
)
183+
}
179184

185+
// TestMessages Tests the messages endpoint of the API using the mocked server.
186+
func TestMessages(t *testing.T) {
187+
threadID := "thread_abc123"
188+
messageID := "msg_abc123"
189+
fileID := "file_abc123"
190+
191+
client, server, teardown := setupOpenAITestServer()
192+
defer teardown()
193+
194+
setupServerForTestMessage(t, server)
180195
ctx := context.Background()
181196

182197
// static assertion of return type
@@ -225,6 +240,17 @@ func TestMessages(t *testing.T) {
225240
t.Fatalf("expected message metadata to get modified")
226241
}
227242

243+
msgDel, err := client.DeleteMessage(ctx, threadID, messageID)
244+
checks.NoError(t, err, "DeleteMessage error")
245+
if msgDel.ID != messageID {
246+
t.Fatalf("unexpected message id: '%s'", msg.ID)
247+
}
248+
if !msgDel.Deleted {
249+
t.Fatalf("expected deleted is true")
250+
}
251+
_, err = client.DeleteMessage(ctx, threadID, "not_exist_id")
252+
checks.HasError(t, err, "DeleteMessage error")
253+
228254
// message files
229255
var msgFile openai.MessageFile
230256
msgFile, err = client.RetrieveMessageFile(ctx, threadID, messageID, fileID)

0 commit comments

Comments
 (0)