Documentation
¶
Index ¶
- Variables
- func IsRateLimited(err error) bool
- func IsRetryable(err error) bool
- type APIError
- type Choice
- type ContentPart
- type Delta
- type Document
- type Event
- type EventType
- type FuncCall
- type FuncRef
- type Function
- type ImageURL
- type Message
- type Middleware
- type Option
- type Provider
- type ProviderConfig
- type Request
- type Response
- type Role
- type Router
- func (r *Router) AddMiddleware(m Middleware)
- func (r *Router) Complete(ctx context.Context, req *Request) (*Response, error)
- func (r *Router) GetProvider(name string) (Provider, bool)
- func (r *Router) MapModel(model, provider string)
- func (r *Router) Providers() []string
- func (r *Router) RegisterProvider(name string, p Provider)
- func (r *Router) Route(ctx context.Context, req *Request) (<-chan Event, error)
- func (r *Router) SetFallbacks(providers ...string)
- func (r *Router) Stream(ctx context.Context, req *Request) (<-chan Event, error)
- type Tool
- type ToolCall
- type ToolChoice
- type Usage
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnknownModel = errors.New("unknown model") ErrUnknownProvider = errors.New("unknown provider") ErrNoProviders = errors.New("no providers registered") ErrRateLimited = errors.New("rate limited") ErrContextCanceled = errors.New("context canceled") ErrStreamClosed = errors.New("stream closed") ErrInvalidRequest = errors.New("invalid request") ErrAuthFailed = errors.New("authentication failed") ErrProviderError = errors.New("provider error") ErrCircuitOpen = errors.New("circuit breaker is open") ErrMaxRetriesExceed = errors.New("max retries exceeded") )
Sentinel errors
Functions ¶
func IsRateLimited ¶
IsRateLimited returns true if the error indicates rate limiting
func IsRetryable ¶
IsRetryable returns true if the error is retryable
Types ¶
type Choice ¶
type Choice struct {
Index int `json:"index"`
Message *Message `json:"message,omitempty"`
Delta *Delta `json:"delta,omitempty"`
FinishReason string `json:"finish_reason,omitempty"`
}
Choice represents a completion choice
type ContentPart ¶
type ContentPart struct {
Type string `json:"type"` // "text", "image_url", or "document"
Text string `json:"text,omitempty"`
ImageURL *ImageURL `json:"image_url,omitempty"`
Document *Document `json:"document,omitempty"`
}
ContentPart represents a part of a multimodal message
type Delta ¶
type Delta struct {
Role Role `json:"role,omitempty"`
Content string `json:"content,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}
Delta represents streaming content delta
type Document ¶
type Document struct {
Base64 string `json:"base64"`
MediaType string `json:"media_type"` // e.g. "application/pdf"
}
Document represents a document (PDF, etc.) for providers that support it natively
type FuncRef ¶
type FuncRef struct {
Name string `json:"name"`
}
FuncRef references a specific function
type Function ¶
type Function struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters json.RawMessage `json:"parameters,omitempty"`
}
Function represents a function definition
type ImageURL ¶
type ImageURL struct {
URL string `json:"url"`
Detail string `json:"detail,omitempty"`
Base64 string `json:"base64,omitempty"`
MediaType string `json:"media_type,omitempty"`
}
ImageURL represents an image reference with both URL and base64 forms
type Message ¶
type Message struct {
Role Role `json:"role"`
Content string `json:"content"`
ContentParts []ContentPart `json:"content_parts,omitempty"`
Name string `json:"name,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
Message represents a chat message
type Middleware ¶
Middleware wraps a Provider with additional functionality
type Option ¶
type Option func(*Router)
Option configures the Router
func WithFallback ¶
WithFallback sets fallback providers in priority order
func WithMiddleware ¶
func WithMiddleware(m ...Middleware) Option
WithMiddleware adds middleware to the processing chain. Use this with middleware from the middleware package:
import "github.com/bluefunda/llm-router/middleware"
router := llmrouter.New(
llmrouter.WithMiddleware(
middleware.NewRetryMiddleware(3, time.Second),
middleware.NewTimeoutMiddleware(60*time.Second),
),
)
func WithModelMapping ¶
WithModelMapping maps a model to a specific provider
func WithProvider ¶
WithProvider registers a provider with the router
type Provider ¶
type Provider interface {
// Name returns the provider identifier (e.g., "openai", "anthropic")
Name() string
// Models returns the list of supported model IDs
Models() []string
// Complete performs a non-streaming completion
Complete(ctx context.Context, req *Request) (*Response, error)
// Stream performs a streaming completion, returning events via channel
Stream(ctx context.Context, req *Request) (<-chan Event, error)
// SupportsTools returns whether the provider supports function/tool calling
SupportsTools() bool
}
Provider is the core interface that all LLM providers must implement
type ProviderConfig ¶
type ProviderConfig struct {
Name string
APIKey string
BaseURL string
Model string
Models []string
MaxRetries int
Timeout time.Duration
CustomHeaders map[string]string // custom HTTP headers (e.g. api-subscription-key)
// StringContentOnly forces message content to be sent as plain strings
// instead of structured arrays. Required for some OpenAI-compatible APIs
// (e.g. Sarvam) that don't support the array content format.
StringContentOnly bool
}
ProviderConfig holds common configuration for providers
type Request ¶
type Request struct {
Messages []Message `json:"messages"`
Model string `json:"model,omitempty"`
Tools []Tool `json:"tools,omitempty"`
ToolChoice *ToolChoice `json:"tool_choice,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
Stop []string `json:"stop,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
Request represents a unified LLM request
type Response ¶
type Response struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []Choice `json:"choices"`
Usage *Usage `json:"usage,omitempty"`
Provider string `json:"provider"`
}
Response represents a unified LLM response (OpenAI-compatible)
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router manages multiple LLM providers and routes requests
func (*Router) AddMiddleware ¶
func (r *Router) AddMiddleware(m Middleware)
AddMiddleware adds middleware to the router
func (*Router) GetProvider ¶
GetProvider returns a provider by name
func (*Router) RegisterProvider ¶
RegisterProvider adds a provider to the router
func (*Router) SetFallbacks ¶
SetFallbacks sets the fallback provider order
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function FuncCall `json:"function"`
Index *int `json:"index,omitempty"`
}
ToolCall represents a tool invocation
type ToolChoice ¶
type ToolChoice struct {
Type string `json:"type,omitempty"`
Function *FuncRef `json:"function,omitempty"`
}
ToolChoice controls tool selection