Documentation
¶
Overview ¶
Package budget 提供令牌预算管理与成本控制能力。
Index ¶
- type Alert
- type AlertHandler
- type AlertType
- type BudgetConfig
- type BudgetStatus
- type TokenBudgetManager
- func (m *TokenBudgetManager) CheckBudget(ctx context.Context, estimatedTokens int, estimatedCost float64) error
- func (m *TokenBudgetManager) GetStatus() BudgetStatus
- func (m *TokenBudgetManager) OnAlert(handler AlertHandler)
- func (m *TokenBudgetManager) RecordUsage(record UsageRecord)
- func (m *TokenBudgetManager) Reset()
- type UsageRecord
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Alert ¶
type Alert struct {
Type AlertType `json:"type"`
Message string `json:"message"`
Threshold float64 `json:"threshold"`
Current float64 `json:"current"`
Timestamp time.Time `json:"timestamp"`
}
警报代表预算警报。
type BudgetConfig ¶
type BudgetConfig struct {
MaxTokensPerRequest int `json:"max_tokens_per_request"`
MaxTokensPerMinute int `json:"max_tokens_per_minute"`
MaxTokensPerHour int `json:"max_tokens_per_hour"`
MaxTokensPerDay int `json:"max_tokens_per_day"`
MaxCostPerRequest float64 `json:"max_cost_per_request"`
MaxCostPerDay float64 `json:"max_cost_per_day"`
AlertThreshold float64 `json:"alert_threshold"` // 0.0-1.0, alert when usage exceeds this
AutoThrottle bool `json:"auto_throttle"`
ThrottleDelay time.Duration `json:"throttle_delay"`
}
预算Config 配置符号预算管理 。
type BudgetStatus ¶
type BudgetStatus struct {
TokensUsedMinute int64 `json:"tokens_used_minute"`
TokensUsedHour int64 `json:"tokens_used_hour"`
TokensUsedDay int64 `json:"tokens_used_day"`
CostUsedDay float64 `json:"cost_used_day"`
MinuteUtilization float64 `json:"minute_utilization"`
HourUtilization float64 `json:"hour_utilization"`
DayUtilization float64 `json:"day_utilization"`
CostUtilization float64 `json:"cost_utilization"`
IsThrottled bool `json:"is_throttled"`
ThrottleUntil *time.Time `json:"throttle_until,omitempty"`
}
预算状况是目前的预算状况。
type TokenBudgetManager ¶
type TokenBudgetManager struct {
// contains filtered or unexported fields
}
TokenBudgetManager管理符名预算并强制执行限制.
func NewTokenBudgetManager ¶
func NewTokenBudgetManager(config BudgetConfig, logger *zap.Logger) *TokenBudgetManager
NewTokenBudgetManager 创建了新的代币预算管理器.
func (*TokenBudgetManager) CheckBudget ¶
func (m *TokenBudgetManager) CheckBudget(ctx context.Context, estimatedTokens int, estimatedCost float64) error
检查预算是否在预算范围内 。
func (*TokenBudgetManager) GetStatus ¶
func (m *TokenBudgetManager) GetStatus() BudgetStatus
Get Status 返回当前预算状况 。
func (*TokenBudgetManager) OnAlert ¶
func (m *TokenBudgetManager) OnAlert(handler AlertHandler)
OnAlert登记了一个警报处理器。
func (*TokenBudgetManager) RecordUsage ¶
func (m *TokenBudgetManager) RecordUsage(record UsageRecord)
记录Usage记录符和成本使用.
type UsageRecord ¶
type UsageRecord struct {
Timestamp time.Time `json:"timestamp"`
Tokens int `json:"tokens"`
Cost float64 `json:"cost"`
Model string `json:"model"`
RequestID string `json:"request_id"`
UserID string `json:"user_id,omitempty"`
AgentID string `json:"agent_id,omitempty"`
}
用法记录代表单一使用记录.
Click to show internal directories.
Click to hide internal directories.