aiutil

package module
v0.0.0-...-c6bc832 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2025 License: MIT Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateAuthorizedHTTPRequest

func CreateAuthorizedHTTPRequest(ctx context.Context, c Client, method, path string, bodyRdr io.Reader) (*http.Request, error)

func CreateDeleteHTTPRequest

func CreateDeleteHTTPRequest(ctx context.Context, c Client, path string) (*http.Request, error)

func CreateJSONRequest

func CreateJSONRequest(ctx context.Context, c Client, method, path string, bodyV interface{}) (*http.Request, error)

func CreateUploadFileHTTPRequest

func CreateUploadFileHTTPRequest(ctx context.Context, c Client, fileName string, fileData []byte) (*http.Request, error)

func DoHTTPRequest

func DoHTTPRequest(ctx context.Context, c Client, req *http.Request, resp interface{}) error

Types

type Assistant

type Assistant struct {
	ID            string `json:"id"`
	CreatedAt     int    `json:"created_at"`
	AssistantBase `json:",inline"`
}

type AssistantBase

type AssistantBase struct {
	Model          Model         `json:"model"` // gpt-4o
	Name           string        `json:"name"`
	Description    string        `json:"description"`
	Tools          []Tool        `json:"tools"`
	ToolResources  ToolResources `json:"tool_resources"`
	Instructions   string        `json:"instructions"`
	ResponseFormat interface{}   `json:"response_format,omitempty"`
}

type ChatChoice

type ChatChoice struct {
	FinishReason string  `json:"finish_reason"`
	Index        int     `json:"index"`
	Logprobs     string  `json:"logprobs"`
	Message      Message `json:"message"`
}

Response Types

type ChatDefaultResponse

type ChatDefaultResponse struct {
	Choices           []ChatChoice `json:"choices,omitempty"`
	Created           int          `json:"created,omitempty"`
	ID                string       `json:"id,omitempty"`
	Model             string       `json:"model,omitempty"`
	Object            string       `json:"object,omitempty"`
	SystemFingerprint string       `json:"system_fingerprint"`
	Usage             *Usage       `json:"usage,omitempty"`
	Error             *Error       `json:"error,omitempty"`
}

Response Types

type ChatRequest

type ChatRequest struct {
	Model            Model                  `json:"model"`
	Messages         []Message              `json:"messages"`
	ResponseFormat   *MessageResponseFormat `json:"response_format,omitempty"`
	FrequencyPenalty float64                `json:"frequency_penalty,omitempty"` // -2 to 2: Low value = more deterministic, high value = more random
	Temperature      float64                `json:"temperature,omitempty"`       // 0-2: Low value = more deterministic, high value = more random
}

Request Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(ctx context.Context) (Client, error)

NewClient creates a new OpenAI client based on HTTP.

func NewClientWithDefaultKey

func NewClientWithDefaultKey(ctx context.Context) (Client, error)

func NewClientWithKey

func NewClientWithKey(ctx context.Context, apiKey string) (Client, error)

NewClient creates a new OpenAI client based on HTTP.

func (Client) Chat

func (c Client) Chat(ctx context.Context, req ChatRequest) (string, error)

ChatDefault sends a chat request to the OpenAI API.

func (Client) CreateAssistant

func (c Client) CreateAssistant(ctx context.Context, bodyV NewAssistantRequest) (Assistant, error)

func (Client) CreateThreadAndRun

func (c Client) CreateThreadAndRun(ctx context.Context, req NewThreadAndRunRequest) (ThreadRun, error)

func (Client) CreateThreadAndRunCompleted

func (c Client) CreateThreadAndRunCompleted(ctx context.Context, req NewThreadAndRunRequest) (ThreadRun, error)

func (Client) Delete

func (c Client) Delete(ctx context.Context, path string) error

func (Client) DeleteAssistant

func (c Client) DeleteAssistant(ctx context.Context, id string) error

func (Client) DeleteFile

func (c Client) DeleteFile(ctx context.Context, id string) error

func (Client) GetThread

func (c Client) GetThread(ctx context.Context, threadID string) (Thread, error)

func (Client) GetThreadMessages

func (c Client) GetThreadMessages(ctx context.Context, threadID string) ([]ThreadMessage, error)

func (Client) GetThreadRun

func (c Client) GetThreadRun(ctx context.Context, threadID, runID string) (ThreadRun, error)

func (Client) GetThreadRunResponse

func (c Client) GetThreadRunResponse(ctx context.Context, threadID string, runID string) (string, error)

func (Client) MakeChatRequest

func (c Client) MakeChatRequest(ctx context.Context, reqV ChatRequest, respV interface{}) error

Chat sends a chat request to the OpenAI API.

func (Client) NewChatWithAssistant

func (c Client) NewChatWithAssistant(ctx context.Context, req NewThreadAndRunRequest) (string, error)

func (Client) NewThread

func (c Client) NewThread(ctx context.Context, req ThreadInput) (Thread, error)

