providers

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertMessagesToOpenAI added in v1.5.1

func ConvertMessagesToOpenAI(messages []Message) []openaiMessage

ConvertMessagesToOpenAI converts generic Message into native OpenAI format (string content or parts) Used internally by OpenAI provider

func ConvertTools added in v1.5.1

func ConvertTools(tools []Tool) []openaiTool

ConvertTools converts generic Tool into OpenAI format; shared by all OpenAI-compatible providers

func GetDefaultURL added in v1.5.2

func GetDefaultURL(name string) string

GetDefaultURL returns the default base URL for a provider. Returns empty string if the provider is not registered.

func IsBuiltinRegistered added in v1.5.2

func IsBuiltinRegistered(name string) bool

IsBuiltinRegistered checks if a provider is registered as a builtin.

func ListBuiltins added in v1.5.2

func ListBuiltins() []string

ListBuiltins returns a list of all registered builtin provider names.

func RegisterBuiltin added in v1.5.2

func RegisterBuiltin(name string, factory BuiltinFactory, defaultURL string)

RegisterBuiltin registers a builtin provider with its factory and default URL. This should be called in each provider's init() function.

Types

type AnthropicProvider

type AnthropicProvider struct {
	*BaseProvider
}

AnthropicProvider implements Anthropic Claude API integration

func NewAnthropic

func NewAnthropic(config ProviderConfig) *AnthropicProvider

NewAnthropic creates a new Anthropic provider

func (*AnthropicProvider) Chat

func (p *AnthropicProvider) Chat(ctx context.Context, req *Request) (*Response, error)

func (*AnthropicProvider) Models

func (p *AnthropicProvider) Models() []ModelInfo

func (*AnthropicProvider) Stream

func (p *AnthropicProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)

func (*AnthropicProvider) SupportsModel

func (p *AnthropicProvider) SupportsModel(model string) bool

type BaseProvider

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

BaseProvider provides common functionality for all providers

func NewBaseProvider

func NewBaseProvider(name string, config ProviderConfig) *BaseProvider

NewBaseProvider creates a new base provider with resilience

func (*BaseProvider) Config

func (p *BaseProvider) Config() ProviderConfig

func (*BaseProvider) HTTPClient

func (p *BaseProvider) HTTPClient() HTTPDoer

func (*BaseProvider) Name

func (p *BaseProvider) Name() string

func (*BaseProvider) ResilienceConfig

func (p *BaseProvider) ResilienceConfig() ResilienceConfig

func (*BaseProvider) Validate

func (p *BaseProvider) Validate() error

func (*BaseProvider) ValidateRequest

func (p *BaseProvider) ValidateRequest(req *Request) error

ValidateRequest validates common request parameters This should be called by all provider implementations before processing requests

type BedrockProvider added in v1.5.2

type BedrockProvider struct {
	*BaseProvider
	// contains filtered or unexported fields
}

func NewBedrock added in v1.5.2

func NewBedrock(config ProviderConfig) *BedrockProvider

func (*BedrockProvider) Chat added in v1.5.2

func (p *BedrockProvider) Chat(ctx context.Context, req *Request) (*Response, error)

func (*BedrockProvider) Models added in v1.5.2

func (p *BedrockProvider) Models() []ModelInfo

func (*BedrockProvider) Stream added in v1.5.2

func (p *BedrockProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)

func (*BedrockProvider) SupportsModel added in v1.5.2

func (p *BedrockProvider) SupportsModel(model string) bool

func (*BedrockProvider) Validate added in v1.5.2

func (p *BedrockProvider) Validate() error

type BuiltinFactory added in v1.5.2

type BuiltinFactory func(ProviderConfig) Provider

BuiltinFactory is a function that creates a Provider instance

func GetBuiltin added in v1.5.2

func GetBuiltin(name string) (BuiltinFactory, bool)

GetBuiltin returns the factory function for a builtin provider. Returns nil and false if the provider is not registered.

type CacheControl

type CacheControl struct {
	Type string // "ephemeral" or "persistent"
	TTL  *int   // Time to live in seconds (optional)
}

CacheControl defines prompt caching behavior for providers

type DeepSeekProvider

type DeepSeekProvider struct {
	*BaseProvider
}

DeepSeekProvider implements DeepSeek API integration

func NewDeepSeek

func NewDeepSeek(config ProviderConfig) *DeepSeekProvider

NewDeepSeek creates a new DeepSeek provider

func (*DeepSeekProvider) Chat

func (p *DeepSeekProvider) Chat(ctx context.Context, req *Request) (*Response, error)

func (*DeepSeekProvider) Models

func (p *DeepSeekProvider) Models() []ModelInfo

func (*DeepSeekProvider) Stream

func (p *DeepSeekProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)

func (*DeepSeekProvider) SupportsModel

func (p *DeepSeekProvider) SupportsModel(model string) bool

type FunctionCall

