Documentation
¶
Index ¶
- func IsQuotaExceededError(err error) bool
- func IsRetryableError(err error) bool
- func ParseRateLimitDelay(err error) time.Duration
- func TestBuilder(t *testing.T)
- func TestConversationConfig(t *testing.T)
- func TestConversationMessages(t *testing.T)
- func TestError(t *testing.T)
- func TestMessage(t *testing.T)
- func TestModel(t *testing.T)
- func TestModelRegistry(t *testing.T)
- func WithRetry(ctx context.Context, req *types.CompletionRequest, config *RetryConfig, ...) (*types.CompletionResponse, error)
- type AIClient
- func (b *AIClient) Build() (*Client, error)
- func (b *AIClient) WithDefaultMaxTokens(maxTokens int) *AIClient
- func (b *AIClient) WithDefaultModel(model string) *AIClient
- func (b *AIClient) WithDefaultProvider(provider string) *AIClient
- func (b *AIClient) WithDefaultTemperature(temperature float64) *AIClient
- func (b *AIClient) WithGoogle(apiKey, projectID string, options ...GoogleOption) *AIClient
- func (b *AIClient) WithMiddleware(middleware ...Middleware) *AIClient
- func (b *AIClient) WithOpenAI(apiKey string, options ...OpenAIOption) *AIClient
- func (b *AIClient) WithReplicate(apiKey string, options ...ReplicateOption) *AIClient
- type Client
- func (c *Client) Close() error
- func (c *Client) Complete(ctx context.Context, req *types.CompletionRequest) (*types.CompletionResponse, error)
- func (c *Client) EstimateTokens(ctx context.Context, messages []*types.Message, model string) (int, error)
- func (c *Client) GetModel(provider, id string) (*types.Model, error)
- func (c *Client) GetProvider(name string) (types.Provider, error)
- func (c *Client) ListModels() []*types.Model
- func (c *Client) ListModelsByProvider(provider string) []*types.Model
- func (c *Client) NewConversation(config *ConversationConfig) *Conversation
- func (c *Client) RegisterProvider(provider types.Provider) error
- func (c *Client) Stream(ctx context.Context, req *types.CompletionRequest, ...) error
- type ClientConfig
- type CompletionFunc
- type Conversation
- func (c *Conversation) AddAssistantMessage(text string) error
- func (c *Conversation) AddMessage(message *types.Message) error
- func (c *Conversation) AddSystemMessage(text string) error
- func (c *Conversation) AddUserMessage(text string) error
- func (c *Conversation) Clear()
- func (c *Conversation) Clone() *Conversation
- func (c *Conversation) EstimateTokens(ctx context.Context, model string) (int, error)
- func (c *Conversation) Export() map[string]interface{}
- func (c *Conversation) GetLastMessage() *types.Message
- func (c *Conversation) GetMessages() []*types.Message
- func (c *Conversation) GetMessagesByRole(role types.Role) []*types.Message
- func (c *Conversation) GetTokenCount() int
- func (c *Conversation) Send(ctx context.Context, userMessage string, model string) (*types.CompletionResponse, error)
- func (c *Conversation) SendStream(ctx context.Context, userMessage string, model string, ...) error
- func (c *Conversation) TruncateToFit(ctx context.Context, model string, preserveSystem bool) error
- type ConversationConfig
- type GoogleOption
- type LoggingMiddleware
- type Middleware
- type OpenAIOption
- type ReplicateOption
- type RetryConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsQuotaExceededError ¶ added in v1.2.4
IsQuotaExceededError checks if an error indicates quota has been exceeded
func IsRetryableError ¶ added in v1.2.4
IsRetryableError determines if an error is worth retrying
func ParseRateLimitDelay ¶ added in v1.2.4
ParseRateLimitDelay extracts the suggested retry delay from rate limit errors Looks for patterns like "Please retry in 34.42245165s"
func TestBuilder ¶ added in v1.2.0
func TestConversationConfig ¶ added in v1.2.0
func TestConversationMessages ¶ added in v1.2.0
func TestMessage ¶ added in v1.2.0
func TestModelRegistry ¶ added in v1.2.0
func WithRetry ¶ added in v1.2.4
func WithRetry(ctx context.Context, req *types.CompletionRequest, config *RetryConfig, fn CompletionFunc) (*types.CompletionResponse, error)
WithRetry executes a completion request with smart retry logic - Parses rate limit errors for suggested retry delays - Uses exponential backoff for other transient errors - Skips retries for non-retryable errors (auth, invalid request) - Falls back through models in FallbackModels on quota errors (if provided)
Types ¶
type AIClient ¶ added in v1.2.0
type AIClient struct {
// contains filtered or unexported fields
}
AIClient provides a fluent interface for configuring and creating a Client
func (*AIClient) WithDefaultMaxTokens ¶ added in v1.2.0
WithDefaultMaxTokens sets the default max tokens
func (*AIClient) WithDefaultModel ¶ added in v1.2.0
WithDefaultModel sets the default model
func (*AIClient) WithDefaultProvider ¶ added in v1.2.0
WithDefaultProvider sets the default provider
func (*AIClient) WithDefaultTemperature ¶ added in v1.2.0
WithDefaultTemperature sets the default temperature
func (*AIClient) WithGoogle ¶ added in v1.2.0
func (b *AIClient) WithGoogle(apiKey, projectID string, options ...GoogleOption) *AIClient
WithGoogle configures Google AI provider
func (*AIClient) WithMiddleware ¶ added in v1.2.0
func (b *AIClient) WithMiddleware(middleware ...Middleware) *AIClient
WithMiddleware adds middleware to the client
func (*AIClient) WithOpenAI ¶ added in v1.2.0
func (b *AIClient) WithOpenAI(apiKey string, options ...OpenAIOption) *AIClient
WithOpenAI configures OpenAI provider
func (*AIClient) WithReplicate ¶ added in v1.2.0
func (b *AIClient) WithReplicate(apiKey string, options ...ReplicateOption) *AIClient
WithReplicate configures Replicate provider
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the main interface for interacting with AI providers
func NewClient ¶ added in v1.2.0
func NewClient(config *ClientConfig) *Client
NewClient creates a new AI client
func NewReplicate ¶ added in v1.2.0
func (*Client) Complete ¶ added in v1.2.0
func (c *Client) Complete(ctx context.Context, req *types.CompletionRequest) (*types.CompletionResponse, error)
Complete performs a completion request
func (*Client) EstimateTokens ¶ added in v1.2.0
func (c *Client) EstimateTokens(ctx context.Context, messages []*types.Message, model string) (int, error)
EstimateTokens estimates token count for messages and model
func (*Client) GetProvider ¶ added in v1.2.0
GetProvider returns a provider by name
func (*Client) ListModels ¶
ListModels returns all available models
func (*Client) ListModelsByProvider ¶ added in v1.2.0
ListModelsByProvider returns models for a specific provider
func (*Client) NewConversation ¶ added in v1.2.0
func (c *Client) NewConversation(config *ConversationConfig) *Conversation
NewConversation creates a new conversation with optional system prompt
func (*Client) RegisterProvider ¶ added in v1.2.0
RegisterProvider registers a new provider with the client
func (*Client) Stream ¶ added in v1.2.0
func (c *Client) Stream(ctx context.Context, req *types.CompletionRequest, callback types.StreamCallback) error
Stream performs a streaming completion request
type ClientConfig ¶ added in v1.0.0
type ClientConfig struct {
DefaultProvider string `json:"default_provider,omitempty"`
DefaultModel string `json:"default_model,omitempty"`
DefaultMaxTokens int `json:"default_max_tokens,omitempty"`
DefaultTemperature float64 `json:"default_temperature,omitempty"`
ProviderConfigs map[string]types.Config `json:"provider_configs,omitempty"`
Middleware []Middleware `json:"-"`
}
ClientConfig holds global client configuration
type CompletionFunc ¶ added in v1.2.4
type CompletionFunc func(ctx context.Context, req *types.CompletionRequest) (*types.CompletionResponse, error)
CompletionFunc is the function signature for AI completion calls
type Conversation ¶
type Conversation struct {
ID string `json:"id"`
Messages []*types.Message `json:"messages"`
MaxTokens int `json:"max_tokens"`
CurrentTokens int `json:"current_tokens"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
// contains filtered or unexported fields
}
Conversation represents a conversation with message history and management
func (*Conversation) AddAssistantMessage ¶ added in v1.2.0
func (c *Conversation) AddAssistantMessage(text string) error
AddAssistantMessage adds an assistant message to the conversation
func (*Conversation) AddMessage ¶ added in v1.2.0
func (c *Conversation) AddMessage(message *types.Message) error
AddMessage adds a message to the conversation
func (*Conversation) AddSystemMessage ¶ added in v1.2.0
func (c *Conversation) AddSystemMessage(text string) error
AddSystemMessage adds a system message to the conversation
func (*Conversation) AddUserMessage ¶ added in v1.2.0
func (c *Conversation) AddUserMessage(text string) error
AddUserMessage adds a user message to the conversation
func (*Conversation) Clear ¶ added in v1.2.0
func (c *Conversation) Clear()
Clear removes all messages from the conversation
func (*Conversation) Clone ¶ added in v1.2.0
func (c *Conversation) Clone() *Conversation
Clone creates a copy of the conversation
func (*Conversation) EstimateTokens ¶ added in v1.2.0
EstimateTokens estimates the current token count of the conversation
func (*Conversation) Export ¶ added in v1.2.0
func (c *Conversation) Export() map[string]interface{}
Export exports the conversation to a JSON-serializable format
func (*Conversation) GetLastMessage ¶ added in v1.2.0
func (c *Conversation) GetLastMessage() *types.Message
GetLastMessage returns the last message in the conversation
func (*Conversation) GetMessages ¶ added in v1.2.0
func (c *Conversation) GetMessages() []*types.Message
GetMessages returns a copy of all messages
func (*Conversation) GetMessagesByRole ¶ added in v1.2.0
func (c *Conversation) GetMessagesByRole(role types.Role) []*types.Message
GetMessagesByRole returns messages filtered by role
func (*Conversation) GetTokenCount ¶ added in v1.2.0
func (c *Conversation) GetTokenCount() int
GetTokenCount returns the last estimated token count
func (*Conversation) Send ¶ added in v1.2.0
func (c *Conversation) Send(ctx context.Context, userMessage string, model string) (*types.CompletionResponse, error)
Send sends a user message and gets a response
func (*Conversation) SendStream ¶ added in v1.2.0
func (c *Conversation) SendStream(ctx context.Context, userMessage string, model string, callback types.StreamCallback) error
SendStream sends a user message and streams the response
func (*Conversation) TruncateToFit ¶ added in v1.2.0
TruncateToFit ensures the conversation fits within token limits
type ConversationConfig ¶ added in v1.2.0
type ConversationConfig struct {
SystemPrompt string `json:"system_prompt,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Model string `json:"model,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
AutoTruncate bool `json:"auto_truncate,omitempty"`
PreserveSystem bool `json:"preserve_system,omitempty"` // Keep system message when truncating
}
ConversationConfig holds configuration for creating a conversation
type GoogleOption ¶ added in v1.2.0
GoogleOption configures Google AI-specific settings
func WithGoogleBaseURL ¶ added in v1.2.0
func WithGoogleBaseURL(baseURL string) GoogleOption
WithGoogleBaseURL sets a custom base URL for Google AI
func WithGoogleLocation ¶ added in v1.2.0
func WithGoogleLocation(location string) GoogleOption
WithGoogleLocation sets the Google Cloud location
type LoggingMiddleware ¶ added in v1.2.0
type LoggingMiddleware struct{}
LoggingMiddleware is an example middleware that logs requests and responses
func (*LoggingMiddleware) ProcessRequest ¶ added in v1.2.0
func (m *LoggingMiddleware) ProcessRequest(ctx context.Context, req *types.CompletionRequest) (*types.CompletionRequest, error)
func (*LoggingMiddleware) ProcessResponse ¶ added in v1.2.0
func (m *LoggingMiddleware) ProcessResponse(ctx context.Context, resp *types.CompletionResponse) (*types.CompletionResponse, error)
type Middleware ¶ added in v1.2.0
type Middleware interface {
ProcessRequest(ctx context.Context, req *types.CompletionRequest) (*types.CompletionRequest, error)
ProcessResponse(ctx context.Context, resp *types.CompletionResponse) (*types.CompletionResponse, error)
}
Middleware defines the interface for request/response middleware
type OpenAIOption ¶ added in v1.2.0
OpenAIOption configures OpenAI-specific settings
func WithOpenAIBaseURL ¶ added in v1.2.0
func WithOpenAIBaseURL(baseURL string) OpenAIOption
WithOpenAIBaseURL sets a custom base URL for OpenAI
func WithOpenAIOrg ¶ added in v1.2.0
func WithOpenAIOrg(orgID string) OpenAIOption
WithOpenAIOrg sets the OpenAI organization ID
func WithOpenAIProject ¶ added in v1.2.0
func WithOpenAIProject(project string) OpenAIOption
WithOpenAIProject sets the OpenAI project
func WithOpenAIUser ¶ added in v1.0.0
func WithOpenAIUser(user string) OpenAIOption
WithOpenAIUser sets the user identifier for OpenAI
type ReplicateOption ¶ added in v1.2.0
ReplicateOption configures Replicate-specific settings
func WithReplicateBaseURL ¶ added in v1.2.0
func WithReplicateBaseURL(baseURL string) ReplicateOption
WithReplicateBaseURL sets a custom base URL for Replicate
func WithReplicateExtraInput ¶ added in v1.2.0
func WithReplicateExtraInput(key string, value interface{}) ReplicateOption
WithReplicateExtraInput adds extra input parameters for Replicate
func WithReplicateWebhook ¶ added in v1.0.0
func WithReplicateWebhook(webhookURL string) ReplicateOption
WithReplicateWebhook sets the webhook URL for Replicate
type RetryConfig ¶ added in v1.2.4
type RetryConfig struct {
MaxAttempts int // Maximum number of retry attempts (default: 5)
BaseDelay time.Duration // Initial delay for exponential backoff (default: 2s)
MaxDelay time.Duration // Maximum delay between retries (default: 30s)
FallbackModels []string // Models to try in order on quota errors (optional)
}
RetryConfig holds configuration for the retry logic
func DefaultRetryConfig ¶ added in v1.2.4
func DefaultRetryConfig() *RetryConfig
DefaultRetryConfig returns the default retry configuration