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
|
package assistant
import (
"bytes"
"context"
"encoding/json"
"net/http"
"within.website/x/web"
)
type Content struct {
Type string `json:"type"`
Text Text `json:"text"`
}
type Text struct {
Value string `json:"value"`
Annotations json.RawMessage `json:"annotations"`
}
type Message struct {
ID string `json:"id"`
Object string `json:"object"`
CreatedAt int64 `json:"created_at"`
ThreadID string `json:"thread_id"`
Role string `json:"role"`
Content []Content `json:"content"`
FileIDs []string `json:"file_ids"`
AssistantID *string `json:"assistant_id,omitempty"`
RunID *string `json:"run_id,omitempty"`
Metadata map[string]string `json:"metadata"`
}
type CreateMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
func (c *Client) CreateMessage(ctx context.Context, tid string, cm CreateMessage) (*Message, error) {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(cm); err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://api.openai.com/v1/threads/"+tid+"/messages", buf)
if err != nil {
return nil, err
}
resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, web.NewError(http.StatusOK, resp)
}
var m Message
if err := json.NewDecoder(resp.Body).Decode(&m); err != nil {
return nil, err
}
return &m, nil
}
func (c *Client) GetMessage(ctx context.Context, tid, mid string) (*Message, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.openai.com/v1/threads/"+tid+"/messages/"+mid, nil)
if err != nil {
return nil, err
}
resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, web.NewError(http.StatusOK, resp)
}
var m Message
if err := json.NewDecoder(resp.Body).Decode(&m); err != nil {
return nil, err
}
return &m, nil
}
func (c *Client) ListMessages(ctx context.Context, tid string) ([]Message, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.openai.com/v1/threads/"+tid+"/messages", nil)
if err != nil {
return nil, err
}
resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, web.NewError(http.StatusOK, resp)
}
type response struct {
Data []Message `json:"data"`
}
var result response
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return result.Data, nil
}
|