diff options
| author | Xe Iaso <me@xeiaso.net> | 2023-11-06 20:56:35 -0500 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2023-11-06 20:56:35 -0500 |
| commit | fd054092f6ebb192534817304d2f6faee19ec217 (patch) | |
| tree | 69fdd51f9edfbdbb808a7ba72943cbd147383e91 /web/openai/assistant/thread.go | |
| parent | 7dfa71d8e887a2d0230ada2b70202f037b15c974 (diff) | |
| download | x-fd054092f6ebb192534817304d2f6faee19ec217.tar.xz x-fd054092f6ebb192534817304d2f6faee19ec217.zip | |
web/openai: try to make assistant bindings
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'web/openai/assistant/thread.go')
| -rw-r--r-- | web/openai/assistant/thread.go | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/web/openai/assistant/thread.go b/web/openai/assistant/thread.go new file mode 100644 index 0000000..af052fc --- /dev/null +++ b/web/openai/assistant/thread.go @@ -0,0 +1,95 @@ +package assistant + +import ( + "context" + "encoding/json" + "net/http" + + "within.website/x/web" +) + +type Thread struct { + ID string `json:"id"` + Object string `json:"object"` + CreatedAt int64 `json:"created_at"` + Metadata map[string]string `json:"metadata"` +} + +func (c *Client) CreateThread(ctx context.Context) (*Thread, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://api.openai.com/v1/threads", nil) + if err != nil { + return nil, err + } + + resp, err := c.Do(req) + if err != nil { + return nil, err + } + + var t Thread + if err := json.NewDecoder(resp.Body).Decode(&t); err != nil { + return nil, err + } + + return &t, nil +} + +func (c *Client) GetThread(ctx context.Context, id string) (*Thread, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.openai.com/v1/threads/"+id, nil) + if err != nil { + return nil, err + } + + resp, err := c.Do(req) + if err != nil { + return nil, err + } + + var t Thread + if err := json.NewDecoder(resp.Body).Decode(&t); err != nil { + return nil, err + } + + if resp.StatusCode != http.StatusOK { + return nil, web.NewError(http.StatusOK, resp) + } + + return &t, nil +} + +func (c *Client) UpdateThread(ctx context.Context, id string, metadata map[string]string) error { + req, err := http.NewRequestWithContext(ctx, http.MethodPatch, "https://api.openai.com/v1/threads/"+id, nil) + if err != nil { + return err + } + + resp, err := c.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return web.NewError(http.StatusOK, resp) + } + + return nil +} + +func (c *Client) DeleteThread(ctx context.Context, id string) error { + req, err := http.NewRequestWithContext(ctx, http.MethodDelete, "https://api.openai.com/v1/threads/"+id, nil) + if err != nil { + return err + } + + resp, err := c.Do(req) + if err != nil { + return err + } + + if resp.StatusCode != http.StatusOK { + return web.NewError(http.StatusOK, resp) + } + + return nil +} |
