-
Notifications
You must be signed in to change notification settings - Fork 2
/
threads.go
151 lines (115 loc) · 3.69 KB
/
threads.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
package assistants
import (
"context"
"fmt"
"net/http"
)
// ThreadObject represents a thread that contains messages.
type ThreadObject struct {
ID string `json:"id"`
Object string `json:"object"`
CreatedAt int `json:"created_at"`
Metadata map[string]string `json:"metadata"`
}
// CreateThreadParams represents parameters for creating a thread.
type CreateThreadParams struct {
Messages []Message `json:"messages"`
Metadata map[string]string `json:"metadata"`
}
// AssembleThreadURL constructs the URL for retrieving, modifying, or deleting a specific thread.
func AssembleThreadURL(threadID string) string {
return getRequestURL(fmt.Sprintf("threads/%s", threadID))
}
// AssembleThreadsURL constructs the URL for listing or creating threads.
func AssembleThreadsURL() string {
return getRequestURL("threads")
}
// CreateThread creates a new thread with the provided messages.
func (c *Client) CreateThread(ctx context.Context, messages []Message) (*ThreadObject, error) {
if len(messages) == 0 {
return nil, fmt.Errorf("messages must be a non-empty array")
}
for _, message := range messages {
if message.Role == "" || len(message.Content) == 0 {
return nil, fmt.Errorf("each message must have a valid role and non-empty content")
}
for _, content := range message.Content {
if content.Type == "" || (content.Text != nil && content.Text.Value == "") {
return nil, fmt.Errorf("each content within a message must have a type and non-empty value if type is text")
}
// TODO: Add similar checks for other types like ImageFile, FileCitation, FilePath as needed
}
}
body := map[string]interface{}{"messages": messages}
fullURL := AssembleThreadsURL()
var result ThreadObject
err := c.sendHTTPRequest(ctx, http.MethodPost, fullURL, body, &result, assistantsPostHeaders)
if err != nil {
return nil, err
}
return &result, nil
}
/*
Error: No Thread Found w/ ID 404
Data
{
"error": {
"message": "No thread found with id '{thread_id}'.",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
*/
// RetrieveThread retrieves an existing thread by its ID.
func (c *Client) RetrieveThread(ctx context.Context, threadId string) (*ThreadObject, error) {
if threadId == "" {
return nil, fmt.Errorf("threadId must be a valid string")
}
fullURL := AssembleThreadURL(threadId)
var result ThreadObject
err := c.sendHTTPRequest(ctx, http.MethodGet, fullURL, nil, &result, assistantsBaseHeaders)
if err != nil {
return nil, err
}
return &result, nil
}
/*
Error: No Thread Found w/ ID 404
Data
{
"error": {
"message": "No thread found with id '{thread_id}'.",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
*/
// ModifyThread updates the metadata of a thread.
func (c *Client) ModifyThread(ctx context.Context, threadId string, metadata map[string]string) (*ThreadObject, error) {
if threadId == "" {
return nil, fmt.Errorf("threadId must be a valid string")
}
body := map[string]interface{}{"metadata": metadata}
fullURL := AssembleThreadURL(threadId)
var result ThreadObject
err := c.sendHTTPRequest(ctx, http.MethodPost, fullURL, body, &result, assistantsPostHeaders)
if err != nil {
return nil, err
}
return &result, nil
}
// DeleteThread deletes a thread by its ID.
func (c *Client) DeleteThread(ctx context.Context, threadId string) (*ThreadObject, error) {
if threadId == "" {
return nil, fmt.Errorf("threadId must be a valid string")
}
fullURL := AssembleThreadURL(threadId)
var result ThreadObject
err := c.sendHTTPRequest(ctx, http.MethodDelete, fullURL, nil, &result, assistantsBaseHeaders)
if err != nil {
return nil, err
}
return &result, nil
}