type FunctionCall struct {
	Name      string
	Arguments string
}

type FunctionDef

type FunctionDef struct {
	Name        string
	Description string
	Parameters  any
}

type GLMProvider

type GLMProvider struct {
	*BaseProvider
}

GLMProvider implements GLM/ZhiPu AI API integration

func NewGLM

func NewGLM(config ProviderConfig) *GLMProvider

NewGLM creates a new GLM provider

func (*GLMProvider) Chat

func (p *GLMProvider) Chat(ctx context.Context, req *Request) (*Response, error)

func (*GLMProvider) Models

func (p *GLMProvider) Models() []ModelInfo

func (*GLMProvider) Stream

func (p *GLMProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)

func (*GLMProvider) SupportsModel

func (p *GLMProvider) SupportsModel(model string) bool

type GeminiProvider

type GeminiProvider struct {
	*BaseProvider
}

GeminiProvider implements Google Gemini API integration

func NewGemini

func NewGemini(config ProviderConfig) *GeminiProvider

NewGemini creates a new Gemini provider

func (*GeminiProvider) Chat

func (p *GeminiProvider) Chat(ctx context.Context, req *Request) (*Response, error)

func (*GeminiProvider) Models

func (p *GeminiProvider) Models() []ModelInfo

func (*GeminiProvider) Stream

func (p *GeminiProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)

func (*GeminiProvider) SupportsModel

func (p *GeminiProvider) SupportsModel(model string) bool

type HTTPDoer

type HTTPDoer interface {
	Do(req *http.Request) (*http.Response, error)
}

type JSONSchema

type JSONSchema struct {
	Name        string
	Description string
	Schema      any
	Strict      *bool
}

type Message

type Message struct {
	Role         string
	Content      string
	Contents     []MessageContent
	ToolCalls    []ToolCall
	ToolCallID   string
	CacheControl *CacheControl
}

type MessageContent added in v1.5.1

type MessageContent struct {
	Type        string
	Text        string
	ImageURL    *MessageImageURL
	Annotations []map[string]interface{}
	Logprobs    []map[string]interface{}
}

type MessageImageURL added in v1.5.1

type MessageImageURL struct {
	URL string
}

type ModelInfo

type ModelInfo struct {
	ID              string
	Provider        string
	Name            string
	ContextWindow   int
	MaxOutputTokens int
	Capabilities    []string
}

type OpenAICompatMessage added in v1.5.1

type OpenAICompatMessage struct {
	Role             string           `json:"role"`
	Content          interface{}      `json:"content,omitempty"` // string or array (needed by OpenRouter)
	ToolCalls        []openaiToolCall `json:"tool_calls,omitempty"`
	ToolCallID       string           `json:"tool_call_id,omitempty"`
	ReasoningContent string           `json:"reasoning_content,omitempty"` // used by GLM/Qwen
}

OpenAICompatMessage is a generic OpenAI-compatible message format Used by DeepSeek/GLM/Qwen etc.; Content stays interface{} to allow flexible formats

func ConvertMessages added in v1.5.1

func ConvertMessages(messages []Message) []OpenAICompatMessage

ConvertMessages converts generic Message into OpenAI-compatible format (Content as interface{}) For DeepSeek/GLM/Qwen/OpenRouter etc.

type OpenAIProvider

type OpenAIProvider struct {
	*BaseProvider
}

OpenAIProvider implements OpenAI API integration

func NewOpenAI

func NewOpenAI(config ProviderConfig) *OpenAIProvider

NewOpenAI creates a new OpenAI provider

func (*OpenAIProvider) Chat

func (p *OpenAIProvider) Chat(ctx context.Context, req *Request) (*Response, error)

func (*OpenAIProvider) Models

func (p *OpenAIProvider) Models() []ModelInfo

func (*OpenAIProvider) Stream

func (p *OpenAIProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)

func (*OpenAIProvider) SupportsModel

func (p *OpenAIProvider) SupportsModel(model string) bool

type OpenRouterProvider

type OpenRouterProvider struct {
	*BaseProvider
}

OpenRouterProvider implements OpenRouter API integration

func NewOpenRouter

func NewOpenRouter(config ProviderConfig) *OpenRouterProvider

NewOpenRouter creates a new OpenRouter provider

func (*OpenRouterProvider) Chat

func (p *OpenRouterProvider) Chat(ctx context.Context, req *Request) (*Response, error)

func (*OpenRouterProvider) Models

func (p *OpenRouterProvider) Models() []ModelInfo

func (*OpenRouterProvider) Stream

func (p *OpenRouterProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)

func (*OpenRouterProvider) SupportsModel

func (p *OpenRouterProvider) SupportsModel(model string) bool

type Provider

type Provider interface {
	Name() string
	Validate() error
	SupportsModel(model string) bool
	Models() []ModelInfo
	Chat(ctx context.Context, req *Request) (*Response, error)
	Stream(ctx context.Context, req *Request) (StreamReader, error)
}

