Skip to content

Commit a2d2bf6

Browse files
authored
Fix Refactor assistant api (#545)
* fix: refactor assistant API * fix * trigger build * fix: use AssistantDeleteResponse
1 parent a0159ad commit a2d2bf6

File tree

2 files changed

+58
-38
lines changed

2 files changed

+58
-38
lines changed

assistant.go

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,43 @@ import (
1010
const (
1111
assistantsSuffix = "/assistants"
1212
assistantsFilesSuffix = "/files"
13+
openaiAssistantsV1 = "assistants=v1"
1314
)
1415

1516
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"`
17+
ID string `json:"id"`
18+
Object string `json:"object"`
19+
CreatedAt int64 `json:"created_at"`
20+
Name *string `json:"name,omitempty"`
21+
Description *string `json:"description,omitempty"`
22+
Model string `json:"model"`
23+
Instructions *string `json:"instructions,omitempty"`
24+
Tools []AssistantTool `json:"tools,omitempty"`
2425

2526
httpHeader
2627
}
2728

28-
type AssistantTool struct {
29-
Type string `json:"type"`
30-
}
31-
32-
type AssistantToolCodeInterpreter struct {
33-
AssistantTool
34-
}
29+
type AssistantToolType string
3530

36-
type AssistantToolRetrieval struct {
37-
AssistantTool
38-
}
31+
const (
32+
AssistantToolTypeCodeInterpreter AssistantToolType = "code_interpreter"
33+
AssistantToolTypeRetrieval AssistantToolType = "retrieval"
34+
AssistantToolTypeFunction AssistantToolType = "function"
35+
)
3936

40-
type AssistantToolFunction struct {
41-
AssistantTool
42-
Function FunctionDefinition `json:"function"`
37+
type AssistantTool struct {
38+
Type AssistantToolType `json:"type"`
39+
Function *FunctionDefinition `json:"function,omitempty"`
4340
}
4441

4542
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"`
43+
Model string `json:"model"`
44+
Name *string `json:"name,omitempty"`
45+
Description *string `json:"description,omitempty"`
46+
Instructions *string `json:"instructions,omitempty"`
47+
Tools []AssistantTool `json:"tools,omitempty"`
48+
FileIDs []string `json:"file_ids,omitempty"`
49+
Metadata map[string]any `json:"metadata,omitempty"`
5350
}
5451

5552
// AssistantsList is a list of assistants.
@@ -59,6 +56,14 @@ type AssistantsList struct {
5956
httpHeader
6057
}
6158

59+
type AssistantDeleteResponse struct {
60+
ID string `json:"id"`
61+
Object string `json:"object"`
62+
Deleted bool `json:"deleted"`
63+
64+
httpHeader
65+
}
66+
6267
type AssistantFile struct {
6368
ID string `json:"id"`
6469
Object string `json:"object"`
@@ -80,7 +85,8 @@ type AssistantFilesList struct {
8085

8186
// CreateAssistant creates a new assistant.
8287
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))
88+
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(assistantsSuffix), withBody(request),
89+
withBetaAssistantV1())
8490
if err != nil {
8591
return
8692
}
@@ -95,7 +101,8 @@ func (c *Client) RetrieveAssistant(
95101
assistantID string,
96102
) (response Assistant, err error) {
97103
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
98-
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
104+
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
105+
withBetaAssistantV1())
99106
if err != nil {
100107
return
101108
}
@@ -111,7 +118,8 @@ func (c *Client) ModifyAssistant(
111118
request AssistantRequest,
112119
) (response Assistant, err error) {
113120
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
114-
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request))
121+
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request),
122+
withBetaAssistantV1())
115123
if err != nil {
116124
return
117125
}
@@ -124,9 +132,10 @@ func (c *Client) ModifyAssistant(
124132
func (c *Client) DeleteAssistant(
125133
ctx context.Context,
126134
assistantID string,
127-
) (response Assistant, err error) {
135+
) (response AssistantDeleteResponse, err error) {
128136
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
129-
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix))
137+
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix),
138+
withBetaAssistantV1())
130139
if err != nil {
131140
return
132141
}
@@ -163,7 +172,8 @@ func (c *Client) ListAssistants(
163172
}
164173

165174
urlSuffix := fmt.Sprintf("%s%s", assistantsSuffix, encodedValues)
166-
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
175+
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
176+
withBetaAssistantV1())
167177
if err != nil {
168178
return
169179
}
@@ -180,7 +190,8 @@ func (c *Client) CreateAssistantFile(
180190
) (response AssistantFile, err error) {
181191
urlSuffix := fmt.Sprintf("%s/%s%s", assistantsSuffix, assistantID, assistantsFilesSuffix)
182192
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix),
183-
withBody(request))
193+
withBody(request),
194+
withBetaAssistantV1())
184195
if err != nil {
185196
return
186197
}
@@ -196,7 +207,8 @@ func (c *Client) RetrieveAssistantFile(
196207
fileID string,
197208
) (response AssistantFile, err error) {
198209
urlSuffix := fmt.Sprintf("%s/%s%s/%s", assistantsSuffix, assistantID, assistantsFilesSuffix, fileID)
199-
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
210+
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
211+
withBetaAssistantV1())
200212
if err != nil {
201213
return
202214
}
@@ -212,7 +224,8 @@ func (c *Client) DeleteAssistantFile(
212224
fileID string,
213225
) (err error) {
214226
urlSuffix := fmt.Sprintf("%s/%s%s/%s", assistantsSuffix, assistantID, assistantsFilesSuffix, fileID)
215-
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix))
227+
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix),
228+
withBetaAssistantV1())
216229
if err != nil {
217230
return
218231
}
@@ -250,7 +263,8 @@ func (c *Client) ListAssistantFiles(
250263
}
251264

252265
urlSuffix := fmt.Sprintf("%s/%s%s%s", assistantsSuffix, assistantID, assistantsFilesSuffix, encodedValues)
253-
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
266+
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
267+
withBetaAssistantV1())
254268
if err != nil {
255269
return
256270
}

client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ func withContentType(contentType string) requestOption {
8383
}
8484
}
8585

86+
func withBetaAssistantV1() requestOption {
87+
return func(args *requestOptions) {
88+
args.header.Set("OpenAI-Beta", "assistants=v1")
89+
}
90+
}
91+
8692
func (c *Client) newRequest(ctx context.Context, method, url string, setters ...requestOption) (*http.Request, error) {
8793
// Default Options
8894
args := &requestOptions{

0 commit comments

Comments
 (0)