func (Client) NewThreadRun

func (c Client) NewThreadRun(ctx context.Context, threadID string, req ThreadRunInput) (ThreadRun, error)

func (Client) NewThreadRunComplete

func (c Client) NewThreadRunComplete(ctx context.Context, threadID string, req ThreadRunInput) (ThreadRun, error)

func (Client) UploadFile

func (c Client) UploadFile(ctx context.Context, fileName string, fileData []byte) (File, error)

func (Client) WaitForThreadRunCompletion

func (c Client) WaitForThreadRunCompletion(ctx context.Context, threadID, runID string) (ThreadRun, error)

type DeleteResponse

type DeleteResponse struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Deleted bool   `json:"deleted"`
}
{
  "id": "asst_e5qL6DqZr0gD2PpYjpRAg4BQ",
  "object": "assistant.deleted",
  "deleted": true
}

type Error

type Error struct {
	Code    string `json:"code,omitempty"`
	Message string `json:"message,omitempty"`
	Param   string `json:"param,omitempty"`
	Type    string `json:"type,omitempty"`
}

Response Types

type File

type File struct {
	Object       string  `json:"object"`
	ID           string  `json:"id"`
	Purpose      string  `json:"purpose"`
	Filename     string  `json:"filename"`
	Bytes        int     `json:"bytes"`
	CreatedAt    int     `json:"created_at"`
	Status       string  `json:"status"`
	StatusDetail *string `json:"status_details"`
}
{
  "object": "file",
  "id": "file-xDqy7hp1VUKBItRWOeIxto3r",
  "purpose": "assistants",
  "filename": "goku-schemasys-lock.json",
  "bytes": 80476,
  "created_at": 1731908604,
  "status": "processed",
  "status_details": null
}

type JSONSchema

type JSONSchema []byte

Request Types

func NewJSONSchemaFromStruct

func NewJSONSchemaFromStruct(v interface{}) (JSONSchema, error)

func (JSONSchema) MarshalJSON

func (v JSONSchema) MarshalJSON() ([]byte, error)

func (*JSONSchema) UnmarshalJSON

func (v *JSONSchema) UnmarshalJSON(data []byte) error

type Message

type Message struct {
	Role    Role   `json:"role"`
	Content string `json:"content"`
	Refusal string `json:"refusal,omitempty"` // Optional: Only in responses, if the assistant refuses to respond.
}

Shared Types

type MessageResponseFormat

type MessageResponseFormat struct {
	Type       string                          `json:"type,omitempty"`
	JSONSchema MessageResponseFormatJSONSchema `json:"json_schema,omitempty"`
}

Request Types

type MessageResponseFormatJSONSchema

type MessageResponseFormatJSONSchema struct {
	Description string     `json:"description,omitempty"`
	Name        string     `json:"name,omitempty"`
	Strict      bool       `json:"strict,omitempty"`
	Schema      JSONSchema `json:"schema,omitempty"`
}

Request Types

type Model

type Model string
const (
	Model_GPT_4o          Model = "gpt-4o"            // Doesn't support response type "json_schema"
	Model_GPT_4o_20240806 Model = "gpt-4o-2024-08-06" // 30k TMP
	Model_GPT_4o_mini     Model = "gpt-4o-mini"       // 200k limit
	Model_GPT_3_5_turbo   Model = "gpt-3.5-turbo"     // 200k limit
)

type NewAssistantRequest

type NewAssistantRequest struct {
	AssistantBase `json:",inline"`
}

type NewThreadAndRunRequest

type NewThreadAndRunRequest struct {
	AssistantID    string                 `json:"assistant_id"`
	Thread         ThreadInput            `json:"thread,omitempty"`         // Optional (if we want to provide initial messages)
	ToolResources  ToolResources          `json:"tool_resources,omitempty"` // Maybe allows us to provide more files to the assistant?
	ResponseFormat *MessageResponseFormat `json:"response_format,omitempty"`
}

type Role

type Role string
const (
	Role_Assistant Role = "assistant"
	Role_User      Role = "user"
	Role_System    Role = "system" // Not used in Assistants V2 API
)

type Thread

type Thread struct {
	ID         string `json:"id,omitempty"`
	ThreadBase `json:",inline"`
}

type ThreadBase

type ThreadBase struct{}

type ThreadInput

type ThreadInput struct {
	Messages   []ThreadMessageInput `json:"messages,omitempty"` // only for initial messages, in the Thread request
	ThreadBase `json:",inline"`
}

type ThreadMessage

type ThreadMessage struct {
	ThreadMessageBase `json:",inline"`
	Content           []ThreadMessageContent `json:"content"`
	ID                string                 `json:"id"`
	CreatedAt         int                    `json:"created_at"`
	AssistantID       string                 `json:"assistant_id"`
	ThreadID          string                 `json:"thread_id"`
	RunID             string                 `json:"run_id"`
}

type ThreadMessageBase

type ThreadMessageBase struct {
	Role Role `json:"role"`
}

