forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
183 lines (156 loc) · 4.56 KB
/
messages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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"`
Object string `json:"object"`
FirstID *string `json:"first_id"`
LastID *string `json:"last_id"`
HasMore bool `json:"has_more"`
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), withBetaAssistantV1())
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
}