Skip to content

Commit 3063e67

Browse files
authored
Feat Implement assistants API (#535)
* chore: implement assistants API * fix * fix * chore: add tests * fix tests * fix linting
1 parent 6d9c3a6 commit 3063e67

File tree

3 files changed

+489
-0
lines changed

3 files changed

+489
-0
lines changed

assistant.go

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
package openai
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
)
9+
10+
const (
11+
assistantsSuffix = "/assistants"
12+
assistantsFilesSuffix = "/files"
13+
)
14+
15+
type Assistant struct {
16+
ID string `json:"id"`
17+
Object string `json:"object"`
18+
CreatedAt int64 `json:"created_at"`
19+
Name *string `json:"name,omitempty"`
20+
Description *string `json:"description,omitempty"`
21+
Model string `json:"model"`
22+
Instructions *string `json:"instructions,omitempty"`
23+
Tools []any `json:"tools,omitempty"`
24+
25+
httpHeader
26+
}
27+
28+
type AssistantTool struct {
29+
Type string `json:"type"`
30+
}
31+
32+
type AssistantToolCodeInterpreter struct {
33+
AssistantTool
34+
}
35+
36+
type AssistantToolRetrieval struct {
37+
AssistantTool
38+
}
39+
40+
type AssistantToolFunction struct {
41+
AssistantTool
42+
Function FunctionDefinition `json:"function"`
43+
}
44+
45+
type AssistantRequest struct {
46+
Model string `json:"model"`
47+
Name *string `json:"name,omitempty"`
48+
Description *string `json:"description,omitempty"`
49+
Instructions *string `json:"instructions,omitempty"`
50+
Tools []any `json:"tools,omitempty"`
51+
FileIDs []string `json:"file_ids,omitempty"`
52+
Metadata map[string]any `json:"metadata,omitempty"`
53+
}
54+
55+
// AssistantsList is a list of assistants.
56+
type AssistantsList struct {
57+
Assistants []Assistant `json:"data"`
58+
59+
httpHeader
60+
}
61+
62+
type AssistantFile struct {
63+
ID string `json:"id"`
64+
Object string `json:"object"`
65+
CreatedAt int64 `json:"created_at"`
66+
AssistantID string `json:"assistant_id"`
67+
68+
httpHeader
69+
}
70+
71+
type AssistantFileRequest struct {
72+
FileID string `json:"file_id"`
73+
}
74+
75+
type AssistantFilesList struct {
76+
AssistantFiles []AssistantFile `json:"data"`
77+
78+
httpHeader
79+
}
80+
81+
// CreateAssistant creates a new assistant.
82+
func (c *Client) CreateAssistant(ctx context.Context, request AssistantRequest) (response Assistant, err error) {
83+
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(assistantsSuffix), withBody(request))
84+
if err != nil {
85+
return
86+
}
87+
88+
err = c.sendRequest(req, &response)
89+
return
90+
}
91+
92+
// RetrieveAssistant retrieves an assistant.
93+
func (c *Client) RetrieveAssistant(
94+
ctx context.Context,
95+
assistantID string,
96+
) (response Assistant, err error) {
97+
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
98+
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
99+
if err != nil {
100+
return
101+
}
102+
103+
err = c.sendRequest(req, &response)
104+
return
105+
}
106+
107+
// ModifyAssistant modifies an assistant.
108+
func (c *Client) ModifyAssistant(
109+
ctx context.Context,
110+
assistantID string,
111+
request AssistantRequest,
112+
) (response Assistant, err error) {
113+
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
114+
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request))
115+
if err != nil {
116+
return
117+
}
118+
119+
err = c.sendRequest(req, &response)
120+
return
121+
}
122+
123+
// DeleteAssistant deletes an assistant.
124+
func (c *Client) DeleteAssistant(
125+
ctx context.Context,
126+
assistantID string,
127+
) (response Assistant, err error) {
128+
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
129+
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix))
130+
if err != nil {
131+
return
132+
}
133+
134+
err = c.sendRequest(req, &response)
135+
return
136+
}
137+
138+
// ListAssistants Lists the currently available assistants.
139+
func (c *Client) ListAssistants(
140+
ctx context.Context,
141+
limit *int,
142+
order *string,
143+
after *string,
144+
before *string,
145+
) (reponse AssistantsList, err error) {
146+
urlValues := url.Values{}
147+
if limit != nil {
148+
urlValues.Add("limit", fmt.Sprintf("%d", *limit))
149+
}
150+
if order != nil {
151+
urlValues.Add("order", *order)
152+
}
153+
if after != nil {
154+
urlValues.Add("after", *after)
155+
}
156+
if before != nil {
157+
urlValues.Add("before", *before)
158+
}
159+
160+
encodedValues := ""
161+
if len(urlValues) > 0 {
162+
encodedValues = "?" + urlValues.Encode()
163+
}
164+
165+
urlSuffix := fmt.Sprintf("%s%s", assistantsSuffix, encodedValues)
166+
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
167+
if err != nil {
168+
return
169+
}
170+
171+
err = c.sendRequest(req, &reponse)
172+
return
173+
}
174+
175+
// CreateAssistantFile creates a new assistant file.
176+
func (c *Client) CreateAssistantFile(
177+
ctx context.Context,
178+
assistantID string,
179+
request AssistantFileRequest,
180+
) (response AssistantFile, err error) {
181+
urlSuffix := fmt.Sprintf("%s/%s%s", assistantsSuffix, assistantID, assistantsFilesSuffix)
182+
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix),
183+
withBody(request))
184+
if err != nil {
185+
return
186+
}
187+
188+
err = c.sendRequest(req, &response)
189+
return
190+
}
191+
192+
// RetrieveAssistantFile retrieves an assistant file.
193+
func (c *Client) RetrieveAssistantFile(
194+
ctx context.Context,
195+
assistantID string,
196+
fileID string,
197+
) (response AssistantFile, err error) {
198+
urlSuffix := fmt.Sprintf("%s/%s%s/%s", assistantsSuffix, assistantID, assistantsFilesSuffix, fileID)
199+
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
200+
if err != nil {
201+
return
202+
}
203+
204+
err = c.sendRequest(req, &response)
205+
return
206+
}
207+
208+
// DeleteAssistantFile deletes an existing file.
209+
func (c *Client) DeleteAssistantFile(
210+
ctx context.Context,
211+
assistantID string,
212+
fileID string,
213+
) (err error) {
214+
urlSuffix := fmt.Sprintf("%s/%s%s/%s", assistantsSuffix, assistantID, assistantsFilesSuffix, fileID)
215+
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix))
216+
if err != nil {
217+
return
218+
}
219+
220+
err = c.sendRequest(req, nil)
221+
return
222+
}
223+
224+
// ListAssistantFiles Lists the currently available files for an assistant.
225+
func (c *Client) ListAssistantFiles(
226+
ctx context.Context,
227+
assistantID string,
228+
limit *int,
229+
order *string,
230+
after *string,
231+
before *string,
232+
) (response AssistantFilesList, err error) {
233+
urlValues := url.Values{}
234+
if limit != nil {
235+
urlValues.Add("limit", fmt.Sprintf("%d", *limit))
236+
}
237+
if order != nil {
238+
urlValues.Add("order", *order)
239+
}
240+
if after != nil {
241+
urlValues.Add("after", *after)
242+
}
243+
if before != nil {
244+
urlValues.Add("before", *before)
245+
}
246+
247+
encodedValues := ""
248+
if len(urlValues) > 0 {
249+
encodedValues = "?" + urlValues.Encode()
250+
}
251+
252+
urlSuffix := fmt.Sprintf("%s/%s%s%s", assistantsSuffix, assistantID, assistantsFilesSuffix, encodedValues)
253+
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
254+
if err != nil {
255+
return
256+
}
257+
258+
err = c.sendRequest(req, &response)
259+
return
260+
}

0 commit comments

Comments
 (0)