type ThreadMessageContent

type ThreadMessageContent struct {
	Type string                   `json:"type"`
	Text ThreadMessageContentText `json:"text"`
}

type ThreadMessageContentText

type ThreadMessageContentText struct {
	Value       string        `json:"value"`
	Annotations []interface{} `json:"annotations"`
}

type ThreadMessageInput

type ThreadMessageInput struct {
	ThreadMessageBase `json:",inline"`
	Content           string `json:"content"`
}

type ThreadMessagesResponse

type ThreadMessagesResponse struct {
	Object string          `json:"object"`
	Data   []ThreadMessage `json:"data"`
}

type ThreadRun

type ThreadRun struct {
	ID                  string   `json:"id"`
	Object              string   `json:"object"` // Always "thread.run"
	CreatedAt           int      `json:"created_at"`
	AssistantID         string   `json:"assistant_id"`
	ThreadID            string   `json:"thread_id"`
	Status              string   `json:"status"`
	StartedAt           int      `json:"started_at"`
	ExpiresAt           int      `json:"expires_at"`
	CancelledAt         int      `json:"cancelled_at"`
	FailedAt            int      `json:"failed_at"`
	CompletedAt         int      `json:"completed_at"`
	LastError           *Error   `json:"last_error,omitempty"`
	Model               Model    `json:"model"`
	Instructions        string   `json:"instructions"`
	Tools               []Tool   `json:"tools"`
	Metadata            struct{} `json:"metadata"`
	IncompleteDetails   string   `json:"incomplete_details"`
	Usage               Usage    `json:"usage"`
	Temperature         float64  `json:"temperature"`
	TopP                float64  `json:"top_p"`
	MaxPromptTokens     int      `json:"max_prompt_tokens"`
	MaxCompletionTokens int      `json:"max_completion_tokens"`
	TruncationStrategy  struct {
		Type         string `json:"type"`
		LastMessages string `json:"last_messages"`
	} `json:"truncation_strategy"`
	ResponseFormat    interface{} `json:"response_format"`
	ToolChoice        string      `json:"tool_choice"`
	ParallelToolCalls bool        `json:"parallel_tool_calls"`
}
{
  "id": "run_abc123",
  "object": "thread.run",
  "created_at": 1698107661,
  "assistant_id": "asst_abc123",
  "thread_id": "thread_abc123",
  "status": "completed",
  "started_at": 1699073476,
  "expires_at": null,
  "cancelled_at": null,
  "failed_at": null,
  "completed_at": 1699073498,
  "last_error": null,
  "model": "gpt-4o",
  "instructions": null,
  "tools": [{"type": "file_search"}, {"type": "code_interpreter"}],
  "metadata": {},
  "incomplete_details": null,
  "usage": {
    "prompt_tokens": 123,
    "completion_tokens": 456,
    "total_tokens": 579
  },
  "temperature": 1.0,
  "top_p": 1.0,
  "max_prompt_tokens": 1000,
  "max_completion_tokens": 1000,
  "truncation_strategy": {
    "type": "auto",
    "last_messages": null
  },
  "response_format": "auto",
  "tool_choice": "auto",
  "parallel_tool_calls": true
}

type ThreadRunInput

type ThreadRunInput struct {
	AssistantID        string                 `json:"assistant_id"`
	Model              Model                  `json:"model"` // If provided, will override the assistant's model
	AdditionalMessages []ThreadMessageInput   `json:"additional_messages,omitempty"`
	ResponseFormat     *MessageResponseFormat `json:"response_format,omitempty"`
}

type Tool

type Tool struct {
	Type string `json:"type"` // code_interpreter, file_search
}

type ToolResources

type ToolResources struct {
	CodeInterpreter ToolResourcesCodeInterpreter `json:"code_interpreter"`
	FileSearch      ToolResourcesFileSearch      `json:"file_search,omitempty"`
}

type ToolResourcesCodeInterpreter

type ToolResourcesCodeInterpreter struct {
	FileIDs []string `json:"file_ids,omitempty"`
}

type ToolResourcesFileSearch

type ToolResourcesFileSearch struct {
	VectorStores   []ToolResourcesFileSearchVectorStore `json:"vector_stores,omitempty"`
	VectorStoreIDs []string                             `json:"vector_store_ids,omitempty"`
}

type ToolResourcesFileSearchVectorStore

type ToolResourcesFileSearchVectorStore struct {
	FileIDs []string `json:"file_ids,omitempty"`
}

type Usage

type Usage struct {
	CompletionTokens       int `json:"completion_tokens"`
	CompletionTokensDetail struct {
		ReasoningTokens int `json:"reasoning_tokens"`
	} `json:"completion_tokens_details"`
	PromptTokens       int `json:"prompt_tokens"`
	PromptTokensDetail struct {
		CachedTokens int `json:"cached_tokens"`
	} `json:"prompt_tokens_details"`
	TotalTokens int `json:"total_tokens"`
}

Response Types

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL