Documentation
¶
Overview ¶
Package provider composes LLM provider clients for Anthropic, OpenAI, and OpenAI-compatible APIs with streaming, retry, and provider detection.
Index ¶
- Variables
- func DetectProvider() string
- func NewAudioMessage(base64Data, format string) core.MuxMessage
- func NewAudioMessageWithText(text, base64Data, format string) core.MuxMessage
- func NewBase64ImageMessage(data, mediaType string) core.MuxMessage
- func NewBase64ImageMessageWithText(text, data, mediaType string) core.MuxMessage
- func NewImageMessage(url string) core.MuxMessage
- func NewImageMessageWithText(text, url string) core.MuxMessage
- func ParseCustomHeaders() map[string]string
- func ResolveDefaultModel(provider string) string
- func ResolveProviderModelEnvOverride(provider string) string
- func SetVersion(v string)
- func WithAPIKey(key string) core.ClientOption
- func WithBaseURL(url string) core.ClientOption
- func WithCoalescing(ttl time.Duration) core.ClientOption
- func WithGuardrailType(types ...core.GuardrailType) core.ClientOption
- func WithGuardrails(rules ...core.GuardrailRule) core.ClientOption
- func WithHTTPClient(hc *http.Client) core.ClientOption
- func WithLogger(l *slog.Logger) core.ClientOption
- func WithMaxTokens(n int) core.ClientOption
- func WithMimoAuth() core.ClientOption
- func WithModel(model string) core.ClientOption
- func WithProviderName(name string) core.ClientOption
- func WithRetry(rc core.RetryConfig) core.ClientOption
- func WithTemperature(t float64) core.ClientOption
- func WithTimeout(d time.Duration) core.ClientOption
- type AnthropicClientConfig
- type LazyProvider
- func (p *LazyProvider) Chat(ctx context.Context, messages []core.MuxMessage, opts core.ChatOptions) (*core.MuxResponse, error)
- func (p *LazyProvider) Name() string
- func (p *LazyProvider) Ping(ctx context.Context) error
- func (p *LazyProvider) SetAPIKey(provider, apiKey string)
- func (p *LazyProvider) StreamChat(ctx context.Context, messages []core.MuxMessage, opts core.ChatOptions) (*core.StreamResult, error)
- type MuxClient
- func (c *MuxClient) Chat(ctx context.Context, messages []core.MuxMessage, opts core.ChatOptions) (*core.MuxResponse, error)
- func (c *MuxClient) ChatWithStructuredOutput(ctx context.Context, messages []core.MuxMessage, opts core.ChatOptions, ...) (*core.MuxResponse, error)
- func (c *MuxClient) CreateEmbedding(ctx context.Context, req embeddings.EmbeddingRequest, provider string) (*embeddings.EmbeddingResponse, error)
- func (c *MuxClient) ExtractRelationships(ctx context.Context, text string, opts extraction.ExtractOptions) ([]extraction.Relationship, error)
- func (c *MuxClient) GetProviderInfo(provider string) *adapters.ProviderRegistryConfig
- func (c *MuxClient) GetProviders() []string
- func (c *MuxClient) Ping(ctx context.Context, provider string) error
- func (c *MuxClient) RegisterCustomProvider(name, baseURL, envKey string) error
- func (c *MuxClient) SetAPIKey(provider, apiKey string)
- func (c *MuxClient) SetCoalescingTTL(ttl time.Duration)
- func (c *MuxClient) StreamChat(ctx context.Context, messages []core.MuxMessage, opts core.ChatOptions) (*core.StreamResult, error)
- func (c *MuxClient) StreamChatContinue(ctx context.Context, messages []core.MuxMessage, opts core.ChatOptions, ...) (*core.StreamResult, error)
Constants ¶
This section is empty.
Variables ¶
var Version = buildversion.Version
Version mirrors core.Version; the canonical value lives in provider/core so feature packages can build User-Agent strings. Default is generated from the canonical VERSION file.
Functions ¶
func DetectProvider ¶
func DetectProvider() string
DetectProvider detects the active provider from the credential store.
func NewAudioMessage ¶
func NewAudioMessage(base64Data, format string) core.MuxMessage
NewAudioMessage creates a user message with base64-encoded audio. format should be "wav" or "mp3".
func NewAudioMessageWithText ¶
func NewAudioMessageWithText(text, base64Data, format string) core.MuxMessage
NewAudioMessageWithText creates a user message with text and base64-encoded audio.
func NewBase64ImageMessage ¶
func NewBase64ImageMessage(data, mediaType string) core.MuxMessage
NewBase64ImageMessage creates a user message with a base64-encoded image. mediaType should be a MIME type like "image/png" or "image/jpeg".
func NewBase64ImageMessageWithText ¶
func NewBase64ImageMessageWithText(text, data, mediaType string) core.MuxMessage
NewBase64ImageMessageWithText creates a user message with text and a base64-encoded image.
func NewImageMessage ¶
func NewImageMessage(url string) core.MuxMessage
NewImageMessage creates a user message with an image from a URL or data URI. The url parameter accepts HTTP(S) URLs or data URIs (data:image/png;base64,...).
func NewImageMessageWithText ¶
func NewImageMessageWithText(text, url string) core.MuxMessage
NewImageMessageWithText creates a user message with text and an image from a URL or data URI.
func ParseCustomHeaders ¶
ParseCustomHeaders parses MUX_CUSTOM_HEADERS env var into a map.
func ResolveDefaultModel ¶
ResolveDefaultModel resolves the default model for a provider from the catalog.
func ResolveProviderModelEnvOverride ¶
ResolveProviderModelEnvOverride resolves the model env override for a provider.
func SetVersion ¶
func SetVersion(v string)
SetVersion wires the canonical version from the VERSION file into the provider runtime and provider/core.
func WithCoalescing ¶
func WithCoalescing(ttl time.Duration) core.ClientOption
WithCoalescing enables request coalescing for identical concurrent requests. When enabled, multiple goroutines sending identical requests (same provider, model, messages, temperature, max_tokens) will be deduplicated into a single API call, with the result broadcast to all waiters.
The ttl parameter controls how long completed requests remain in the coalescer for potential reuse. A typical value is 100-500ms.
func WithGuardrailType ¶
func WithGuardrailType(types ...core.GuardrailType) core.ClientOption
WithGuardrailType attaches output guardrails using built-in rules for the specified types. For example, WithGuardrailType(GuardrailPII, GuardrailSecretLeak) enables PII redaction and secret leak blocking with default patterns.
func WithGuardrails ¶
func WithGuardrails(rules ...core.GuardrailRule) core.ClientOption
WithGuardrails attaches output guardrails to the provider. core.Guardrails run after the LLM response but before returning to the caller. Blocked responses are replaced with an error; redacted responses have matches replaced with asterisks.
func WithHTTPClient ¶
func WithHTTPClient(hc *http.Client) core.ClientOption
WithHTTPClient sets a custom HTTP provider.
func WithMaxTokens ¶
func WithMaxTokens(n int) core.ClientOption
WithMaxTokens sets the default max tokens for requests.
func WithMimoAuth ¶
func WithMimoAuth() core.ClientOption
WithMimoAuth uses api-key header per MiMo documentation (OpenAI + Anthropic compat).
func WithModel ¶
func WithModel(model string) core.ClientOption
WithModel sets the default model for requests.
func WithProviderName ¶
func WithProviderName(name string) core.ClientOption
WithProviderName sets the OpenAI client provider name for errors/logging. No-op for the Anthropic adapter, which reports a fixed provider name.
func WithRetry ¶
func WithRetry(rc core.RetryConfig) core.ClientOption
WithRetry sets retry configuration.
func WithTemperature ¶
func WithTemperature(t float64) core.ClientOption
WithTemperature sets the default temperature for requests.
func WithTimeout ¶
func WithTimeout(d time.Duration) core.ClientOption
WithTimeout sets the HTTP client timeout.
Types ¶
type AnthropicClientConfig ¶
type AnthropicClientConfig struct {
APIKey string `json:"-"`
DefaultHeaders map[string]string `json:"default_headers,omitempty"`
Timeout int `json:"timeout,omitempty"`
MaxRetries int `json:"max_retries,omitempty"`
Provider string `json:"provider,omitempty"`
BaseURL string `json:"base_url,omitempty"`
}
AnthropicClientConfig holds config for creating an Anthropic provider.
type LazyProvider ¶
type LazyProvider struct {
// contains filtered or unexported fields
}
LazyProvider adapts MuxClient to the core.Provider interface without eagerly resolving credentials or constructing a concrete provider.
func NewLazyProvider ¶
func NewLazyProvider(cfg *core.MuxConfig) *LazyProvider
NewLazyProvider creates a provider wrapper that resolves the concrete provider only when chat or ping operations are invoked.
func (*LazyProvider) Chat ¶
func (p *LazyProvider) Chat(ctx context.Context, messages []core.MuxMessage, opts core.ChatOptions) (*core.MuxResponse, error)
func (*LazyProvider) Name ¶
func (p *LazyProvider) Name() string
func (*LazyProvider) SetAPIKey ¶
func (p *LazyProvider) SetAPIKey(provider, apiKey string)
func (*LazyProvider) StreamChat ¶
func (p *LazyProvider) StreamChat(ctx context.Context, messages []core.MuxMessage, opts core.ChatOptions) (*core.StreamResult, error)
type MuxClient ¶
type MuxClient struct {
// contains filtered or unexported fields
}
MuxClient is the universal LLM provider. It is safe for concurrent use.
func Client ¶
func Client(cfg *core.MuxConfig, opts ...core.ClientOption) *MuxClient
Client creates an MuxClient.
func (*MuxClient) Chat ¶
func (c *MuxClient) Chat(ctx context.Context, messages []core.MuxMessage, opts core.ChatOptions) (*core.MuxResponse, error)
Chat sends a chat request to the specified (or default) provider.
func (*MuxClient) ChatWithStructuredOutput ¶
func (c *MuxClient) ChatWithStructuredOutput(ctx context.Context, messages []core.MuxMessage, opts core.ChatOptions, validation core.SchemaValidation) (*core.MuxResponse, error)
ChatWithStructuredOutput validates structured JSON responses while retaining MuxClient's provider resolution and default-model behavior. The schema, validation, prompting, and retry implementation lives in provider/media.
func (*MuxClient) CreateEmbedding ¶
func (c *MuxClient) CreateEmbedding(ctx context.Context, req embeddings.EmbeddingRequest, provider string) (*embeddings.EmbeddingResponse, error)
CreateEmbedding sends an embedding request to the specified (or default) provider.
func (*MuxClient) ExtractRelationships ¶
func (c *MuxClient) ExtractRelationships(ctx context.Context, text string, opts extraction.ExtractOptions) ([]extraction.Relationship, error)
ExtractRelationships delegates relationship extraction to the extraction feature while retaining MuxClient's public convenience method.
func (*MuxClient) GetProviderInfo ¶
func (c *MuxClient) GetProviderInfo(provider string) *adapters.ProviderRegistryConfig
GetProviderInfo returns config for a provider.
func (*MuxClient) GetProviders ¶
GetProviders lists all available providers.
func (*MuxClient) RegisterCustomProvider ¶
RegisterCustomProvider adds an OpenAI-compatible endpoint to this client only. Re-registration replaces the endpoint and invalidates its cached transport. Other clients are unaffected.
func (*MuxClient) SetCoalescingTTL ¶
SetCoalescingTTL enables request coalescing with the given reuse TTL. Implements core.MuxConfigurable for WithCoalescing.
func (*MuxClient) StreamChat ¶
func (c *MuxClient) StreamChat(ctx context.Context, messages []core.MuxMessage, opts core.ChatOptions) (*core.StreamResult, error)
StreamChat sends a streaming chat request.
func (*MuxClient) StreamChatContinue ¶
func (c *MuxClient) StreamChatContinue(ctx context.Context, messages []core.MuxMessage, opts core.ChatOptions, cfg core.ContinuationConfig) (*core.StreamResult, error)
StreamChatContinue is like StreamChat but automatically continues if the response hits max_tokens with text-only content. Continuations are transparent to the caller.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package core holds the provider contract and the data types shared by every layer of the mux client: adapters, middleware, caching, embeddings, and the provider runtime itself.
|
Package core holds the provider contract and the data types shared by every layer of the mux client: adapters, middleware, caching, embeddings, and the provider runtime itself. |
|
Package client provides request coalescing for identical concurrent LLM requests.
|
Package client provides request coalescing for identical concurrent LLM requests. |