forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.go
171 lines (139 loc) · 4.64 KB
/
thread.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
package openai
import (
"context"
"net/http"
)
const (
threadsSuffix = "/threads"
)
type Thread struct {
ID string `json:"id"`
Object string `json:"object"`
CreatedAt int64 `json:"created_at"`
Metadata map[string]any `json:"metadata"`
ToolResources ToolResources `json:"tool_resources,omitempty"`
httpHeader
}
type ThreadRequest struct {
Messages []ThreadMessage `json:"messages,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
ToolResources *ToolResourcesRequest `json:"tool_resources,omitempty"`
}
type ToolResources struct {
CodeInterpreter *CodeInterpreterToolResources `json:"code_interpreter,omitempty"`
FileSearch *FileSearchToolResources `json:"file_search,omitempty"`
}
type CodeInterpreterToolResources struct {
FileIDs []string `json:"file_ids,omitempty"`
}
type FileSearchToolResources struct {
VectorStoreIDs []string `json:"vector_store_ids,omitempty"`
}
type ToolResourcesRequest struct {
CodeInterpreter *CodeInterpreterToolResourcesRequest `json:"code_interpreter,omitempty"`
FileSearch *FileSearchToolResourcesRequest `json:"file_search,omitempty"`
}
type CodeInterpreterToolResourcesRequest struct {
FileIDs []string `json:"file_ids,omitempty"`
}
type FileSearchToolResourcesRequest struct {
VectorStoreIDs []string `json:"vector_store_ids,omitempty"`
VectorStores []VectorStoreToolResources `json:"vector_stores,omitempty"`
}
type VectorStoreToolResources struct {
FileIDs []string `json:"file_ids,omitempty"`
ChunkingStrategy *ChunkingStrategy `json:"chunking_strategy,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
type ChunkingStrategy struct {
Type ChunkingStrategyType `json:"type"`
Static *StaticChunkingStrategy `json:"static,omitempty"`
}
type StaticChunkingStrategy struct {
MaxChunkSizeTokens int `json:"max_chunk_size_tokens"`
ChunkOverlapTokens int `json:"chunk_overlap_tokens"`
}
type ChunkingStrategyType string
const (
ChunkingStrategyTypeAuto ChunkingStrategyType = "auto"
ChunkingStrategyTypeStatic ChunkingStrategyType = "static"
)
type ModifyThreadRequest struct {
Metadata map[string]any `json:"metadata"`
ToolResources *ToolResources `json:"tool_resources,omitempty"`
}
type ThreadMessageRole string
const (
ThreadMessageRoleAssistant ThreadMessageRole = "assistant"
ThreadMessageRoleUser ThreadMessageRole = "user"
)
type ThreadMessage struct {
Role ThreadMessageRole `json:"role"`
Content string `json:"content"`
FileIDs []string `json:"file_ids,omitempty"`
Attachments []ThreadAttachment `json:"attachments,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
type ThreadAttachment struct {
FileID string `json:"file_id"`
Tools []ThreadAttachmentTool `json:"tools"`
}
type ThreadAttachmentTool struct {
Type string `json:"type"`
}
type ThreadDeleteResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Deleted bool `json:"deleted"`
httpHeader
}
// CreateThread creates a new thread.
func (c *Client) CreateThread(ctx context.Context, request ThreadRequest) (response Thread, err error) {
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(threadsSuffix), withBody(request),
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
err = c.sendRequest(req, &response)
return
}
// RetrieveThread retrieves a thread.
func (c *Client) RetrieveThread(ctx context.Context, threadID string) (response Thread, err error) {
urlSuffix := threadsSuffix + "/" + threadID
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
err = c.sendRequest(req, &response)
return
}
// ModifyThread modifies a thread.
func (c *Client) ModifyThread(
ctx context.Context,
threadID string,
request ModifyThreadRequest,
) (response Thread, err error) {
urlSuffix := threadsSuffix + "/" + threadID
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request),
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
err = c.sendRequest(req, &response)
return
}
// DeleteThread deletes a thread.
func (c *Client) DeleteThread(
ctx context.Context,
threadID string,
) (response ThreadDeleteResponse, err error) {
urlSuffix := threadsSuffix + "/" + threadID
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix),
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
err = c.sendRequest(req, &response)
return
}