✨ feat: auto update new model ratio to database
This commit is contained in:
parent
2b6d11f999
commit
3b8ae9a6cd
@ -138,6 +138,35 @@ func UpdateModelRatioByJSONString(jsonStr string) error {
|
|||||||
return json.Unmarshal([]byte(jsonStr), &ModelRatio)
|
return json.Unmarshal([]byte(jsonStr), &ModelRatio)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MergeModelRatioByJSONString(jsonStr string) (newJsonStr string, err error) {
|
||||||
|
inputModelRatio := make(map[string]float64)
|
||||||
|
err = json.Unmarshal([]byte(jsonStr), &inputModelRatio)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
isNew := false
|
||||||
|
// 与现有的ModelRatio进行比较,如果有新增的模型,需要添加
|
||||||
|
for key, value := range ModelRatio {
|
||||||
|
if _, ok := inputModelRatio[key]; !ok {
|
||||||
|
isNew = true
|
||||||
|
inputModelRatio[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isNew {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var jsonBytes []byte
|
||||||
|
jsonBytes, err = json.Marshal(inputModelRatio)
|
||||||
|
if err != nil {
|
||||||
|
SysError("error marshalling model ratio: " + err.Error())
|
||||||
|
}
|
||||||
|
newJsonStr = string(jsonBytes)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func GetModelRatio(name string) float64 {
|
func GetModelRatio(name string) float64 {
|
||||||
if strings.HasPrefix(name, "qwen-") && strings.HasSuffix(name, "-internet") {
|
if strings.HasPrefix(name, "qwen-") && strings.HasSuffix(name, "-internet") {
|
||||||
name = strings.TrimSuffix(name, "-internet")
|
name = strings.TrimSuffix(name, "-internet")
|
||||||
|
@ -19,6 +19,11 @@ func AllOption() ([]*Option, error) {
|
|||||||
return options, err
|
return options, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetOption(key string) (option Option, err error) {
|
||||||
|
err = DB.First(&option, Option{Key: key}).Error
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func InitOptionMap() {
|
func InitOptionMap() {
|
||||||
common.OptionMapRWMutex.Lock()
|
common.OptionMapRWMutex.Lock()
|
||||||
common.OptionMap = make(map[string]string)
|
common.OptionMap = make(map[string]string)
|
||||||
@ -73,9 +78,28 @@ func InitOptionMap() {
|
|||||||
common.OptionMap["QuotaPerUnit"] = strconv.FormatFloat(common.QuotaPerUnit, 'f', -1, 64)
|
common.OptionMap["QuotaPerUnit"] = strconv.FormatFloat(common.QuotaPerUnit, 'f', -1, 64)
|
||||||
common.OptionMap["RetryTimes"] = strconv.Itoa(common.RetryTimes)
|
common.OptionMap["RetryTimes"] = strconv.Itoa(common.RetryTimes)
|
||||||
common.OptionMapRWMutex.Unlock()
|
common.OptionMapRWMutex.Unlock()
|
||||||
|
initModelRatio()
|
||||||
loadOptionsFromDatabase()
|
loadOptionsFromDatabase()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initModelRatio() {
|
||||||
|
// 查询数据库中的ModelRatio
|
||||||
|
option, err := GetOption("ModelRatio")
|
||||||
|
if err != nil || option.Value == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
newModelRatio, err := common.MergeModelRatioByJSONString(option.Value)
|
||||||
|
if err != nil || newModelRatio == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新数据库中的ModelRatio
|
||||||
|
common.SysLog("update ModelRatio")
|
||||||
|
UpdateOption("ModelRatio", newModelRatio)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func loadOptionsFromDatabase() {
|
func loadOptionsFromDatabase() {
|
||||||
options, _ := AllOption()
|
options, _ := AllOption()
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
|
Loading…
Reference in New Issue
Block a user