Feat/messages api (#546)
* fix test server setup: - go map access is not deterministic - this can lead to a route: /foo/bar/1 matching /foo/bar before matching /foo/bar/1 if the map iteration go through /foo/bar first since the regex match wasn't bound to start and end anchors - registering handlers now converts * in routes to .* for proper regex matching - test server route handling now tries to fully match the handler route * add missing /v1 prefix to fine-tuning job cancel test server handler * add create message call * add messages list call * add get message call * add modify message call, fix return types for other message calls * add message file retrieve call * add list message files call * code style fixes * add test for list messages with pagination options * add beta header to msg calls now that #545 is merged * Update messages.go Co-authored-by: Simone Vellei <henomis@gmail.com> * Update messages.go Co-authored-by: Simone Vellei <henomis@gmail.com> * add missing object details for message, fix tests * fix merge formatting * minor style fixes --------- Co-authored-by: Simone Vellei <henomis@gmail.com>
This commit is contained in:
committed by
GitHub
parent
9fefd50e12
commit
b7cac703ac
178
messages.go
Normal file
178
messages.go
Normal file
@@ -0,0 +1,178 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
messagesSuffix = "messages"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
CreatedAt int `json:"created_at"`
|
||||
ThreadID string `json:"thread_id"`
|
||||
Role string `json:"role"`
|
||||
Content []MessageContent `json:"content"`
|
||||
FileIds []string `json:"file_ids"`
|
||||
AssistantID *string `json:"assistant_id,omitempty"`
|
||||
RunID *string `json:"run_id,omitempty"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
|
||||
httpHeader
|
||||
}
|
||||
|
||||
type MessagesList struct {
|
||||
Messages []Message `json:"data"`
|
||||
|
||||
httpHeader
|
||||
}
|
||||
|
||||
type MessageContent struct {
|
||||
Type string `json:"type"`
|
||||
Text *MessageText `json:"text,omitempty"`
|
||||
ImageFile *ImageFile `json:"image_file,omitempty"`
|
||||
}
|
||||
type MessageText struct {
|
||||
Value string `json:"value"`
|
||||
Annotations []any `json:"annotations"`
|
||||
}
|
||||
|
||||
type ImageFile struct {
|
||||
FileID string `json:"file_id"`
|
||||
}
|
||||
|
||||
type MessageRequest struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
FileIds []string `json:"file_ids,omitempty"`
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
type MessageFile struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
CreatedAt int `json:"created_at"`
|
||||
MessageID string `json:"message_id"`
|
||||
|
||||
httpHeader
|
||||
}
|
||||
|
||||
type MessageFilesList struct {
|
||||
MessageFiles []MessageFile `json:"data"`
|
||||
|
||||
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)
|
||||
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = c.sendRequest(req, &msg)
|
||||
return
|
||||
}
|
||||
|
||||
// ListMessage fetches all messages in the thread.
|
||||
func (c *Client) ListMessage(ctx context.Context, threadID string,
|
||||
limit *int,
|
||||
order *string,
|
||||
after *string,
|
||||
before *string,
|
||||
) (messages MessagesList, err error) {
|
||||
urlValues := url.Values{}
|
||||
if limit != nil {
|
||||
urlValues.Add("limit", fmt.Sprintf("%d", *limit))
|
||||
}
|
||||
if order != nil {
|
||||
urlValues.Add("order", *order)
|
||||
}
|
||||
if after != nil {
|
||||
urlValues.Add("after", *after)
|
||||
}
|
||||
if before != nil {
|
||||
urlValues.Add("before", *before)
|
||||
}
|
||||
encodedValues := ""
|
||||
if len(urlValues) > 0 {
|
||||
encodedValues = "?" + urlValues.Encode()
|
||||
}
|
||||
|
||||
urlSuffix := fmt.Sprintf("/threads/%s/%s%s", threadID, messagesSuffix, encodedValues)
|
||||
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantV1())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = c.sendRequest(req, &messages)
|
||||
return
|
||||
}
|
||||
|
||||
// RetrieveMessage retrieves a Message.
|
||||
func (c *Client) RetrieveMessage(
|
||||
ctx context.Context,
|
||||
threadID, messageID string,
|
||||
) (msg Message, err error) {
|
||||
urlSuffix := fmt.Sprintf("/threads/%s/%s/%s", threadID, messagesSuffix, messageID)
|
||||
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantV1())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = c.sendRequest(req, &msg)
|
||||
return
|
||||
}
|
||||
|
||||
// ModifyMessage modifies a message.
|
||||
func (c *Client) ModifyMessage(
|
||||
ctx context.Context,
|
||||
threadID, messageID string,
|
||||
metadata map[string]any,
|
||||
) (msg Message, err error) {
|
||||
urlSuffix := fmt.Sprintf("/threads/%s/%s/%s", threadID, messagesSuffix, messageID)
|
||||
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix),
|
||||
withBody(metadata), withBetaAssistantV1())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = c.sendRequest(req, &msg)
|
||||
return
|
||||
}
|
||||
|
||||
// RetrieveMessageFile fetches a message file.
|
||||
func (c *Client) RetrieveMessageFile(
|
||||
ctx context.Context,
|
||||
threadID, messageID, fileID string,
|
||||
) (file MessageFile, err error) {
|
||||
urlSuffix := fmt.Sprintf("/threads/%s/%s/%s/files/%s", threadID, messagesSuffix, messageID, fileID)
|
||||
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantV1())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = c.sendRequest(req, &file)
|
||||
return
|
||||
}
|
||||
|
||||
// ListMessageFiles fetches all files attached to a message.
|
||||
func (c *Client) ListMessageFiles(
|
||||
ctx context.Context,
|
||||
threadID, messageID string,
|
||||
) (files MessageFilesList, err error) {
|
||||
urlSuffix := fmt.Sprintf("/threads/%s/%s/%s/files", threadID, messagesSuffix, messageID)
|
||||
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantV1())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = c.sendRequest(req, &files)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user