Documentation
¶
Overview ¶
Package api provides the OpenRouter API client.
Index ¶
- Constants
- Variables
- type APIError
- type ChatCall
- type ChatRequest
- type ChatResponse
- type ChatStreamCall
- type Choice
- type Client
- type ClientConfig
- type ImageConfig
- type ImageContent
- type ImageURL
- type ListModelsCall
- type ListModelsOptions
- type Message
- type MockClient
- func (m *MockClient) Chat(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
- func (m *MockClient) ChatStream(ctx context.Context, req *ChatRequest) (*StreamReader, error)
- func (m *MockClient) ListModels(ctx context.Context, opts *ListModelsOptions) ([]Model, error)
- func (m *MockClient) Reset()
- type Model
- type ModelArchitecture
- type ModelPricing
- type ModelsResponse
- type PerRequestLimits
- type RetryConfig
- type StreamChunk
- type StreamError
- type StreamReader
- type TopProviderInfo
Constants ¶
const ( // DefaultBaseURL is the default OpenRouter API base URL. DefaultBaseURL = "https://openrouter.ai/api/v1" // DefaultTimeout is the default HTTP timeout. DefaultTimeout = 30 * time.Second // DefaultStreamTimeout is the default timeout for streaming requests. DefaultStreamTimeout = 5 * time.Minute // DefaultImageTimeout is the default timeout for image generation requests. DefaultImageTimeout = 5 * time.Minute // DefaultMaxRetries is the default maximum number of retries. DefaultMaxRetries = 3 // DefaultInitialBackoff is the default initial backoff duration. DefaultInitialBackoff = 500 * time.Millisecond // DefaultMaxBackoff is the default maximum backoff duration. DefaultMaxBackoff = 5 * time.Second )
const ( SSEDataPrefix = "data: " SSECommentPrefix = ":" StreamEndSignal = "[DONE]" )
SSE stream constants.
Variables ¶
var ( ErrRateLimited = errors.New("rate limited") ErrStreamClosed = errors.New("stream closed") )
Sentinel errors for common API error conditions.
Functions ¶
This section is empty.
Types ¶
type ChatCall ¶
type ChatCall struct {
Ctx context.Context
Req *ChatRequest
}
ChatCall records a call to Chat.
type ChatRequest ¶
type ChatRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Stream bool `json:"stream"`
Modalities []string `json:"modalities,omitempty"`
ImageConfig *ImageConfig `json:"image_config,omitempty"`
}
ChatRequest represents a request to the chat completions API.
type ChatResponse ¶
type ChatResponse struct {
Choices []Choice `json:"choices"`
Error *struct {
Message string `json:"message"`
} `json:"error"`
}
ChatResponse represents the response from the chat completions API.
type ChatStreamCall ¶
type ChatStreamCall struct {
Ctx context.Context
Req *ChatRequest
}
ChatStreamCall records a call to ChatStream.
type Choice ¶
type Choice struct {
Delta struct {
Content string `json:"content"`
} `json:"delta"`
Message struct {
Content string `json:"content"`
Images []ImageContent `json:"images,omitempty"`
} `json:"message"`
FinishReason *string `json:"finish_reason"`
}
Choice represents a completion choice in the response.
type Client ¶
type Client interface {
// Chat sends a non-streaming chat request.
Chat(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
// ChatStream sends a streaming chat request and returns a StreamReader.
ChatStream(ctx context.Context, req *ChatRequest) (*StreamReader, error)
// ListModels retrieves available models.
ListModels(ctx context.Context, opts *ListModelsOptions) ([]Model, error)
}
Client is the interface for interacting with the OpenRouter API.
func DefaultClient ¶
DefaultClient creates a new client with default configuration.
func ImageClient ¶
ImageClient creates a new client configured for image generation with longer timeout. Image generation uses non-streaming requests, so only Timeout is extended.
func NewClient ¶
func NewClient(cfg ClientConfig) Client
NewClient creates a new API client with the given configuration.
type ClientConfig ¶
type ClientConfig struct {
// APIKey is the OpenRouter API key.
APIKey string
// BaseURL is the API base URL. Defaults to DefaultBaseURL.
BaseURL string
// Timeout is the HTTP timeout for non-streaming requests.
// Defaults to DefaultTimeout.
Timeout time.Duration
// StreamTimeout is the timeout for streaming requests.
// Defaults to DefaultStreamTimeout.
StreamTimeout time.Duration
// HTTPClient is an optional custom HTTP client.
// If nil, a new client will be created.
HTTPClient *http.Client
// Referer is the HTTP-Referer header value.
Referer string
// Title is the X-Title header value.
Title string
// Retry configures retry behavior. If nil, retries are disabled.
Retry *RetryConfig
}
ClientConfig contains configuration for the API client.
type ImageConfig ¶
type ImageConfig struct {
AspectRatio string `json:"aspect_ratio,omitempty"` // e.g., "1:1", "16:9"
Size string `json:"size,omitempty"` // e.g., "1K", "2K", "4K"
}
ImageConfig represents configuration for image generation.
type ImageContent ¶
type ImageContent struct {
Type string `json:"type"` // "image_url"
ImageURL ImageURL `json:"image_url"`
}
ImageContent represents image content in the response.
type ImageURL ¶
type ImageURL struct {
URL string `json:"url"` // data:image/png;base64,...
}
ImageURL represents an image URL in the response.
type ListModelsCall ¶
type ListModelsCall struct {
Ctx context.Context
Opts *ListModelsOptions
}
ListModelsCall records a call to ListModels.
type ListModelsOptions ¶
ListModelsOptions represents options for listing models.
type MockClient ¶
type MockClient struct {
// ChatFunc is called when Chat is invoked.
ChatFunc func(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
// ChatStreamFunc is called when ChatStream is invoked.
ChatStreamFunc func(ctx context.Context, req *ChatRequest) (*StreamReader, error)
// ListModelsFunc is called when ListModels is invoked.
ListModelsFunc func(ctx context.Context, opts *ListModelsOptions) ([]Model, error)
// ChatCalls records all calls to Chat.
ChatCalls []ChatCall
// ChatStreamCalls records all calls to ChatStream.
ChatStreamCalls []ChatStreamCall
// ListModelsCalls records all calls to ListModels.
ListModelsCalls []ListModelsCall
}
MockClient is a mock implementation of the Client interface for testing.
func NewMockClient ¶
func NewMockClient() *MockClient
NewMockClient creates a new MockClient with default implementations.
func (*MockClient) Chat ¶
func (m *MockClient) Chat(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
Chat implements Client.Chat.
func (*MockClient) ChatStream ¶
func (m *MockClient) ChatStream(ctx context.Context, req *ChatRequest) (*StreamReader, error)
ChatStream implements Client.ChatStream.
func (*MockClient) ListModels ¶
func (m *MockClient) ListModels(ctx context.Context, opts *ListModelsOptions) ([]Model, error)
ListModels implements Client.ListModels.
type Model ¶
type Model struct {
ID string `json:"id"`
Name string `json:"name"`
Created int64 `json:"created"`
Description string `json:"description"`
ContextLength *int `json:"context_length"`
Pricing ModelPricing `json:"pricing"`
Architecture ModelArchitecture `json:"architecture"`
TopProvider TopProviderInfo `json:"top_provider"`
PerRequestLimits *PerRequestLimits `json:"per_request_limits"`
SupportedParameters []string `json:"supported_parameters"`
}
Model represents an OpenRouter model.
func (*Model) IsImageModel ¶
IsImageModel returns true if the model supports image output.
func (*Model) IsTextOnlyModel ¶
IsTextOnlyModel returns true if the model supports text output but not image output.
type ModelArchitecture ¶
type ModelArchitecture struct {
Tokenizer string `json:"tokenizer"`
InstructType *string `json:"instruct_type"`
InputModalities []string `json:"input_modalities"`
OutputModalities []string `json:"output_modalities"`
}
ModelArchitecture represents the architecture of a model.
type ModelPricing ¶
type ModelPricing struct {
Prompt string `json:"prompt"`
Completion string `json:"completion"`
Request string `json:"request"`
Image string `json:"image"`
Web string `json:"web_search,omitempty"`
Audio string `json:"input_audio,omitempty"`
}
ModelPricing represents pricing information for a model.
type ModelsResponse ¶
type ModelsResponse struct {
Data []Model `json:"data"`
}
ModelsResponse represents the response from the models API.
type PerRequestLimits ¶
type PerRequestLimits struct {
PromptTokens *int `json:"prompt_tokens"`
CompletionTokens *int `json:"completion_tokens"`
}
PerRequestLimits represents per-request token limits.
type RetryConfig ¶
type RetryConfig struct {
// MaxRetries is the maximum number of retry attempts.
MaxRetries int
// InitialBackoff is the initial backoff duration.
InitialBackoff time.Duration
// MaxBackoff is the maximum backoff duration.
MaxBackoff time.Duration
}
RetryConfig configures retry behavior.
func DefaultRetryConfig ¶
func DefaultRetryConfig() RetryConfig
DefaultRetryConfig returns the default retry configuration.
type StreamChunk ¶
StreamChunk represents a chunk of streamed content.
type StreamError ¶
StreamError represents an error that occurred during streaming.
func (*StreamError) Error ¶
func (e *StreamError) Error() string
func (*StreamError) Unwrap ¶
func (e *StreamError) Unwrap() error
type StreamReader ¶
type StreamReader struct {
// contains filtered or unexported fields
}
StreamReader reads SSE events from a stream.
func NewStreamReader ¶
func NewStreamReader(body io.ReadCloser) *StreamReader
NewStreamReader creates a new StreamReader from an io.ReadCloser.
func (*StreamReader) Close ¶
func (r *StreamReader) Close() error
Close closes the underlying stream.
func (*StreamReader) Next ¶
func (r *StreamReader) Next() (*StreamChunk, error)
Next reads the next chunk from the stream. Returns nil, nil when the stream is complete. Returns nil, error on stream errors.
func (*StreamReader) ReadAll ¶
func (r *StreamReader) ReadAll() (string, error)
ReadAll reads all content from the stream and returns it as a string. This is a convenience method for non-TUI usage.
type TopProviderInfo ¶
type TopProviderInfo struct {
ContextLength *int `json:"context_length"`
MaxCompletionTokens *int `json:"max_completion_tokens"`
IsModerated bool `json:"is_moderated"`
}
TopProviderInfo represents information about the top provider.