|
| 1 | +package openai |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | +) |
| 7 | + |
| 8 | +const ( |
| 9 | + threadsSuffix = "/threads" |
| 10 | +) |
| 11 | + |
| 12 | +type Thread struct { |
| 13 | + ID string `json:"id"` |
| 14 | + Object string `json:"object"` |
| 15 | + CreatedAt int64 `json:"created_at"` |
| 16 | + Metadata map[string]any `json:"metadata"` |
| 17 | + |
| 18 | + httpHeader |
| 19 | +} |
| 20 | + |
| 21 | +type ThreadRequest struct { |
| 22 | + Messages []ThreadMessage `json:"messages,omitempty"` |
| 23 | + Metadata map[string]any `json:"metadata,omitempty"` |
| 24 | +} |
| 25 | + |
| 26 | +type ModifyThreadRequest struct { |
| 27 | + Metadata map[string]any `json:"metadata"` |
| 28 | +} |
| 29 | + |
| 30 | +type ThreadMessageRole string |
| 31 | + |
| 32 | +const ( |
| 33 | + ThreadMessageRoleUser ThreadMessageRole = "user" |
| 34 | +) |
| 35 | + |
| 36 | +type ThreadMessage struct { |
| 37 | + Role ThreadMessageRole `json:"role"` |
| 38 | + Content string `json:"content"` |
| 39 | + FileIDs []string `json:"file_ids,omitempty"` |
| 40 | + Metadata map[string]any `json:"metadata,omitempty"` |
| 41 | +} |
| 42 | + |
| 43 | +type ThreadDeleteResponse struct { |
| 44 | + ID string `json:"id"` |
| 45 | + Object string `json:"object"` |
| 46 | + Deleted bool `json:"deleted"` |
| 47 | + |
| 48 | + httpHeader |
| 49 | +} |
| 50 | + |
| 51 | +// CreateThread creates a new thread. |
| 52 | +func (c *Client) CreateThread(ctx context.Context, request ThreadRequest) (response Thread, err error) { |
| 53 | + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(threadsSuffix), withBody(request), |
| 54 | + withBetaAssistantV1()) |
| 55 | + if err != nil { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + err = c.sendRequest(req, &response) |
| 60 | + return |
| 61 | +} |
| 62 | + |
| 63 | +// RetrieveThread retrieves a thread. |
| 64 | +func (c *Client) RetrieveThread(ctx context.Context, threadID string) (response Thread, err error) { |
| 65 | + urlSuffix := threadsSuffix + "/" + threadID |
| 66 | + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), |
| 67 | + withBetaAssistantV1()) |
| 68 | + if err != nil { |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + err = c.sendRequest(req, &response) |
| 73 | + return |
| 74 | +} |
| 75 | + |
| 76 | +// ModifyThread modifies a thread. |
| 77 | +func (c *Client) ModifyThread( |
| 78 | + ctx context.Context, |
| 79 | + threadID string, |
| 80 | + request ModifyThreadRequest, |
| 81 | +) (response Thread, err error) { |
| 82 | + urlSuffix := threadsSuffix + "/" + threadID |
| 83 | + req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), |
| 84 | + withBetaAssistantV1()) |
| 85 | + if err != nil { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + err = c.sendRequest(req, &response) |
| 90 | + return |
| 91 | +} |
| 92 | + |
| 93 | +// DeleteThread deletes a thread. |
| 94 | +func (c *Client) DeleteThread( |
| 95 | + ctx context.Context, |
| 96 | + threadID string, |
| 97 | +) (response ThreadDeleteResponse, err error) { |
| 98 | + urlSuffix := threadsSuffix + "/" + threadID |
| 99 | + req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix), |
| 100 | + withBetaAssistantV1()) |
| 101 | + if err != nil { |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + err = c.sendRequest(req, &response) |
| 106 | + return |
| 107 | +} |
0 commit comments