Documentation
¶
Index ¶
- Constants
- func DefaultOptions() []llm.Option
- type ModelData
- type Provider
- func (p *Provider) CountTokens(_ context.Context, req tokencount.TokenCountRequest) (*tokencount.TokenCount, error)
- func (p *Provider) CreateStream(ctx context.Context, opts llm.Request) (llm.Stream, error)
- func (p *Provider) DefaultModel() string
- func (p *Provider) FetchModels(ctx context.Context) ([]llm.Model, error)
- func (p *Provider) Models() llm.Models
- func (p *Provider) Name() string
- func (p *Provider) Resolve(model string) (llm.Model, error)
- func (p *Provider) WithDefaultModel(modelID string) *Provider
Constants ¶
const (
// DefaultModel is the recommended default model for OpenRouter.
DefaultModel = "auto"
)
Variables ¶
This section is empty.
Functions ¶
func DefaultOptions ¶ added in v0.12.0
DefaultOptions returns the default options for OpenRouter. Base URL defaults to https://openrouter.ai/api. API key should be provided via WithAPIKey() or WithAPIKeyFunc().
Types ¶
type ModelData ¶
type ModelData struct {
ID string `json:"id"`
CanonicalSlug string `json:"canonical_slug"`
HuggingFaceID string `json:"hugging_face_id"`
Name string `json:"name"`
Created int64 `json:"created"`
Description string `json:"description"`
ContextLength int `json:"context_length"`
Architecture struct {
Modality string `json:"modality"`
InputModalities []string `json:"input_modalities"`
OutputModalities []string `json:"output_modalities"`
Tokenizer string `json:"tokenizer"`
InstructType *string `json:"instruct_type"`
} `json:"architecture"`
Pricing struct {
Prompt string `json:"prompt"`
Completion string `json:"completion"`
InputCacheRead string `json:"input_cache_read"`
} `json:"pricing"`
TopProvider struct {
ContextLength int `json:"context_length"`
MaxCompletionTokens int `json:"max_completion_tokens"`
IsModerated bool `json:"is_moderated"`
} `json:"top_provider"`
PerRequestLimits interface{} `json:"per_request_limits"`
SupportedParameters []string `json:"supported_parameters"`
DefaultParameters map[string]interface{} `json:"default_parameters"`
}
ModelData represents the full structure of a model from OpenRouter API.
func GetModelData ¶
GetModelData returns the full model data from the embedded models.json file. This includes pricing, context length, supported parameters, and other metadata.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider implements the OpenRouter LLM backend.
func New ¶
New creates a new OpenRouter provider. Options are applied on top of DefaultOptions().
Example usage:
// Add API key
p := openrouter.New(llm.WithAPIKey("sk-or-..."))
// Add API key from environment
p := openrouter.New(llm.APIKeyFromEnv("OPENROUTER_API_KEY"))
// Add dynamic API key resolution
p := openrouter.New(llm.WithAPIKeyFunc(func(ctx context.Context) (string, error) {
return secretStore.Get(ctx, "openrouter-key")
}))
func (*Provider) CountTokens ¶ added in v0.24.0
func (p *Provider) CountTokens(_ context.Context, req tokencount.TokenCountRequest) (*tokencount.TokenCount, error)
CountTokens estimates the number of input tokens for the given request.
The BPE encoding is selected based on the model ToolCallID (o200k_base for GPT-4o / o-series; cl100k_base for everything else including unknown models). No per-message overhead is applied — OpenRouter routes to many providers and the overhead constants are model-family-specific.
func (*Provider) CreateStream ¶
func (*Provider) DefaultModel ¶
DefaultModel returns the configured default model ToolCallID.
func (*Provider) FetchModels ¶
func (*Provider) WithDefaultModel ¶
WithDefaultModel sets the default model to use.