Skip to content

Commit

Permalink
Remove hardcoded assistants version (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
danai-antoniou authored Apr 24, 2024
1 parent c6a63ed commit 8d15a37
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 45 deletions.
19 changes: 9 additions & 10 deletions assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
const (
assistantsSuffix = "/assistants"
assistantsFilesSuffix = "/files"
openaiAssistantsV1 = "assistants=v1"
)

type Assistant struct {
Expand Down Expand Up @@ -116,7 +115,7 @@ 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),
withBetaAssistantV1())
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -132,7 +131,7 @@ func (c *Client) RetrieveAssistant(
) (response Assistant, err error) {
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
withBetaAssistantV1())
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -149,7 +148,7 @@ func (c *Client) ModifyAssistant(
) (response Assistant, err error) {
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request),
withBetaAssistantV1())
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -165,7 +164,7 @@ func (c *Client) DeleteAssistant(
) (response AssistantDeleteResponse, err error) {
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix),
withBetaAssistantV1())
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand Down Expand Up @@ -203,7 +202,7 @@ func (c *Client) ListAssistants(

urlSuffix := fmt.Sprintf("%s%s", assistantsSuffix, encodedValues)
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
withBetaAssistantV1())
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -221,7 +220,7 @@ func (c *Client) CreateAssistantFile(
urlSuffix := fmt.Sprintf("%s/%s%s", assistantsSuffix, assistantID, assistantsFilesSuffix)
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix),
withBody(request),
withBetaAssistantV1())
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -238,7 +237,7 @@ func (c *Client) RetrieveAssistantFile(
) (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),
withBetaAssistantV1())
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -255,7 +254,7 @@ func (c *Client) DeleteAssistantFile(
) (err error) {
urlSuffix := fmt.Sprintf("%s/%s%s/%s", assistantsSuffix, assistantID, assistantsFilesSuffix, fileID)
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix),
withBetaAssistantV1())
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand Down Expand Up @@ -294,7 +293,7 @@ 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),
withBetaAssistantV1())
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ func withContentType(contentType string) requestOption {
}
}

