feat: Support Delete Message API (#799)
* feat: Add DeleteMessage function to API client * fix: linter nolint : Deprecated method split function: cognitive complexity 21 * rename func name for unit-test
This commit is contained in:
committed by
GitHub
parent
d86425a5cf
commit
6d021190f0
@@ -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, "", "", "")
|
||||
}},
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
24
messages.go
24
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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user