Documentation
¶
Index ¶
- func PurposeFrom(ctx context.Context) string
- func WithPurpose(ctx context.Context, purpose string) context.Context
- type AnthropicConfig
- type AnthropicProvider
- type Config
- type ErrInvalidResponse
- type ErrMaxTokensExceeded
- type ErrProviderUnavailable
- type ErrRateLimit
- type GeminiConfig
- type GeminiProvider
- type LoggingProvider
- type Message
- type MockProvider
- type MockResponse
- type ModelCost
- type OpenAIConfig
- type OpenAIProvider
- type OpenRouterConfig
- type OpenRouterProvider
- type Provider
- func NewProvider(ctx context.Context, cfg Config, eventRepo store.EventRepo) (Provider, error)
- func NewProviderFromEnv(ctx context.Context, eventRepo store.EventRepo) (Provider, error)
- func WithLogging(p Provider, repo store.EventRepo, providerName string) Provider
- func WithRetry(p Provider, cfg RetryConfig) Provider
- type Request
- type Response
- type RetryConfig
- type RetryProvider
- type Role
- type Schema
- type Usage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PurposeFrom ¶
PurposeFrom extracts the purpose label from the context.
Types ¶
type AnthropicConfig ¶
AnthropicConfig holds Anthropic-specific configuration.
type AnthropicProvider ¶
type AnthropicProvider struct {
// contains filtered or unexported fields
}
AnthropicProvider implements Provider using the Anthropic SDK.
func NewAnthropicProvider ¶
func NewAnthropicProvider(cfg AnthropicConfig) (*AnthropicProvider, error)
NewAnthropicProvider creates a new Anthropic provider.
func (*AnthropicProvider) ModelID ¶
func (p *AnthropicProvider) ModelID() string
type Config ¶
type Config struct {
// Provider selects which LLM provider to use.
// Values: "anthropic", "openai", "gemini", "openrouter", "mock"
Provider string
Anthropic AnthropicConfig
OpenAI OpenAIConfig
Gemini GeminiConfig
OpenRouter OpenRouterConfig
Retry RetryConfig
// Timeout is the maximum duration for a single LLM request
// (including retries). Default: 30s.
Timeout time.Duration
}
Config holds all LLM provider configuration.
func ConfigFromEnv ¶
func ConfigFromEnv() Config
ConfigFromEnv builds a Config from environment variables, falling back to defaults for unset values.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible defaults.
func DiscoverConfig ¶
DiscoverConfig probes standard API key env vars in priority order (Gemini → OpenAI → Anthropic) and returns a Config for the first provider whose key is found. Returns (Config{}, false) if none found.
type ErrInvalidResponse ¶
type ErrInvalidResponse struct {
Content json.RawMessage
Err error
}
ErrInvalidResponse indicates the LLM returned content that does not conform to the requested schema.
func (*ErrInvalidResponse) Error ¶
func (e *ErrInvalidResponse) Error() string
func (*ErrInvalidResponse) Unwrap ¶
func (e *ErrInvalidResponse) Unwrap() error
type ErrMaxTokensExceeded ¶
type ErrMaxTokensExceeded struct {
Content json.RawMessage
}
ErrMaxTokensExceeded indicates the response was truncated because it hit the MaxTokens limit.
func (*ErrMaxTokensExceeded) Error ¶
func (e *ErrMaxTokensExceeded) Error() string
type ErrProviderUnavailable ¶
type ErrProviderUnavailable struct {
}
ErrProviderUnavailable indicates the provider is down or unreachable.
func (*ErrProviderUnavailable) Error ¶
func (e *ErrProviderUnavailable) Error() string
func (*ErrProviderUnavailable) Unwrap ¶
func (e *ErrProviderUnavailable) Unwrap() error
type ErrRateLimit ¶
ErrRateLimit indicates the provider returned a rate limit error (429).
func (*ErrRateLimit) Error ¶
func (e *ErrRateLimit) Error() string
func (*ErrRateLimit) Unwrap ¶
func (e *ErrRateLimit) Unwrap() error
type GeminiConfig ¶
GeminiConfig holds Gemini-specific configuration.
type GeminiProvider ¶
type GeminiProvider struct {
// contains filtered or unexported fields
}
GeminiProvider implements Provider using the Google Gemini SDK.
func NewGeminiProvider ¶
func NewGeminiProvider(ctx context.Context, cfg GeminiConfig) (*GeminiProvider, error)
NewGeminiProvider creates a new Gemini provider.
func (*GeminiProvider) ModelID ¶
func (p *GeminiProvider) ModelID() string
type LoggingProvider ¶
type LoggingProvider struct {
// contains filtered or unexported fields
}
LoggingProvider is a decorator that records every LLM request as an event.
func (*LoggingProvider) ModelID ¶
func (l *LoggingProvider) ModelID() string
type MockProvider ¶
type MockProvider struct {
Calls []Request
// contains filtered or unexported fields
}
MockProvider is a deterministic Provider for testing. It returns canned responses in FIFO order and records all requests.
func NewMockProvider ¶
func NewMockProvider(responses ...MockResponse) *MockProvider
NewMockProvider creates a MockProvider with the given canned responses.
func (*MockProvider) AddResponse ¶
func (m *MockProvider) AddResponse(resp MockResponse)
AddResponse appends a canned response to the queue.
func (*MockProvider) CallCount ¶
func (m *MockProvider) CallCount() int
CallCount returns the number of Generate calls made.
type MockResponse ¶
type MockResponse struct {
Content json.RawMessage
Usage Usage
Err error
}
MockResponse is a canned response for the MockProvider.
type ModelCost ¶
type ModelCost struct {
InputPerMTok float64 // USD per 1M input tokens
OutputPerMTok float64 // USD per 1M output tokens
}
ModelCost holds per-million-token pricing for a model. Prices are in USD per 1 million tokens, sourced from models.dev.
func LookupCost ¶
LookupCost returns the pricing for a model ID, or nil if unknown.
type OpenAIConfig ¶
type OpenAIConfig struct {
APIKey string
Model string // Default: "gpt-4o-mini"
BaseURL string // Optional. Override for OpenRouter or compatible APIs.
}
OpenAIConfig holds OpenAI-specific configuration.
type OpenAIProvider ¶
type OpenAIProvider struct {
// contains filtered or unexported fields
}
OpenAIProvider implements Provider using the OpenAI SDK. It also supports OpenRouter and other OpenAI-compatible APIs via BaseURL.
func NewOpenAIProvider ¶
func NewOpenAIProvider(cfg OpenAIConfig) (*OpenAIProvider, error)
NewOpenAIProvider creates a new OpenAI provider.
func (*OpenAIProvider) ModelID ¶
func (p *OpenAIProvider) ModelID() string
type OpenRouterConfig ¶ added in v0.0.4
type OpenRouterConfig struct {
APIKey string
Model string // Default: "google/gemini-2.0-flash-exp"
BaseURL string // Default: "https://openrouter.ai/api/v1"
}
OpenRouterConfig holds OpenRouter-specific configuration.
type OpenRouterProvider ¶ added in v0.0.4
type OpenRouterProvider struct {
*OpenAIProvider
}
OpenRouterProvider wraps OpenAIProvider with OpenRouter-specific defaults. OpenRouter exposes an OpenAI-compatible API, so the underlying SDK is reused.
func NewOpenRouterProvider ¶ added in v0.0.4
func NewOpenRouterProvider(cfg OpenRouterConfig) (*OpenRouterProvider, error)
NewOpenRouterProvider creates a provider targeting the OpenRouter API.
type Provider ¶
type Provider interface {
// Generate sends a prompt to the LLM and returns a structured response.
// The request's Schema field, when set, instructs the provider to return
// JSON conforming to that schema. The response Content will be the
// validated JSON.
Generate(ctx context.Context, req Request) (*Response, error)
// ModelID returns the model identifier this provider is configured to use.
ModelID() string
}
Provider is the core abstraction for LLM interaction. Consumers call Generate with a Request and receive structured JSON.
func NewProvider ¶
NewProvider creates a Provider from configuration. It returns the provider wrapped with retry and logging middleware.
func NewProviderFromEnv ¶
NewProviderFromEnv auto-discovers LLM credentials from standard env vars (GEMINI_API_KEY, OPENAI_API_KEY, ANTHROPIC_API_KEY) and creates a fully decorated provider. Returns an error if no credentials are found.
func WithLogging ¶
WithLogging wraps a Provider with event logging.
func WithRetry ¶
func WithRetry(p Provider, cfg RetryConfig) Provider
WithRetry wraps a Provider with retry logic.
type Request ¶
type Request struct {
// System is the system prompt. Sets the LLM's role and constraints.
System string
// Messages is the conversation history. For single-turn generation
// (the common case in Mathiz), this contains one user message.
Messages []Message
// Schema is the JSON Schema the response must conform to.
// When set, the provider uses its native structured output mechanism.
// When nil, the response Content is raw text as json.RawMessage.
Schema *Schema
// MaxTokens is the maximum number of tokens in the response.
MaxTokens int
// Temperature controls randomness. Range: 0.0 - 1.0.
// Default: 0.0 (deterministic) when not set.
Temperature float64
}
Request describes what to send to the LLM.
type Response ¶
type Response struct {
// Content is the generated output. When a Schema was provided in the
// request, this is the validated JSON object. When no Schema was
// provided, this is the raw text response wrapped as a JSON string.
Content json.RawMessage
// Usage reports token consumption for this request.
Usage Usage
// Model is the actual model that served the request.
Model string
// StopReason indicates why generation stopped.
// Normalized to: "end", "max_tokens", "error"
StopReason string
}
Response holds the LLM's output.
type RetryConfig ¶
type RetryConfig struct {
MaxAttempts int
InitialWait time.Duration
MaxWait time.Duration
Multiplier float64
}
RetryConfig configures retry behavior for transient failures.
type RetryProvider ¶
type RetryProvider struct {
// contains filtered or unexported fields
}
RetryProvider is a decorator that retries transient errors with exponential backoff and jitter.
func (*RetryProvider) ModelID ¶
func (r *RetryProvider) ModelID() string
type Schema ¶
type Schema struct {
// Name identifies this schema (used as tool name for Anthropic,
// schema name for OpenAI). Kebab-case, e.g. "math-question".
Name string
// Description is a human-readable description of what this schema
// represents. Sent to the LLM to guide generation.
Description string
// Definition is the JSON Schema definition as a map.
Definition map[string]any
}
Schema defines the JSON structure expected from the LLM.