racing

package
v1.0.21 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package racing provides a virtual provider that races multiple providers concurrently. It returns the first successful response, with support for weighted and quality-based selection strategies, along with performance tracking to optimize provider selection over time.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Default configuration for backwards compatibility
	TimeoutMS     int      `yaml:"timeout_ms"`
	GracePeriodMS int      `yaml:"grace_period_ms"`
	Strategy      Strategy `yaml:"strategy"`
	ProviderNames []string `yaml:"providers"`

	// Virtual models configuration
	VirtualModels       map[string]VirtualModelConfig `yaml:"virtual_models"`
	DefaultVirtualModel string                        `yaml:"default_virtual_model"`
	PerformanceFile     string                        `yaml:"performance_file,omitempty"`
}

Config represents configuration for the racing provider

func DefaultConfig added in v1.0.14

func DefaultConfig() *Config

DefaultConfig returns a default configuration with sensible defaults

func (*Config) GetEffectiveStrategy added in v1.0.14

func (c *Config) GetEffectiveStrategy(modelName string) Strategy

GetEffectiveStrategy returns the effective strategy for a given virtual model Falls back to default config values if the virtual model doesn't exist

func (*Config) GetEffectiveTimeout added in v1.0.14

func (c *Config) GetEffectiveTimeout(modelName string) int

GetEffectiveTimeout returns the effective timeout for a given virtual model Falls back to default config values if the virtual model doesn't exist

func (*Config) GetVirtualModel added in v1.0.14

func (c *Config) GetVirtualModel(modelName string) *VirtualModelConfig

GetVirtualModel returns the virtual model configuration for the given model name

func (*Config) Validate added in v1.0.14

func (c *Config) Validate() error

Validate performs validation on the configuration

type ConfigError added in v1.0.14

type ConfigError struct {
	Field   string
	Message string
}

ConfigError represents a configuration validation error

func (*ConfigError) Error added in v1.0.14

func (e *ConfigError) Error() string

type PerformanceTracker

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

func NewPerformanceTracker

func NewPerformanceTracker() *PerformanceTracker

func (*PerformanceTracker) GetAllStats

func (pt *PerformanceTracker) GetAllStats() map[string]*ProviderStats

func (*PerformanceTracker) GetScore

func (pt *PerformanceTracker) GetScore(provider string) float64

func (*PerformanceTracker) RecordLoss

func (pt *PerformanceTracker) RecordLoss(provider string, latency time.Duration)

func (*PerformanceTracker) RecordWin

func (pt *PerformanceTracker) RecordWin(provider string, latency time.Duration)

type ProviderReference added in v1.0.14

type ProviderReference struct {
	Name     string                 `yaml:"name"`
	Model    string                 `yaml:"model,omitempty"`
	Priority int                    `yaml:"priority,omitempty"` // Higher numbers = higher priority
	Config   map[string]interface{} `yaml:"config,omitempty"`
}

ProviderReference represents a reference to a provider with optional configuration

type ProviderStats

type ProviderStats struct {
	TotalRaces   int64         `json:"total_races"`
	Wins         int64         `json:"wins"`
	Losses       int64         `json:"losses"`
	AvgLatency   time.Duration `json:"avg_latency"`
	TotalLatency time.Duration `json:"-"`
	WinRate      float64       `json:"win_rate"`
	LastUpdated  time.Time     `json:"last_updated"`
}

type RacingProvider

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

RacingProvider races multiple providers and returns the first successful response

func NewRacingProvider

func NewRacingProvider(name string, config *Config) *RacingProvider

func (*RacingProvider) Authenticate

func (r *RacingProvider) Authenticate(ctx context.Context, authConfig types.AuthConfig) error

func (*RacingProvider) Configure

func (r *RacingProvider) Configure(config types.ProviderConfig) error

func (*RacingProvider) Description

func (r *RacingProvider) Description() string

func (*RacingProvider) GenerateChatCompletion

func (r *RacingProvider) GenerateChatCompletion(ctx context.Context, opts types.GenerateOptions) (types.ChatCompletionStream, error)

func (*RacingProvider) GetConfig

func (r *RacingProvider) GetConfig() types.ProviderConfig

func (*RacingProvider) GetDefaultModel

func (r *RacingProvider) GetDefaultModel() string

func (*RacingProvider) GetMetrics

func (r *RacingProvider) GetMetrics() types.ProviderMetrics

func (*RacingProvider) GetModels

func (r *RacingProvider) GetModels(ctx context.Context) ([]types.Model, error)

func (*RacingProvider) GetPerformanceStats

func (r *RacingProvider) GetPerformanceStats() map[string]*ProviderStats

func (*RacingProvider) GetToolFormat

func (r *RacingProvider) GetToolFormat() types.ToolFormat

func (*RacingProvider) HealthCheck

func (r *RacingProvider) HealthCheck(ctx context.Context) error

func (*RacingProvider) InvokeServerTool

func (r *RacingProvider) InvokeServerTool(ctx context.Context, toolName string, params interface{}) (interface{}, error)

func (*RacingProvider) IsAuthenticated

func (r *RacingProvider) IsAuthenticated() bool

func (*RacingProvider) Logout

func (r *RacingProvider) Logout(ctx context.Context) error

func (*RacingProvider) Name

func (r *RacingProvider) Name() string

func (*RacingProvider) SetMetricsCollector added in v1.0.7

func (r *RacingProvider) SetMetricsCollector(collector types.MetricsCollector)

func (*RacingProvider) SetProviders

func (r *RacingProvider) SetProviders(providers []types.Provider)

func (*RacingProvider) SupportsResponsesAPI

func (r *RacingProvider) SupportsResponsesAPI() bool

func (*RacingProvider) SupportsStreaming

func (r *RacingProvider) SupportsStreaming() bool

func (*RacingProvider) SupportsToolCalling

func (r *RacingProvider) SupportsToolCalling() bool

func (*RacingProvider) Type

func (r *RacingProvider) Type() types.ProviderType

type Strategy

type Strategy string
const (
	StrategyFirstWins Strategy = "first_wins"
	StrategyWeighted  Strategy = "weighted"
	StrategyQuality   Strategy = "quality"
)

type VirtualModelConfig added in v1.0.14

type VirtualModelConfig struct {
	DisplayName string              `yaml:"display_name"`
	Description string              `yaml:"description"`
	Providers   []ProviderReference `yaml:"providers"`
	Strategy    Strategy            `yaml:"strategy"`
	TimeoutMS   int                 `yaml:"timeout_ms"`
}

VirtualModelConfig represents configuration for a single virtual model

Jump to

Keyboard shortcuts

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