Skip to content

Commit

Permalink
fix: refactor assistant API
Browse files Browse the repository at this point in the history
  • Loading branch information
henomis committed Nov 7, 2023
1 parent 1ad6b6f commit 934aef9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 37 deletions.
81 changes: 44 additions & 37 deletions assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,44 @@ import (
const (
assistantsSuffix = "/assistants"
assistantsFilesSuffix = "/files"
openaiBetaHeader = "OpenAI-Beta"
openaiAssistantsV1 = "assistants=v1"
)

type Assistant struct {
ID string `json:"id"`
Object string `json:"object"`
CreatedAt int64 `json:"created_at"`
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
Model string `json:"model"`
Instructions *string `json:"instructions,omitempty"`
Tools []any `json:"tools,omitempty"`
ID string `json:"id"`
Object string `json:"object"`
CreatedAt int64 `json:"created_at"`
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
Model string `json:"model"`
Instructions *string `json:"instructions,omitempty"`
Tools []AssistantTool `json:"tools,omitempty"`

httpHeader
}

type AssistantTool struct {
Type string `json:"type"`
}

type AssistantToolCodeInterpreter struct {
AssistantTool
}
type AssistantToolType string

type AssistantToolRetrieval struct {
AssistantTool
}
const (
AssistantToolTypeCodeInterpreter AssistantToolType = "code_interpreter"
AssistantToolTypeRetrieval AssistantToolType = "retrieval"
AssistantToolTypeFunction AssistantToolType = "function"
)

type AssistantToolFunction struct {
AssistantTool
Function FunctionDefinition `json:"function"`
type AssistantTool struct {
Type AssistantToolType `json:"type"`
Function *FunctionDefinition `json:"function,omitempty"`
}

type AssistantRequest struct {
Model string `json:"model"`
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
Instructions *string `json:"instructions,omitempty"`
Tools []any `json:"tools,omitempty"`
FileIDs []string `json:"file_ids,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
Model string `json:"model"`
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
Instructions *string `json:"instructions,omitempty"`
Tools []AssistantTool `json:"tools,omitempty"`
FileIDs []string `json:"file_ids,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}

// AssistantsList is a list of assistants.
Expand Down Expand Up @@ -80,7 +78,8 @@ type AssistantFilesList struct {

// CreateAssistant creates a new assistant.
func (c *Client) CreateAssistant(ctx context.Context, request AssistantRequest) (response Assistant, err error) {
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(assistantsSuffix), withBody(request))
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(assistantsSuffix), withBody(request),
withHeader(openaiBetaHeader, openaiAssistantsV1))
if err != nil {
return
}
Expand All @@ -95,7 +94,8 @@ func (c *Client) RetrieveAssistant(
assistantID string,
) (response Assistant, err error) {
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
withHeader(openaiBetaHeader, openaiAssistantsV1))
if err != nil {
return
}
Expand All @@ -111,7 +111,8 @@ func (c *Client) ModifyAssistant(
request AssistantRequest,
) (response Assistant, err error) {
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request))
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request),
withHeader(openaiBetaHeader, openaiAssistantsV1))
if err != nil {
return
}
Expand All @@ -126,7 +127,8 @@ func (c *Client) DeleteAssistant(
assistantID string,
) (response Assistant, err error) {
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix))
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix),
withHeader(openaiBetaHeader, openaiAssistantsV1))
if err != nil {
return
}
Expand Down Expand Up @@ -163,7 +165,8 @@ func (c *Client) ListAssistants(
}

urlSuffix := fmt.Sprintf("%s%s", assistantsSuffix, encodedValues)
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
withHeader(openaiBetaHeader, openaiAssistantsV1))
if err != nil {
return
}
Expand All @@ -180,7 +183,8 @@ func (c *Client) CreateAssistantFile(
) (response AssistantFile, err error) {
urlSuffix := fmt.Sprintf("%s/%s%s", assistantsSuffix, assistantID, assistantsFilesSuffix)
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix),
withBody(request))
withBody(request),
withHeader(openaiBetaHeader, openaiAssistantsV1))
if err != nil {
return
}
Expand All @@ -196,7 +200,8 @@ func (c *Client) RetrieveAssistantFile(
fileID string,
) (response AssistantFile, err error) {
urlSuffix := fmt.Sprintf("%s/%s%s/%s", assistantsSuffix, assistantID, assistantsFilesSuffix, fileID)
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
withHeader(openaiBetaHeader, openaiAssistantsV1))
if err != nil {
return
}
Expand All @@ -212,7 +217,8 @@ func (c *Client) DeleteAssistantFile(
fileID string,
) (err error) {
urlSuffix := fmt.Sprintf("%s/%s%s/%s", assistantsSuffix, assistantID, assistantsFilesSuffix, fileID)
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix))
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix),
withHeader(openaiBetaHeader, openaiAssistantsV1))
if err != nil {
return
}
Expand Down Expand Up @@ -250,7 +256,8 @@ func (c *Client) ListAssistantFiles(
}

urlSuffix := fmt.Sprintf("%s/%s%s%s", assistantsSuffix, assistantID, assistantsFilesSuffix, encodedValues)
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
withHeader(openaiBetaHeader, openaiAssistantsV1))
if err != nil {
return
}
Expand Down
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ func withContentType(contentType string) requestOption {
}
}

func withHeader(key, value string) requestOption {
return func(args *requestOptions) {
args.header.Set(key, value)
}
}

func (c *Client) newRequest(ctx context.Context, method, url string, setters ...requestOption) (*http.Request, error) {
// Default Options
args := &requestOptions{
Expand Down

0 comments on commit 934aef9

Please sign in to comment.