Provider interface

type ProviderConfig

type ProviderConfig struct {
	APIKey     string           `json:"api_key"`
	BaseURL    string           `json:"base_url,omitempty"`
	Timeout    time.Duration    `json:"timeout,omitempty"`
	Extra      map[string]any   `json:"extra,omitempty"`
	Resilience ResilienceConfig `json:"resilience,omitempty"`
	HTTPClient HTTPDoer         `json:"-"`
}

ProviderConfig holds configuration for a provider

type QwenProvider

type QwenProvider struct {
	*BaseProvider
}

QwenProvider implements Qwen/DashScope API integration

func NewQwen

func NewQwen(config ProviderConfig) *QwenProvider

NewQwen creates a new Qwen provider

func (*QwenProvider) Chat

func (p *QwenProvider) Chat(ctx context.Context, req *Request) (*Response, error)

func (*QwenProvider) Models

func (p *QwenProvider) Models() []ModelInfo

func (*QwenProvider) Stream

func (p *QwenProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)

func (*QwenProvider) SupportsModel

func (p *QwenProvider) SupportsModel(model string) bool

type ReasoningChunk

type ReasoningChunk struct {
	Summary string
	Content string
}

type ReasoningData

type ReasoningData struct {
	Summary    string
	Content    string
	TokensUsed int
}

type Request

type Request struct {
	Model             string
	Messages          []Message
	MaxTokens         *int
	Temperature       *float64
	TopP              *float64
	TopLogProbs       *int
	Stream            bool
	Tools             []Tool
	ToolChoice        any
	ResponseFormat    *ResponseFormat
	Stop              []string
	ReasoningEffort   string
	ReasoningSummary  string
	UseResponsesAPI   bool
	ServiceTier       string
	Store             *bool
	ParallelToolCalls *bool
	SafetyIdentifier  string

	ResponsesParams *ResponsesParams

	// Provider-specific extensions
	Extra map[string]interface{}
}

type ResilienceConfig

type ResilienceConfig struct {
	MaxRetries     int           `json:"max_retries"`
	InitialDelay   time.Duration `json:"initial_delay"`
	MaxDelay       time.Duration `json:"max_delay"`
	Multiplier     float64       `json:"multiplier"`
	Jitter         bool          `json:"jitter"`
	RequestTimeout time.Duration `json:"request_timeout"`
	ConnectTimeout time.Duration `json:"connect_timeout"`
}

ResilienceConfig holds network resilience configuration for providers

func DefaultResilienceConfig

func DefaultResilienceConfig() ResilienceConfig

DefaultResilienceConfig returns default resilience configuration for providers

type Response

type Response struct {
	Content      string
	Contents     []MessageContent
	ToolCalls    []ToolCall
	Usage        Usage
	Model        string
	Provider     string
	FinishReason string
	Reasoning    *ReasoningData
}

type ResponseFormat

type ResponseFormat struct {
	Type       string
	JSONSchema *JSONSchema
}

type ResponsesParams added in v1.5.1

type ResponsesParams struct {
	Instructions         string
	Conversation         string
	PreviousResponseID   string
	Metadata             map[string]string
	Store                *bool
	MaxOutputTokens      *int
	MaxInputTokens       *int
	MaxToolCalls         *int
	ParallelToolCalls    *bool
	Include              []string
	SafetyIdentifier     string
	ServiceTier          string
	Temperature          *float64
	TopP                 *float64
	ToolChoice           any
	ResponseFormat       *ResponseFormat
	PromptCacheKey       string
	PromptCacheRetention string
	Background           *bool
	Prompt               map[string]interface{}
	ModelOverride        string
}

ResponsesParams describes Response-API-specific or more granular controls

type StreamChunk

type StreamChunk struct {
	Type          string
	Content       string
	ContentIndex  *int
	OutputIndex   *int
	ItemID        string
	ToolCallDelta *ToolCallDelta
	FinishReason  string
	Model         string
	Provider      string
	Done          bool
	Reasoning     *ReasoningChunk
	Usage         *Usage
}

type StreamReader

type StreamReader interface {
	Next() (*StreamChunk, error)
	Close() error
}

type Tool

type Tool struct {
	Type     string
	Function FunctionDef
}

type ToolCall

type ToolCall struct {
	ID       string
	Type     string
	Function FunctionCall
}

type ToolCallDelta

type ToolCallDelta struct {
	Index          int
	ID             string
	Type           string
	FunctionName   string
	ArgumentsDelta string
	OutputIndex    *int
	ItemID         string
}

type Usage

type Usage struct {
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
	ReasoningTokens  int

	// Cache-related token statistics
	CacheCreationInputTokens int // Tokens written to cache
	CacheReadInputTokens     int // Tokens read from cache
}

Jump to

Keyboard shortcuts

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