104 lines
3.9 KiB
Go
104 lines
3.9 KiB
Go
// Package llm 映射外部 LLM 配置中心 MySQL 的三张表
|
||
//
|
||
// 严格只读:本服务永远不应该 INSERT / UPDATE / DELETE 这些表,
|
||
// DAO 中只用 Find / First / Where。所有写操作由配置中心自身负责。
|
||
package llm
|
||
|
||
import "time"
|
||
|
||
// 模型类型枚举(对应 t_llm_model.model_type)
|
||
const (
|
||
ModelTypeChat = 1 // 文字对话
|
||
ModelTypeVision = 2 // 视觉(图文)
|
||
ModelTypeMultimodal = 3 // 多模态
|
||
ModelTypeASR = 4 // 语音识别(本服务转写功能用此类型)
|
||
)
|
||
|
||
// 通用状态枚举
|
||
const (
|
||
StatusDisabled = 0 // 停用
|
||
StatusEnabled = 1 // 启用
|
||
// t_llm_key 额外定义
|
||
KeyStatusAutoDisabled = 2 // 连续失败自动停用
|
||
)
|
||
|
||
// API 协议
|
||
const (
|
||
APIProtocolOpenAICompatible = "openai_compatible"
|
||
APIProtocolCustom = "custom"
|
||
)
|
||
|
||
// Provider 大模型提供商表 t_llm_provider(只读)
|
||
//
|
||
// 选择 provider 时使用:deleted=0 AND status=1 ORDER BY sort ASC
|
||
type Provider struct {
|
||
ID int64 `gorm:"column:id;primaryKey"`
|
||
ProviderName string `gorm:"column:provider_name"`
|
||
ProviderCode string `gorm:"column:provider_code"`
|
||
BaseURL string `gorm:"column:base_url"`
|
||
APIProtocol string `gorm:"column:api_protocol"` // openai_compatible / custom
|
||
Status int `gorm:"column:status"`
|
||
Sort int `gorm:"column:sort"`
|
||
Remark string `gorm:"column:remark"`
|
||
CreateTime time.Time `gorm:"column:create_time"`
|
||
UpdateTime time.Time `gorm:"column:update_time"`
|
||
Deleted int `gorm:"column:deleted"`
|
||
}
|
||
|
||
// TableName 指定 MySQL 表名
|
||
func (Provider) TableName() string { return "t_llm_provider" }
|
||
|
||
// Model 大模型表 t_llm_model(只读)
|
||
//
|
||
// 选择 STT 模型时使用:deleted=0 AND status=1 AND model_type=4 ORDER BY sort ASC
|
||
type Model struct {
|
||
ID int64 `gorm:"column:id;primaryKey"`
|
||
ProviderID int64 `gorm:"column:provider_id"`
|
||
ModelCode string `gorm:"column:model_code"`
|
||
ModelName string `gorm:"column:model_name"`
|
||
ModelType int `gorm:"column:model_type"`
|
||
MaxTokens int `gorm:"column:max_tokens"`
|
||
InputPrice float64 `gorm:"column:input_price"`
|
||
OutputPrice float64 `gorm:"column:output_price"`
|
||
Status int `gorm:"column:status"`
|
||
Sort int `gorm:"column:sort"`
|
||
Remark string `gorm:"column:remark"`
|
||
CreateTime time.Time `gorm:"column:create_time"`
|
||
UpdateTime time.Time `gorm:"column:update_time"`
|
||
Deleted int `gorm:"column:deleted"`
|
||
}
|
||
|
||
func (Model) TableName() string { return "t_llm_model" }
|
||
|
||
// Key 大模型 Key 池表 t_llm_key(只读)
|
||
//
|
||
// 选择策略(DAO.PickKey):
|
||
// - WHERE deleted=0 AND status=1 AND provider_id=?
|
||
// - AND (expire_time IS NULL OR expire_time > NOW())
|
||
// - AND (daily_limit = 0 OR today_count < daily_limit)
|
||
// - ORDER BY weight DESC, today_count ASC
|
||
// - LIMIT 1
|
||
//
|
||
// 注意:本服务不写回 today_count / total_count / fail_count,
|
||
// 因为这些字段属于 LLM 网关自身的统计职责,由网关在调用时自增。
|
||
// 我们这边仅消费它们做选择权重。
|
||
type Key struct {
|
||
ID int64 `gorm:"column:id;primaryKey"`
|
||
ProviderID int64 `gorm:"column:provider_id"`
|
||
APIKey string `gorm:"column:api_key"`
|
||
KeyAlias string `gorm:"column:key_alias"`
|
||
Weight int `gorm:"column:weight"`
|
||
DailyLimit int `gorm:"column:daily_limit"`
|
||
TodayCount int `gorm:"column:today_count"`
|
||
TotalCount int64 `gorm:"column:total_count"`
|
||
FailCount int `gorm:"column:fail_count"`
|
||
Status int `gorm:"column:status"` // 0/1/2
|
||
LastUsedTime *time.Time `gorm:"column:last_used_time"`
|
||
ExpireTime *time.Time `gorm:"column:expire_time"`
|
||
CreateTime time.Time `gorm:"column:create_time"`
|
||
UpdateTime time.Time `gorm:"column:update_time"`
|
||
Deleted int `gorm:"column:deleted"`
|
||
}
|
||
|
||
func (Key) TableName() string { return "t_llm_key" }
|