provider

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyChatOptions

func ApplyChatOptions(opts *ChatOptions, options ...ChatOption)

ApplyChatOptions applies a list of ChatOption functions to ChatOptions

func ApplyStreamOptions

func ApplyStreamOptions(opts *ChatOptions, options ...StreamOption)

ApplyStreamOptions applies a list of StreamOption functions to ChatOptions

Types

type AuthenticationError

type AuthenticationError struct {
	Provider string
	Reason   string
}

AuthenticationError represents an authentication failure

func NewAuthenticationError

func NewAuthenticationError(provider, reason string) *AuthenticationError

NewAuthenticationError creates a new AuthenticationError

func (*AuthenticationError) Error

func (e *AuthenticationError) Error() string

Error implements the error interface

type BaseProvider

type BaseProvider struct {
	// contains filtered or unexported fields
}

BaseProvider provides common functionality for all provider implementations

func NewBaseProvider

func NewBaseProvider(config BaseProviderConfig) *BaseProvider

NewBaseProvider creates a new BaseProvider with the given configuration

func (*BaseProvider) Close

func (b *BaseProvider) Close() error

Close releases resources held by the base provider

func (*BaseProvider) DoRequest

func (b *BaseProvider) DoRequest(ctx context.Context, req *http.Request) (*http.Response, error)

DoRequest executes an HTTP request with retry logic and error handling

func (*BaseProvider) HandleHTTPError

func (b *BaseProvider) HandleHTTPError(resp *http.Response, body []byte) error

HandleHTTPError converts HTTP errors to provider-specific errors

func (*BaseProvider) LogRequest

func (b *BaseProvider) LogRequest(req *http.Request)

LogRequest logs details about an HTTP request (for debugging)

func (*BaseProvider) LogResponse

func (b *BaseProvider) LogResponse(resp *http.Response)

LogResponse logs details about an HTTP response (for debugging)

func (*BaseProvider) Name

func (b *BaseProvider) Name() string

Name returns the provider's name

func (*BaseProvider) ValidateModel

func (b *BaseProvider) ValidateModel(model string, supportedModels []string) error

ValidateModel checks if a model is supported and returns an error if not

type BaseProviderConfig

type BaseProviderConfig struct {
	Name        string
	HTTPClient  *http.Client
	Logger      logger.LoggerInterface
	RetryConfig RetryConfig
}

BaseProviderConfig contains configuration for creating a BaseProvider

type ChatOption

type ChatOption func(*ChatOptions)

ChatOption is a function that modifies ChatOptions

func WithMaxTokens

func WithMaxTokens(maxTokens int) ChatOption

WithMaxTokens sets the maximum number of tokens to generate

func WithMetadata

func WithMetadata(key, value string) ChatOption

WithMetadata adds custom metadata to the request

func WithModel

func WithModel(model string) ChatOption

WithModel sets the model to use for the request

func WithStopSequences

func WithStopSequences(sequences ...string) ChatOption

WithStopSequences sets the sequences that will stop generation

func WithSystemPrompt

func WithSystemPrompt(prompt string) ChatOption

WithSystemPrompt sets the system prompt for the conversation

func WithTemperature

func WithTemperature(temperature float64) ChatOption

WithTemperature sets the sampling temperature (0.0 to 1.0)

func WithTopP

func WithTopP(topP float64) ChatOption

WithTopP sets the nucleus sampling parameter (0.0 to 1.0)

type ChatOptions

type ChatOptions struct {
	Model         string
	MaxTokens     int
	Temperature   float64
	TopP          float64
	StopSequences []string
	SystemPrompt  string
	Stream        bool
	Metadata      map[string]string
}

ChatOptions contains configuration options for chat requests

func DefaultChatOptions

func DefaultChatOptions() *ChatOptions

DefaultChatOptions returns ChatOptions with sensible defaults

type ContextLengthError

type ContextLengthError struct {
	Provider      string
	Model         string
	RequestTokens int
	MaxTokens     int
}

ContextLengthError represents a context length exceeded error

func NewContextLengthError

func NewContextLengthError(provider, model string, requestTokens, maxTokens int) *ContextLengthError

NewContextLengthError creates a new ContextLengthError

func (*ContextLengthError) Error

func (e *ContextLengthError) Error() string

Error implements the error interface

type Event

type Event struct {
	Type    EventType
	Content string
	Error   error
	Done    bool
}

Event represents a streaming event

type EventType

type EventType int

EventType represents the type of streaming event

const (
	EventTypeContentDelta EventType = iota // Incremental content
	EventTypeContentStart                  // Stream started
	EventTypeContentEnd                    // Stream completed
	EventTypeError                         // Error occurred
)

func (EventType) String

func (e EventType) String() string

String returns the string representation of EventType

type InvalidModelError

type InvalidModelError struct {
	Provider        string
	Model           string
	SupportedModels []string
}

InvalidModelError represents an invalid model identifier error

func NewInvalidModelError

func NewInvalidModelError(provider, model string, supportedModels []string) *InvalidModelError

NewInvalidModelError creates a new InvalidModelError

func (*InvalidModelError) Error

func (e *InvalidModelError) Error() string

