mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-17 17:16:38 +08:00
feat: atomatically migrate old ratio to new
This commit is contained in:
parent
215e59b76d
commit
56df7c097a
@ -83,15 +83,35 @@ func InitOptionMap() {
|
|||||||
|
|
||||||
func loadOptionsFromDatabase() {
|
func loadOptionsFromDatabase() {
|
||||||
options, _ := AllOption()
|
options, _ := AllOption()
|
||||||
|
var oldModelRatio string
|
||||||
|
var oldCompletionRatio string
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
if option.Key == "ModelRatio" {
|
if option.Key == "ModelRatio" {
|
||||||
|
oldModelRatio = option.Value
|
||||||
option.Value = billingratio.AddNewMissingRatio(option.Value)
|
option.Value = billingratio.AddNewMissingRatio(option.Value)
|
||||||
}
|
}
|
||||||
|
if option.Key == "CompletionRatio" {
|
||||||
|
oldCompletionRatio = option.Value
|
||||||
|
}
|
||||||
err := updateOptionMap(option.Key, option.Value)
|
err := updateOptionMap(option.Key, option.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.SysError("failed to update option map: " + err.Error())
|
logger.SysError("failed to update option map: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, option := range options {
|
||||||
|
if option.Key == "Ratio" {
|
||||||
|
option.Value = billingratio.AddOldRatio(oldModelRatio, oldCompletionRatio)
|
||||||
|
err := updateOptionMap(option.Key, option.Value)
|
||||||
|
if err != nil {
|
||||||
|
logger.SysError("failed to update option map: " + err.Error())
|
||||||
|
}
|
||||||
|
err = UpdateOption(option.Key, option.Value)
|
||||||
|
if err != nil {
|
||||||
|
logger.SysError("failed to update option map: " + err.Error())
|
||||||
|
}
|
||||||
|
logger.SysLog("ratio merged")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SyncOptions(frequency int) {
|
func SyncOptions(frequency int) {
|
||||||
|
@ -3,6 +3,7 @@ package ratio
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/songquanpeng/one-api/common/logger"
|
"github.com/songquanpeng/one-api/common/logger"
|
||||||
@ -394,6 +395,58 @@ func AddNewMissingRatio(oldRatio string) string {
|
|||||||
return string(jsonBytes)
|
return string(jsonBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AddOldRatio(oldRatio string, oldCompletionRatio string) string {
|
||||||
|
modelRatio := make(map[string]float64)
|
||||||
|
if oldRatio != "" {
|
||||||
|
err := json.Unmarshal([]byte(oldRatio), &modelRatio)
|
||||||
|
if err != nil {
|
||||||
|
logger.SysError("error unmarshalling old ratio: " + err.Error())
|
||||||
|
return oldRatio
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
completionRatio := make(map[string]float64)
|
||||||
|
if oldCompletionRatio != "" {
|
||||||
|
err := json.Unmarshal([]byte(oldCompletionRatio), &completionRatio)
|
||||||
|
if err != nil {
|
||||||
|
logger.SysError("error unmarshalling old completion ratio: " + err.Error())
|
||||||
|
return oldCompletionRatio
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
newRatio := make(map[string]Ratio)
|
||||||
|
|
||||||
|
for k, v := range DefaultRatio {
|
||||||
|
if _, ok := newRatio[k]; !ok {
|
||||||
|
newRatio[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range modelRatio {
|
||||||
|
if _, ok := DefaultRatio[k]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
modelName, channelType := SplitModelName(k)
|
||||||
|
ratio := Ratio{}
|
||||||
|
ratio.Input = v
|
||||||
|
|
||||||
|
if val, ok := completionRatio[k]; ok {
|
||||||
|
ratio.Output = v * val
|
||||||
|
} else {
|
||||||
|
ratio.Output = v * GetCompletionRatio(modelName, channelType)
|
||||||
|
}
|
||||||
|
|
||||||
|
newRatio[k] = ratio
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonBytes, err := json.Marshal(newRatio)
|
||||||
|
if err != nil {
|
||||||
|
logger.SysError("error marshalling new ratio: " + err.Error())
|
||||||
|
return oldRatio
|
||||||
|
}
|
||||||
|
return string(jsonBytes)
|
||||||
|
}
|
||||||
|
|
||||||
func ModelRatio2JSONString() string {
|
func ModelRatio2JSONString() string {
|
||||||
jsonBytes, err := json.Marshal(ModelRatio)
|
jsonBytes, err := json.Marshal(ModelRatio)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -444,6 +497,18 @@ func UpdateCompletionRatioByJSONString(jsonStr string) error {
|
|||||||
return json.Unmarshal([]byte(jsonStr), &CompletionRatio)
|
return json.Unmarshal([]byte(jsonStr), &CompletionRatio)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SplitModelName(name string) (string, int) {
|
||||||
|
model := strings.Split(name, "(")
|
||||||
|
modelName := model[0]
|
||||||
|
channelType := 0
|
||||||
|
if len(model) > 1 {
|
||||||
|
if v, err := strconv.Atoi(model[1]); err == nil {
|
||||||
|
channelType = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return modelName, channelType
|
||||||
|
}
|
||||||
|
|
||||||
func GetCompletionRatio(name string, channelType int) float64 {
|
func GetCompletionRatio(name string, channelType int) 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")
|
||||||
|
Loading…
Reference in New Issue
Block a user