66 lines
2.7 KiB
Go
66 lines
2.7 KiB
Go
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"` // 服务商名称,唯一索引
|
||
BaseURL string `gorm:"not null"` // API基础URL
|
||
ApiKey string `gorm:"not null"` // 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"` // API密钥ID
|
||
VirtualModelName string `gorm:"index"` // 虚拟模型名称
|
||
BackendModelName string `gorm:"index"` // 后端模型名称
|
||
RequestTimestamp time.Time `gorm:"index;not null"` // 请求时间戳
|
||
ResponseTimestamp time.Time `gorm:"not null"` // 响应时间戳
|
||
RequestTokens int `gorm:"default:0"` // 请求token数
|
||
ResponseTokens int `gorm:"default:0"` // 响应token数
|
||
Cost float64 `gorm:"type:decimal(10,6)"` // 成本
|
||
RequestBody string `gorm:"type:text"` // 请求体
|
||
ResponseBody string `gorm:"type:text"` // 响应体
|
||
}
|