Fix Refactor assistant api (#545)
* fix: refactor assistant API * fix * trigger build * fix: use AssistantDeleteResponse
This commit is contained in:
92
assistant.go
92
assistant.go
@@ -10,46 +10,43 @@ import (
|
|||||||
const (
|
const (
|
||||||
assistantsSuffix = "/assistants"
|
assistantsSuffix = "/assistants"
|
||||||
assistantsFilesSuffix = "/files"
|
assistantsFilesSuffix = "/files"
|
||||||
|
openaiAssistantsV1 = "assistants=v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Assistant struct {
|
type Assistant struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Object string `json:"object"`
|
Object string `json:"object"`
|
||||||
CreatedAt int64 `json:"created_at"`
|
CreatedAt int64 `json:"created_at"`
|
||||||
Name *string `json:"name,omitempty"`
|
Name *string `json:"name,omitempty"`
|
||||||
Description *string `json:"description,omitempty"`
|
Description *string `json:"description,omitempty"`
|
||||||
Model string `json:"model"`
|
Model string `json:"model"`
|
||||||
Instructions *string `json:"instructions,omitempty"`
|
Instructions *string `json:"instructions,omitempty"`
|
||||||
Tools []any `json:"tools,omitempty"`
|
Tools []AssistantTool `json:"tools,omitempty"`
|
||||||
|
|
||||||
httpHeader
|
httpHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AssistantToolType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
AssistantToolTypeCodeInterpreter AssistantToolType = "code_interpreter"
|
||||||
|
AssistantToolTypeRetrieval AssistantToolType = "retrieval"
|
||||||
|
AssistantToolTypeFunction AssistantToolType = "function"
|
||||||
|
)
|
||||||
|
|
||||||
type AssistantTool struct {
|
type AssistantTool struct {
|
||||||
Type string `json:"type"`
|
Type AssistantToolType `json:"type"`
|
||||||
}
|
Function *FunctionDefinition `json:"function,omitempty"`
|
||||||
|
|
||||||
type AssistantToolCodeInterpreter struct {
|
|
||||||
AssistantTool
|
|
||||||
}
|
|
||||||
|
|
||||||
type AssistantToolRetrieval struct {
|
|
||||||
AssistantTool
|
|
||||||
}
|
|
||||||
|
|
||||||
type AssistantToolFunction struct {
|
|
||||||
AssistantTool
|
|
||||||
Function FunctionDefinition `json:"function"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type AssistantRequest struct {
|
type AssistantRequest struct {
|
||||||
Model string `json:"model"`
|
Model string `json:"model"`
|
||||||
Name *string `json:"name,omitempty"`
|
Name *string `json:"name,omitempty"`
|
||||||
Description *string `json:"description,omitempty"`
|
Description *string `json:"description,omitempty"`
|
||||||
Instructions *string `json:"instructions,omitempty"`
|
Instructions *string `json:"instructions,omitempty"`
|
||||||
Tools []any `json:"tools,omitempty"`
|
Tools []AssistantTool `json:"tools,omitempty"`
|
||||||
FileIDs []string `json:"file_ids,omitempty"`
|
FileIDs []string `json:"file_ids,omitempty"`
|
||||||
Metadata map[string]any `json:"metadata,omitempty"`
|
Metadata map[string]any `json:"metadata,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// AssistantsList is a list of assistants.
|
// AssistantsList is a list of assistants.
|
||||||
@@ -59,6 +56,14 @@ type AssistantsList struct {
|
|||||||
httpHeader
|
httpHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AssistantDeleteResponse struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Object string `json:"object"`
|
||||||
|
Deleted bool `json:"deleted"`
|
||||||
|
|
||||||
|
httpHeader
|
||||||
|
}
|
||||||
|
|
||||||
type AssistantFile struct {
|
type AssistantFile struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Object string `json:"object"`
|
Object string `json:"object"`
|
||||||
@@ -80,7 +85,8 @@ type AssistantFilesList struct {
|
|||||||
|
|
||||||
// CreateAssistant creates a new assistant.
|
// CreateAssistant creates a new assistant.
|
||||||
func (c *Client) CreateAssistant(ctx context.Context, request AssistantRequest) (response Assistant, err error) {
|
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),
|
||||||
|
withBetaAssistantV1())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -95,7 +101,8 @@ func (c *Client) RetrieveAssistant(
|
|||||||
assistantID string,
|
assistantID string,
|
||||||
) (response Assistant, err error) {
|
) (response Assistant, err error) {
|
||||||
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
|
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),
|
||||||
|
withBetaAssistantV1())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -111,7 +118,8 @@ func (c *Client) ModifyAssistant(
|
|||||||
request AssistantRequest,
|
request AssistantRequest,
|
||||||
) (response Assistant, err error) {
|
) (response Assistant, err error) {
|
||||||
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
|
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),
|
||||||
|
withBetaAssistantV1())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -124,9 +132,10 @@ func (c *Client) ModifyAssistant(
|
|||||||
func (c *Client) DeleteAssistant(
|
func (c *Client) DeleteAssistant(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
assistantID string,
|
assistantID string,
|
||||||
) (response Assistant, err error) {
|
) (response AssistantDeleteResponse, err error) {
|
||||||
urlSuffix := fmt.Sprintf("%s/%s", assistantsSuffix, assistantID)
|
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),
|
||||||
|
withBetaAssistantV1())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -163,7 +172,8 @@ func (c *Client) ListAssistants(
|
|||||||
}
|
}
|
||||||
|
|
||||||
urlSuffix := fmt.Sprintf("%s%s", assistantsSuffix, encodedValues)
|
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),
|
||||||
|
withBetaAssistantV1())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -180,7 +190,8 @@ func (c *Client) CreateAssistantFile(
|
|||||||
) (response AssistantFile, err error) {
|
) (response AssistantFile, err error) {
|
||||||
urlSuffix := fmt.Sprintf("%s/%s%s", assistantsSuffix, assistantID, assistantsFilesSuffix)
|
urlSuffix := fmt.Sprintf("%s/%s%s", assistantsSuffix, assistantID, assistantsFilesSuffix)
|
||||||
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix),
|
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix),
|
||||||
withBody(request))
|
withBody(request),
|
||||||
|
withBetaAssistantV1())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -196,7 +207,8 @@ func (c *Client) RetrieveAssistantFile(
|
|||||||
fileID string,
|
fileID string,
|
||||||
) (response AssistantFile, err error) {
|
) (response AssistantFile, err error) {
|
||||||
urlSuffix := fmt.Sprintf("%s/%s%s/%s", assistantsSuffix, assistantID, assistantsFilesSuffix, fileID)
|
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),
|
||||||
|
withBetaAssistantV1())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -212,7 +224,8 @@ func (c *Client) DeleteAssistantFile(
|
|||||||
fileID string,
|
fileID string,
|
||||||
) (err error) {
|
) (err error) {
|
||||||
urlSuffix := fmt.Sprintf("%s/%s%s/%s", assistantsSuffix, assistantID, assistantsFilesSuffix, fileID)
|
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),
|
||||||
|
withBetaAssistantV1())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -250,7 +263,8 @@ func (c *Client) ListAssistantFiles(
|
|||||||
}
|
}
|
||||||
|
|
||||||
urlSuffix := fmt.Sprintf("%s/%s%s%s", assistantsSuffix, assistantID, assistantsFilesSuffix, encodedValues)
|
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),
|
||||||
|
withBetaAssistantV1())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,6 +83,12 @@ func withContentType(contentType string) requestOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func withBetaAssistantV1() requestOption {
|
||||||
|
return func(args *requestOptions) {
|
||||||
|
args.header.Set("OpenAI-Beta", "assistants=v1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) newRequest(ctx context.Context, method, url string, setters ...requestOption) (*http.Request, error) {
|
func (c *Client) newRequest(ctx context.Context, method, url string, setters ...requestOption) (*http.Request, error) {
|
||||||
// Default Options
|
// Default Options
|
||||||
args := &requestOptions{
|
args := &requestOptions{
|
||||||
|
|||||||
Reference in New Issue
Block a user