Error implements the error interface

type Message

type Message struct {
	Role    string // "user", "assistant", "system"
	Content string
}

Message represents a chat message

type Provider

type Provider interface {
	// Chat sends a complete chat request and waits for the full response
	Chat(ctx context.Context, messages []Message, opts ...ChatOption) (Response, error)

	// Stream sends a streaming chat request and returns a channel for events
	Stream(ctx context.Context, messages []Message, opts ...StreamOption) (<-chan Event, error)

	// Name returns the provider's name
	Name() string

	// Models returns the list of supported model identifiers
	Models() []string

	// Close releases any resources held by the provider
	Close() error
}

Provider defines the interface for LLM providers

type ProviderError

type ProviderError struct {
	Provider string
	Model    string
	Err      error
}

ProviderError represents a provider-specific error with additional context

func NewProviderError

func NewProviderError(provider, model string, err error) *ProviderError

NewProviderError creates a new ProviderError

func (*ProviderError) Error

func (e *ProviderError) Error() string

Error implements the error interface

func (*ProviderError) Unwrap

func (e *ProviderError) Unwrap() error

Unwrap returns the underlying error

type RateLimitError

type RateLimitError struct {
	Provider     string
	RetryAfter   int // seconds until retry is allowed
	LimitType    string
	CurrentUsage int
	Limit        int
}

RateLimitError represents a rate limiting error

func NewRateLimitError

func NewRateLimitError(provider string, retryAfter int) *RateLimitError

NewRateLimitError creates a new RateLimitError with retry information

func NewRateLimitErrorWithDetails

func NewRateLimitErrorWithDetails(provider, limitType string, currentUsage, limit int) *RateLimitError

NewRateLimitErrorWithDetails creates a detailed RateLimitError

func (*RateLimitError) Error

func (e *RateLimitError) Error() string

Error implements the error interface

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages registered LLM providers in a thread-safe manner

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new provider registry

func (*Registry) Close

func (r *Registry) Close() error

Close closes all registered providers and clears the registry

func (*Registry) Count

func (r *Registry) Count() int

Count returns the number of registered providers

func (*Registry) Get

func (r *Registry) Get(name string) (Provider, error)

Get retrieves a provider by name Returns an error if the provider is not found

func (*Registry) Has

func (r *Registry) Has(name string) bool

Has checks if a provider with the given name is registered

func (*Registry) List

func (r *Registry) List() []string

List returns the names of all registered providers

func (*Registry) Register

func (r *Registry) Register(name string, p Provider) error

Register adds a provider to the registry Returns an error if a provider with the same name is already registered

func (*Registry) Unregister

func (r *Registry) Unregister(name string) error

Unregister removes a provider from the registry Returns an error if the provider is not found

type Response

type Response struct {
	Content string
	Usage   Usage
	Model   string
}

Response represents a complete chat response

type RetryConfig

type RetryConfig struct {
	MaxRetries           int           // Maximum number of retry attempts
	InitialBackoff       time.Duration // Initial backoff duration
	MaxBackoff           time.Duration // Maximum backoff duration
	Multiplier           float64       // Backoff multiplier for exponential backoff
	RetryableStatusCodes []int         // HTTP status codes that should trigger retries
}

RetryConfig configures retry behavior for failed requests

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns sensible default retry configuration

type StreamOption

type StreamOption func(*ChatOptions)

StreamOption is a function that modifies ChatOptions for streaming requests

func StreamWithMaxTokens

func StreamWithMaxTokens(maxTokens int) StreamOption

StreamWithMaxTokens sets the maximum number of tokens for streaming requests

func StreamWithMetadata

func StreamWithMetadata(key, value string) StreamOption

StreamWithMetadata adds metadata for streaming requests

func StreamWithModel

func StreamWithModel(model string) StreamOption

StreamWithModel sets the model to use for streaming requests

func StreamWithStopSequences

func StreamWithStopSequences(sequences ...string) StreamOption

StreamWithStopSequences sets stop sequences for streaming requests

func StreamWithSystemPrompt

func StreamWithSystemPrompt(prompt string) StreamOption

StreamWithSystemPrompt sets the system prompt for streaming requests

func StreamWithTemperature

func StreamWithTemperature(temperature float64) StreamOption

StreamWithTemperature sets the temperature for streaming requests

func StreamWithTopP

func StreamWithTopP(topP float64) StreamOption

StreamWithTopP sets the top_p for streaming requests

type StreamingNotSupportedError

type StreamingNotSupportedError struct {
	Provider string
	Model    string
	Reason   string
}

StreamingNotSupportedError represents an error when streaming is not supported

func NewStreamingNotSupportedError

func NewStreamingNotSupportedError(provider, model, reason string) *StreamingNotSupportedError

NewStreamingNotSupportedError creates a new StreamingNotSupportedError

func (*StreamingNotSupportedError) Error

Error implements the error interface

type ThinkingBlock

type ThinkingBlock struct {
	Content   string
	Index     int
	Timestamp int64
	Type      string
}

ThinkingBlock represents a thinking/reasoning block from Claude's extended thinking

type Usage

type Usage struct {
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
}

Usage represents token usage statistics

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL