package models import ( "time" "gorm.io/gorm" ) // BillingMethod 计费方式常量 const ( BillingMethodToken = "token" // 按token计费 BillingMethodRequest = "request" // 按请求次数计费 ) // Provider 代表一个AI服务提供商 type Provider struct { gorm.Model Name string `gorm:"uniqueIndex;not null" json:"name"` // 服务商名称,唯一索引 BaseURL string `gorm:"not null" json:"base_url"` // API基础URL ApiKey string `gorm:"not null" json:"api_key"` // API密钥 APIVersion string `json:"api_version"` // API版本(可选) } // APIKey 用于网关本身的API认证 type APIKey struct { gorm.Model Key string `gorm:"uniqueIndex;not null"` // API密钥字符串,唯一索引 } // VirtualModel 用户与之交互的虚拟模型 type VirtualModel struct { gorm.Model Name string `gorm:"uniqueIndex;not null"` // 虚拟模型名称,唯一索引 BackendModels []BackendModel `gorm:"foreignKey:VirtualModelID"` // 关联的后端模型列表 } // BackendModel 实际的后端AI模型 type BackendModel struct { gorm.Model VirtualModelID uint `gorm:"index;not null"` // 关联的虚拟模型ID ProviderID uint `gorm:"index;not null"` // 关联的服务商ID Provider Provider `gorm:"foreignKey:ProviderID"` // GORM关联 Name string `gorm:"not null"` // 后端模型名称 Priority int `gorm:"not null"` // 优先级(数字越小优先级越高) MaxContextLength int `gorm:"not null"` // 最大上下文长度 BillingMethod string `gorm:"not null"` // 计费方式 PromptTokenPrice float64 `gorm:"type:decimal(10,6)"` // 输入token单价 CompletionTokenPrice float64 `gorm:"type:decimal(10,6)"` // 输出token单价 FixedPrice float64 `gorm:"type:decimal(10,2)"` // 固定价格(按次计费) CostThreshold float64 `gorm:"type:decimal(10,6)"` // 成本阈值 } // RequestLog 记录每次API请求的详细信息 type RequestLog struct { gorm.Model APIKeyID uint `gorm:"index" json:"api_key_id"` // API密钥ID ProviderName string `gorm:"index" json:"provider_name"` // 服务商名称 VirtualModelName string `gorm:"index" json:"virtual_model_name"` // 虚拟模型名称 BackendModelName string `gorm:"index" json:"backend_model_name"` // 后端模型名称 RequestTimestamp time.Time `gorm:"index;not null" json:"request_timestamp"` // 请求时间戳 ResponseTimestamp time.Time `gorm:"not null" json:"response_timestamp"` // 响应时间戳 RequestTokens int `gorm:"default:0" json:"request_tokens"` // 请求token数 ResponseTokens int `gorm:"default:0" json:"response_tokens"` // 响应token数 Cost float64 `gorm:"type:decimal(10,6)" json:"cost"` // 成本 RequestBody string `gorm:"type:text" json:"request_body"` // 请求体 ResponseBody string `gorm:"type:text" json:"response_body"` // 响应体 }