Skip to content

Commit

Permalink
feat: Support Delete Message API (#799)
Browse files Browse the repository at this point in the history
* feat: Add DeleteMessage function to API client

* fix: linter

nolint : Deprecated method
split function: cognitive complexity 21

* rename func name for unit-test
  • Loading branch information
kappa-lab authored Aug 22, 2024
1 parent d86425a commit 6d02119
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
3 changes: 3 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "", "", "")
}},
Expand Down
2 changes: 1 addition & 1 deletion fine_tunes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
24 changes: 24 additions & 0 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
36 changes: 31 additions & 5 deletions messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 setupServerForTestMessage(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) {
Expand Down Expand Up @@ -115,6 +112,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)
}
Expand Down Expand Up @@ -176,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()

setupServerForTestMessage(t, server)
ctx := context.Background()

// static assertion of return type
Expand Down Expand Up @@ -225,6 +240,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)
Expand Down

0 comments on commit 6d02119

Please sign in to comment.