Documentation
¶
Overview ¶
Package factory provides a centralized factory for creating LLM Provider instances by name. It imports all provider sub-packages and maps string names to their constructors, breaking the import cycle that would occur if this logic lived in the llm package directly.
Index ¶
- func NewProviderFromConfig(name string, cfg ProviderConfig, logger *zap.Logger) (llm.Provider, error)
- func NewRegistryFromConfig(cfg RegistryConfig, logger *zap.Logger) (*llm.ProviderRegistry, error)
- func SupportedProviders() []string
- func WrapWithRetry(provider llm.Provider, policy *retry.RetryPolicy, logger *zap.Logger) llm.Provider
- type ProviderConfig
- type RegistryConfig
- type RetryProvider
- func (p *RetryProvider) Completion(ctx context.Context, req *llm.ChatRequest) (*llm.ChatResponse, error)
- func (p *RetryProvider) Endpoints() llm.ProviderEndpoints
- func (p *RetryProvider) HealthCheck(ctx context.Context) (*llm.HealthStatus, error)
- func (p *RetryProvider) ListModels(ctx context.Context) ([]llm.Model, error)
- func (p *RetryProvider) Name() string
- func (p *RetryProvider) Stream(ctx context.Context, req *llm.ChatRequest) (<-chan llm.StreamChunk, error)
- func (p *RetryProvider) SupportsNativeFunctionCalling() bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewProviderFromConfig ¶
func NewProviderFromConfig(name string, cfg ProviderConfig, logger *zap.Logger) (llm.Provider, error)
NewProviderFromConfig creates a Provider instance based on the provider name and a generic ProviderConfig. It maps the name to the appropriate constructor.
Supported names: openai, anthropic, claude, gemini, deepseek, qwen, glm, grok, kimi, mistral, minimax, hunyuan, doubao, llama.
func NewRegistryFromConfig ¶
func NewRegistryFromConfig(cfg RegistryConfig, logger *zap.Logger) (*llm.ProviderRegistry, error)
NewRegistryFromConfig creates a ProviderRegistry populated with all providers defined in the RegistryConfig. It sets the default provider if specified. Any provider that fails to initialize is logged as a warning and skipped.
func SupportedProviders ¶
func SupportedProviders() []string
SupportedProviders returns the list of built-in provider names. Any name not in this list will be treated as a generic OpenAI-compatible provider, requiring base_url in the configuration.
func WrapWithRetry ¶ added in v1.2.0
func WrapWithRetry(provider llm.Provider, policy *retry.RetryPolicy, logger *zap.Logger) llm.Provider
WrapWithRetry wraps an existing Provider with retry logic. If policy is nil, DefaultRetryPolicy is used.
Types ¶
type ProviderConfig ¶
type ProviderConfig struct {
APIKey string `json:"api_key" yaml:"api_key"`
APIKeys []providers.APIKeyEntry `json:"api_keys,omitempty" yaml:"api_keys,omitempty"`
BaseURL string `json:"base_url" yaml:"base_url"`
Model string `json:"model,omitempty" yaml:"model,omitempty"`
Timeout time.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
Extra map[string]any `json:"extra,omitempty" yaml:"extra,omitempty"`
}
ProviderConfig is the generic configuration accepted by the factory function. It uses a flat structure with an Extra map for provider-specific fields.
type RegistryConfig ¶
type RegistryConfig struct {
// Default is the name of the default provider (must match a key in Providers).
Default string `json:"default" yaml:"default"`
// Providers maps provider names to their configurations.
Providers map[string]ProviderConfig `json:"providers" yaml:"providers"`
}
RegistryConfig describes multiple providers and which one is the default. Use this with NewRegistryFromConfig to build a ProviderRegistry in one call.
type RetryProvider ¶ added in v1.2.0
type RetryProvider struct {
// contains filtered or unexported fields
}
RetryProvider wraps an llm.Provider with automatic retry using exponential backoff. Completion and Stream (connection establishment only) are retried; HealthCheck and ListModels delegate directly.
func (*RetryProvider) Completion ¶ added in v1.2.0
func (p *RetryProvider) Completion(ctx context.Context, req *llm.ChatRequest) (*llm.ChatResponse, error)
Completion wraps the inner Completion with retry.
func (*RetryProvider) Endpoints ¶ added in v1.2.0
func (p *RetryProvider) Endpoints() llm.ProviderEndpoints
Endpoints delegates to the inner provider.
func (*RetryProvider) HealthCheck ¶ added in v1.2.0
func (p *RetryProvider) HealthCheck(ctx context.Context) (*llm.HealthStatus, error)
HealthCheck delegates to the inner provider.
func (*RetryProvider) ListModels ¶ added in v1.2.0
ListModels delegates to the inner provider.
func (*RetryProvider) Name ¶ added in v1.2.0
func (p *RetryProvider) Name() string
Name delegates to the inner provider.
func (*RetryProvider) Stream ¶ added in v1.2.0
func (p *RetryProvider) Stream(ctx context.Context, req *llm.ChatRequest) (<-chan llm.StreamChunk, error)
Stream wraps the inner Stream with retry for connection establishment.
func (*RetryProvider) SupportsNativeFunctionCalling ¶ added in v1.2.0
func (p *RetryProvider) SupportsNativeFunctionCalling() bool
SupportsNativeFunctionCalling delegates to the inner provider.