func withBetaAssistantV1() requestOption {
func withBetaAssistantVersion(version string) requestOption {
return func(args *requestOptions) {
args.header.Set("OpenAI-Beta", "assistants=v1")
args.header.Set("OpenAI-Beta", fmt.Sprintf("assistants=%s", version))
}
}

Expand Down
14 changes: 9 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ const (

const AzureAPIKeyHeader = "api-key"

const defaultAssistantVersion = "v1" // This will be deprecated by the end of 2024.

// ClientConfig is a configuration of a client.
type ClientConfig struct {
authToken string

BaseURL string
OrgID string
APIType APIType
APIVersion string // required when APIType is APITypeAzure or APITypeAzureAD
APIVersion string // required when APIType is APITypeAzure or APITypeAzureAD
AssistantVersion string
AzureModelMapperFunc func(model string) string // replace model to azure deployment name func
HTTPClient *http.Client

Expand All @@ -39,10 +42,11 @@ type ClientConfig struct {

func DefaultConfig(authToken string) ClientConfig {
return ClientConfig{
authToken: authToken,
BaseURL: openaiAPIURLv1,
APIType: APITypeOpenAI,
OrgID: "",
authToken: authToken,
BaseURL: openaiAPIURLv1,
APIType: APITypeOpenAI,
AssistantVersion: defaultAssistantVersion,
OrgID: "",

HTTPClient: &http.Client{},

Expand Down
17 changes: 11 additions & 6 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ type MessageFilesList struct {
// CreateMessage creates a new message.
func (c *Client) CreateMessage(ctx context.Context, threadID string, request MessageRequest) (msg Message, err error) {
urlSuffix := fmt.Sprintf("/threads/%s/%s", threadID, messagesSuffix)
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request), withBetaAssistantV1())
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request),
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand Down Expand Up @@ -111,7 +112,8 @@ func (c *Client) ListMessage(ctx context.Context, threadID string,
}

urlSuffix := fmt.Sprintf("/threads/%s/%s%s", threadID, messagesSuffix, encodedValues)
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantV1())
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -126,7 +128,8 @@ func (c *Client) RetrieveMessage(
threadID, messageID string,
) (msg Message, err error) {
urlSuffix := fmt.Sprintf("/threads/%s/%s/%s", threadID, messagesSuffix, messageID)
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantV1())
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -143,7 +146,7 @@ func (c *Client) ModifyMessage(
) (msg Message, err error) {
urlSuffix := fmt.Sprintf("/threads/%s/%s/%s", threadID, messagesSuffix, messageID)
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix),
withBody(map[string]any{"metadata": metadata}), withBetaAssistantV1())
withBody(map[string]any{"metadata": metadata}), withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -158,7 +161,8 @@ func (c *Client) RetrieveMessageFile(
threadID, messageID, fileID string,
) (file MessageFile, err error) {
urlSuffix := fmt.Sprintf("/threads/%s/%s/%s/files/%s", threadID, messagesSuffix, messageID, fileID)
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantV1())
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -173,7 +177,8 @@ func (c *Client) ListMessageFiles(
threadID, messageID string,
) (files MessageFilesList, err error) {
urlSuffix := fmt.Sprintf("/threads/%s/%s/%s/files", threadID, messagesSuffix, messageID)
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix), withBetaAssistantV1())
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand Down
27 changes: 9 additions & 18 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ func (c *Client) CreateRun(
http.MethodPost,
c.fullURL(urlSuffix),
withBody(request),
withBetaAssistantV1(),
)
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -247,8 +246,7 @@ func (c *Client) RetrieveRun(
ctx,
http.MethodGet,
c.fullURL(urlSuffix),
withBetaAssistantV1(),
)
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -270,8 +268,7 @@ func (c *Client) ModifyRun(
http.MethodPost,
c.fullURL(urlSuffix),
withBody(request),
withBetaAssistantV1(),
)
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand Down Expand Up @@ -310,8 +307,7 @@ func (c *Client) ListRuns(
ctx,
http.MethodGet,
c.fullURL(urlSuffix),
withBetaAssistantV1(),
)
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -332,8 +328,7 @@ func (c *Client) SubmitToolOutputs(
http.MethodPost,
c.fullURL(urlSuffix),
withBody(request),
withBetaAssistantV1(),
)
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -352,8 +347,7 @@ func (c *Client) CancelRun(
ctx,
http.MethodPost,
c.fullURL(urlSuffix),
withBetaAssistantV1(),
)
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -372,8 +366,7 @@ func (c *Client) CreateThreadAndRun(
http.MethodPost,
c.fullURL(urlSuffix),
withBody(request),
withBetaAssistantV1(),
)
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -394,8 +387,7 @@ func (c *Client) RetrieveRunStep(
ctx,
http.MethodGet,
c.fullURL(urlSuffix),
withBetaAssistantV1(),
)
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand Down Expand Up @@ -435,8 +427,7 @@ func (c *Client) ListRunSteps(
ctx,
http.MethodGet,
c.fullURL(urlSuffix),
withBetaAssistantV1(),
)
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand Down
8 changes: 4 additions & 4 deletions thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type ThreadDeleteResponse struct {
// CreateThread creates a new thread.
func (c *Client) CreateThread(ctx context.Context, request ThreadRequest) (response Thread, err error) {
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(threadsSuffix), withBody(request),
withBetaAssistantV1())
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -64,7 +64,7 @@ func (c *Client) CreateThread(ctx context.Context, request ThreadRequest) (respo
func (c *Client) RetrieveThread(ctx context.Context, threadID string) (response Thread, err error) {
urlSuffix := threadsSuffix + "/" + threadID
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix),
withBetaAssistantV1())
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -81,7 +81,7 @@ func (c *Client) ModifyThread(
) (response Thread, err error) {
urlSuffix := threadsSuffix + "/" + threadID
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix), withBody(request),
withBetaAssistantV1())
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand All @@ -97,7 +97,7 @@ func (c *Client) DeleteThread(
) (response ThreadDeleteResponse, err error) {
urlSuffix := threadsSuffix + "/" + threadID
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix),
withBetaAssistantV1())
withBetaAssistantVersion(c.config.AssistantVersion))
if err != nil {
return
}
Expand Down

0 comments on commit 8d15a37

Please sign in to comment.