Documentation
¶
Index ¶
- Constants
- type APIType
- type ChatCompletionChoice
- type ChatCompletionRequest
- type ChatCompletionResponse
- type ChatCompletionUsage
- type ChatMessage
- type ChatMessageToolCall
- type ChatMessageToolCallFunction
- type ChatTool
- type ChatToolFunction
- type ErrorResponse
- type Model
- type Provider
- func (p *Provider) GetModel(name string) (model.Model, error)
- func (p *Provider) ResetRateLimiter()
- func (p *Provider) SetAPIType(apiType APIType) *Provider
- func (p *Provider) SetAPIVersion(apiVersion string) *Provider
- func (p *Provider) SetBaseURL(baseURL string) *Provider
- func (p *Provider) SetDefaultModel(modelName string) *Provider
- func (p *Provider) UpdateTokenCount(tokens int)
- func (p *Provider) WaitForRateLimit()
- func (p *Provider) WithAPIKey(apiKey string) *Provider
- func (p *Provider) WithDefaultModel(modelName string) *Provider
- func (p *Provider) WithHTTPClient(client *http.Client) *Provider
- func (p *Provider) WithOrganization(org string) *Provider
- func (p *Provider) WithRateLimit(rpm, tpm int) *Provider
- func (p *Provider) WithRetryConfig(maxRetries int, retryAfter time.Duration) *Provider
- type StreamOptions
Constants ¶
const ( // DefaultBaseURL is the default base URL for the OpenAI API DefaultBaseURL = "https://api.openai.com/v1" // DefaultRPM is the default rate limit for requests per minute DefaultRPM = 200 // DefaultTPM is the default rate limit for tokens per minute DefaultTPM = 150000 // DefaultMaxRetries is the default number of retries for rate limited requests DefaultMaxRetries = 5 // DefaultRetryAfter is the default time to wait before retrying a rate limited request DefaultRetryAfter = 1 * time.Second DefaultAPIVersion = "2023-05-15" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChatCompletionChoice ¶
type ChatCompletionChoice struct {
Index int `json:"index"`
Message ChatMessage `json:"message"`
FinishReason string `json:"finish_reason"`
}
ChatCompletionChoice represents a choice in a chat completion response
type ChatCompletionRequest ¶
type ChatCompletionRequest struct {
Model string `json:"model"`
Messages []ChatMessage `json:"messages"`
Tools []ChatTool `json:"tools,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
TopP float64 `json:"top_p,omitempty"`
FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
PresencePenalty float64 `json:"presence_penalty,omitempty"`
MaxCompletionTokens int `json:"max_completion_tokens,omitempty"`
Stream bool `json:"stream,omitempty"`
StreamOptions *StreamOptions `json:"stream_options,omitempty"`
ResponseFormat interface{} `json:"response_format,omitempty"`
}
ChatCompletionRequest represents a request to the chat completions API
type ChatCompletionResponse ¶
type ChatCompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []ChatCompletionChoice `json:"choices"`
Usage ChatCompletionUsage `json:"usage"`
}
ChatCompletionResponse represents a response from the chat completions API
type ChatCompletionUsage ¶
type ChatCompletionUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
ChatCompletionUsage represents usage information in a chat completion response
type ChatMessage ¶
type ChatMessage struct {
Role string `json:"role"`
Content interface{} `json:"content"`
Name string `json:"name,omitempty"`
ToolCalls []ChatMessageToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
ChatMessage represents a message in a chat. Content is interface{} to support both plain strings and multimodal content arrays.
type ChatMessageToolCall ¶
type ChatMessageToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function ChatMessageToolCallFunction `json:"function"`
}
ChatMessageToolCall represents a tool call in a chat message
type ChatMessageToolCallFunction ¶
type ChatMessageToolCallFunction struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
}
ChatMessageToolCallFunction represents a function in a tool call
type ChatTool ¶
type ChatTool struct {
Type string `json:"type"`
Function ChatToolFunction `json:"function"`
}
ChatTool represents a tool in a chat
type ChatToolFunction ¶
type ChatToolFunction struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]interface{} `json:"parameters"`
}
ChatToolFunction represents a function in a tool
type ErrorResponse ¶
type ErrorResponse struct {
Error struct {
Message string `json:"message"`
Type string `json:"type"`
Param string `json:"param"`
Code string `json:"code"`
} `json:"error"`
}
ErrorResponse represents an error response from the API
type Model ¶
Model implements the model.Model interface for OpenAI
func (*Model) GetResponse ¶
GetResponse gets a single response from the model with retry logic
func (*Model) StreamResponse ¶
func (m *Model) StreamResponse(ctx context.Context, request *model.Request) (<-chan model.StreamEvent, error)
StreamResponse streams a response from the model with retry logic
type Provider ¶
type Provider struct {
// Configuration
APIKey string
Organization string
HTTPClient *http.Client
// Model configuration
DefaultModel string
// Rate limiting configuration
RPM int // Requests per minute
TPM int // Tokens per minute
MaxRetries int // Maximum number of retries
RetryAfter time.Duration // Time to wait before retrying
// contains filtered or unexported fields
}
Provider implements model.Provider for OpenAI
func NewOpenAIProvider ¶
NewOpenAIProvider creates a new Provider with default settings
func NewProvider ¶
NewProvider creates a new provider with default settings, requires an API key
func (*Provider) ResetRateLimiter ¶
func (p *Provider) ResetRateLimiter()
ResetRateLimiter resets the rate limit counters
func (*Provider) SetAPIType ¶
SetAPIType sets the api type for the provider
func (*Provider) SetAPIVersion ¶
SetAPIType sets the api version for the provider
func (*Provider) SetBaseURL ¶
SetBaseURL sets the base URL for the provider
func (*Provider) SetDefaultModel ¶
SetDefaultModel sets the default model for the provider
func (*Provider) UpdateTokenCount ¶
UpdateTokenCount updates the token count for rate limiting
func (*Provider) WaitForRateLimit ¶
func (p *Provider) WaitForRateLimit()
WaitForRateLimit waits for the rate limiter to allow a new request
func (*Provider) WithAPIKey ¶
WithAPIKey sets the API key for the provider
func (*Provider) WithDefaultModel ¶
WithDefaultModel sets the default model for the provider
func (*Provider) WithHTTPClient ¶
WithHTTPClient sets the HTTP client for the provider
func (*Provider) WithOrganization ¶
WithOrganization sets the organization for the provider
func (*Provider) WithRateLimit ¶
WithRateLimit sets the rate limit configuration for the provider
type StreamOptions ¶ added in v0.16.0
type StreamOptions struct {
IncludeUsage bool `json:"include_usage"`
}
StreamOptions configures streaming behavior.