llm

package
v0.2.34 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultConcurrencyLimit is the default concurrency limit for each model.
	DefaultConcurrencyLimit = 10
	// MaxModelFailures is the maximum consecutive failures before disabling a model.
	MaxModelFailures = 5
)
View Source
const (
	KeyTimeoutRate = "timeoutRate"
)

Variables

This section is empty.

Functions

func CalculateModelCost

func CalculateModelCost(models map[string]*LLMModelConfig, modelName string, inputTokens, outputTokens int) (float64, error)

CalculateModelCost calculates cost for a model based on token usage. Prices are per 1M tokens in config, so we divide by 1,000,000.

func GetModelPricing

func GetModelPricing(models map[string]*LLMModelConfig, modelName string) (promptPrice, completionPrice, cachePrice float64, err error)

GetModelPricing returns pricing information for a model.

func ResolveModels

func ResolveModels(models map[string]*LLMModelConfig) error

ResolveModels resolves the 'extend' field in LLM model configurations.

func SubstituteEnvValue

func SubstituteEnvValue(val interface{}) interface{}

SubstituteEnvValue applies SubstituteEnvVars recursively to a value tree.

func SubstituteEnvVars

func SubstituteEnvVars(content string) string

SubstituteEnvVars replaces ${VAR_NAME} or ${VAR_NAME:default} with environment variable values.

func WithReasoningEffort

func WithReasoningEffort(e string) model.Option

func WithToolStream

func WithToolStream(enable bool) model.Option

Types

type GLMChatModel

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

func NewGLMChatModel

func NewGLMChatModel(apiKey, baseURL, model string) *GLMChatModel

func (*GLMChatModel) Generate

func (g *GLMChatModel) Generate(ctx context.Context, input []*schema.Message, opts ...model.Option) (*schema.Message, error)

func (*GLMChatModel) Stream

func (g *GLMChatModel) Stream(ctx context.Context, input []*schema.Message, opts ...model.Option) (*schema.StreamReader[*schema.Message], error)

func (*GLMChatModel) WithTools

func (g *GLMChatModel) WithTools(tools []*schema.ToolInfo) (model.ToolCallingChatModel, error)

type LLMCallMeta

type LLMCallMeta struct {
	ModelName    string
	ModelConfig  *LLMModelConfig
	SystemPrompt string
	UserPrompt   string
	Messages     []*schema.Message
	Attempt      int
	Start        time.Time
	Duration     time.Duration
}

LLMCallMeta captures per-attempt context for hooks.

type LLMCallOptions

type LLMCallOptions struct {
	UserID    int
	JobID     int
	Node      string
	OnAttempt func(meta *LLMCallMeta)
	OnSuccess func(meta *LLMCallMeta, response *schema.Message)
	OnFailure func(meta *LLMCallMeta, err error)
}

LLMCallOptions contains options for LLM calls.

type LLMManager

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

LLMManager manages multiple LLM models with dynamic model ordering.

func NewLLMManager

func NewLLMManager(cfg ModelConfigProvider, getModels func() []string) (*LLMManager, error)

NewLLMManager creates a new LLM manager with a model provider.

func (*LLMManager) Call

func (m *LLMManager) Call(ctx context.Context, systemPrompt, userPrompt string, opts *LLMCallOptions) (string, error)

Call invokes LLM with automatic failover based on getModels order.

func (*LLMManager) GetAllModelStats

func (m *LLMManager) GetAllModelStats() []ModelStats

GetAllModelStats returns statistics for all models managed by this manager.

func (*LLMManager) GetModels

func (m *LLMManager) GetModels() []*LLMModel

GetModels returns the list of models managed by this manager.

func (*LLMManager) ResetAllModelStats

func (m *LLMManager) ResetAllModelStats()

ResetAllModelStats resets statistics for all models managed by this manager.

type LLMModel

type LLMModel struct {
	Config    *LLMModelConfig
	ChatModel model.ToolCallingChatModel
	// contains filtered or unexported fields
}

LLMModel represents a single LLM model with concurrency control.

func GetLLMModel

func GetLLMModel(cfg ModelConfigProvider, name string) (*LLMModel, error)

GetLLMModel retrieves or creates an LLM model by name with caching.

func (*LLMModel) Generate

func (m *LLMModel) Generate(ctx context.Context, messages []*schema.Message) (*schema.Message, error)

Generate calls the LLM model with concurrency control and timeout.

func (*LLMModel) GetStats

func (m *LLMModel) GetStats() (successCount, failureCount, totalWaitTime, totalDuration int64)

GetStats returns the statistics for this model.

func (*LLMModel) IsDisabled

func (m *LLMModel) IsDisabled() bool

IsDisabled checks if a model is disabled.

func (*LLMModel) RecordStatus

func (m *LLMModel) RecordStatus(success bool)

RecordStatus records a success/failure for a model and disables it if threshold is reached.

func (*LLMModel) ResetStats

func (m *LLMModel) ResetStats()

ResetStats resets the statistics counters for this model.

type LLMModelConfig

type LLMModelConfig struct {
	Name            string                 `yaml:"name,omitempty" mapstructure:"name"`
	Extend          interface{}            `yaml:"extend,omitempty" mapstructure:"extend"` // string or []string
	APIType         string                 `yaml:"api_type,omitempty" mapstructure:"api_type"`
	APIKey          string                 `yaml:"api_key,omitempty" mapstructure:"api_key"`
	BaseURL         string                 `yaml:"base_url,omitempty" mapstructure:"base_url"`
	Timeout         int                    `yaml:"timeout,omitempty" mapstructure:"timeout"`
	TimeoutRate     float64                `yaml:"timeout_rate,omitempty" mapstructure:"timeout_rate"`
	Temperature     *float64               `yaml:"temperature,omitempty" mapstructure:"temperature"`
	Concurrency     int                    `yaml:"concurrency,omitempty" mapstructure:"concurrency"`
	PromptPrice     float64                `yaml:"prompt_price,omitempty" mapstructure:"prompt_price"`
	CompletionPrice float64                `yaml:"completion_price,omitempty" mapstructure:"completion_price"`
	CachePrice      float64                `yaml:"cache_price,omitempty" mapstructure:"cache_price"`
	Payload         map[string]interface{} `yaml:"payload,omitempty" mapstructure:"payload"`
}

LLMModelConfig represents a single LLM model configuration.

func GetModelConfig

func GetModelConfig(models map[string]*LLMModelConfig, name string) (*LLMModelConfig, error)

GetModelConfig returns LLM model configuration by name.

type ModelConfigProvider

type ModelConfigProvider interface {
	GetLLMModel(name string) (*LLMModelConfig, error)
}

ModelConfigProvider provides access to model configuration by name.

type ModelNamesProvider

type ModelNamesProvider interface {
	GetLLMModelNames() []string
}

ModelNamesProvider provides a default ordered model list.

type ModelSelectionStrategy

type ModelSelectionStrategy string

ModelSelectionStrategy defines how models are selected.

const (
	StrategySequential ModelSelectionStrategy = "sequential"
	StrategyRandom     ModelSelectionStrategy = "random"
)

type ModelStats

type ModelStats struct {
	ModelName     string  `json:"model_name"`
	SuccessCount  int64   `json:"success_count"`
	FailureCount  int64   `json:"failure_count"`
	WaitTimeRatio float64 `json:"wait_time_ratio"`
}

ModelStats tracks success and failure counts for a specific model.

Jump to

Keyboard shortcuts

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