Skip to content

Commit 4b5f7aa

Browse files
authored
refactor(model): 为价格信息添加模型详情支持 (#861)
* 在ModelInfo中添加ToResponse方法,将模型信息转换为API响应格式 * 为Price结构体新增ModelInfo字段,用于存储关联的模型详细信息 * 在GetAllPrices中集成模型信息获取逻辑,增强价格数据的完整性 * 支持显示模型的模态类型、标签、上下文长度等关键参数
1 parent 8653a3f commit 4b5f7aa

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

model/model_info.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package model
22

33
import (
44
"one-api/common/logger"
5+
"one-api/common/utils"
56
)
67

78
type ModelInfo struct {
@@ -19,6 +20,48 @@ type ModelInfo struct {
1920
UpdatedAt int64 `json:"updated_at" gorm:"autoUpdateTime"`
2021
}
2122

23+
type ModelInfoResponse struct {
24+
Model string `json:"model"`
25+
Name string `json:"name"`
26+
Description string `json:"description"`
27+
ContextLength int `json:"context_length"`
28+
MaxTokens int `json:"max_tokens"`
29+
InputModalities []string `json:"input_modalities"`
30+
OutputModalities []string `json:"output_modalities"`
31+
Tags []string `json:"tags"`
32+
SupportUrl []string `json:"support_url"`
33+
CreatedAt int64 `json:"created_at"`
34+
UpdatedAt int64 `json:"updated_at"`
35+
}
36+
37+
func (m *ModelInfo) ToResponse() *ModelInfoResponse {
38+
res := &ModelInfoResponse{
39+
Model: m.Model,
40+
Name: m.Name,
41+
Description: m.Description,
42+
ContextLength: m.ContextLength,
43+
MaxTokens: m.MaxTokens,
44+
CreatedAt: m.CreatedAt,
45+
UpdatedAt: m.UpdatedAt,
46+
}
47+
48+
res.InputModalities, _ = utils.UnmarshalString[[]string](m.InputModalities)
49+
res.OutputModalities, _ = utils.UnmarshalString[[]string](m.OutputModalities)
50+
res.Tags, _ = utils.UnmarshalString[[]string](m.Tags)
51+
52+
var err error
53+
res.SupportUrl, err = utils.UnmarshalString[[]string](m.SupportUrl)
54+
if err != nil {
55+
if m.SupportUrl != "" {
56+
res.SupportUrl = []string{m.SupportUrl}
57+
} else {
58+
res.SupportUrl = []string{}
59+
}
60+
}
61+
62+
return res
63+
}
64+
2265
func (m *ModelInfo) TableName() string {
2366
return "model_info"
2467
}

model/price.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type Price struct {
6262
Locked bool `json:"locked" gorm:"default:false"` // 如果模型为locked 则覆盖模式不会更新locked的模型价格
6363

6464
ExtraRatios *datatypes.JSONType[map[string]float64] `json:"extra_ratios,omitempty" gorm:"type:json"`
65+
ModelInfo *ModelInfoResponse `json:"model_info,omitempty" gorm:"-"`
6566
}
6667

6768
func GetAllPrices() ([]*Price, error) {

model/pricing.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ func (p *Pricing) Init() error {
7777
return nil
7878
}
7979

80+
modelInfos, err := GetAllModelInfo()
81+
if err == nil {
82+
modelInfoMap := make(map[string]*ModelInfoResponse)
83+
for _, info := range modelInfos {
84+
modelInfoMap[info.Model] = info.ToResponse()
85+
}
86+
87+
for _, price := range prices {
88+
if info, ok := modelInfoMap[price.Model]; ok {
89+
price.ModelInfo = info
90+
}
91+
}
92+
} else {
93+
logger.SysError("Failed to fetch model infos: " + err.Error())
94+
}
95+
8096
newPrices := make(map[string]*Price)
8197
newMatch := make(map[string]bool)
8298

0 commit comments

Comments
 (0)