From 67332bc8dfb1c8a89266318215c9d2e2d03eb451 Mon Sep 17 00:00:00 2001 From: CaIon <1808837298@qq.com> Date: Sat, 23 Mar 2024 17:19:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=A8=A1=E5=9E=8B=E5=9B=BA=E5=AE=9A?= =?UTF-8?q?=E4=BB=B7=E6=A0=BC=E4=B8=BA=E7=A9=BA=E6=97=B6=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E9=BB=98=E8=AE=A4=E4=BB=B7=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/model-ratio.go | 38 +++++++++++++++++++------------------- model/option.go | 8 ++++---- service/token_counter.go | 2 +- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/common/model-ratio.go b/common/model-ratio.go index 7e361053..ca669d14 100644 --- a/common/model-ratio.go +++ b/common/model-ratio.go @@ -6,7 +6,7 @@ import ( "time" ) -// ModelRatio +// modelRatio // https://platform.openai.com/docs/models/model-endpoint-compatibility // https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Blfmc9dlf // https://openai.com/pricing @@ -114,14 +114,14 @@ var DefaultModelPrice = map[string]float64{ "swap_face": 0.05, } -var ModelPrice = map[string]float64{} -var ModelRatio = map[string]float64{} +var modelPrice map[string]float64 = nil +var modelRatio map[string]float64 = nil func ModelPrice2JSONString() string { - if len(ModelPrice) == 0 { - ModelPrice = DefaultModelPrice + if modelPrice == nil { + modelPrice = DefaultModelPrice } - jsonBytes, err := json.Marshal(ModelPrice) + jsonBytes, err := json.Marshal(modelPrice) if err != nil { SysError("error marshalling model price: " + err.Error()) } @@ -129,18 +129,18 @@ func ModelPrice2JSONString() string { } func UpdateModelPriceByJSONString(jsonStr string) error { - ModelPrice = make(map[string]float64) - return json.Unmarshal([]byte(jsonStr), &ModelPrice) + modelPrice = make(map[string]float64) + return json.Unmarshal([]byte(jsonStr), &modelPrice) } func GetModelPrice(name string, printErr bool) float64 { - if len(ModelPrice) == 0 { - ModelPrice = DefaultModelPrice + if modelPrice == nil { + modelPrice = DefaultModelPrice } if strings.HasPrefix(name, "gpt-4-gizmo") { name = "gpt-4-gizmo-*" } - price, ok := ModelPrice[name] + price, ok := modelPrice[name] if !ok { if printErr { SysError("model price not found: " + name) @@ -151,10 +151,10 @@ func GetModelPrice(name string, printErr bool) float64 { } func ModelRatio2JSONString() string { - if len(ModelRatio) == 0 { - ModelRatio = DefaultModelRatio + if modelRatio == nil { + modelRatio = DefaultModelRatio } - jsonBytes, err := json.Marshal(ModelRatio) + jsonBytes, err := json.Marshal(modelRatio) if err != nil { SysError("error marshalling model ratio: " + err.Error()) } @@ -162,18 +162,18 @@ func ModelRatio2JSONString() string { } func UpdateModelRatioByJSONString(jsonStr string) error { - ModelRatio = make(map[string]float64) - return json.Unmarshal([]byte(jsonStr), &ModelRatio) + modelRatio = make(map[string]float64) + return json.Unmarshal([]byte(jsonStr), &modelRatio) } func GetModelRatio(name string) float64 { - if len(ModelRatio) == 0 { - ModelRatio = DefaultModelRatio + if modelRatio == nil { + modelRatio = DefaultModelRatio } if strings.HasPrefix(name, "gpt-4-gizmo") { name = "gpt-4-gizmo-*" } - ratio, ok := ModelRatio[name] + ratio, ok := modelRatio[name] if !ok { SysError("model ratio not found: " + name) return 30 diff --git a/model/option.go b/model/option.go index 46e41da4..6483e26f 100644 --- a/model/option.go +++ b/model/option.go @@ -78,8 +78,8 @@ func InitOptionMap() { common.OptionMap["QuotaForInvitee"] = strconv.Itoa(common.QuotaForInvitee) common.OptionMap["QuotaRemindThreshold"] = strconv.Itoa(common.QuotaRemindThreshold) common.OptionMap["PreConsumedQuota"] = strconv.Itoa(common.PreConsumedQuota) - common.OptionMap["ModelRatio"] = common.ModelRatio2JSONString() - common.OptionMap["ModelPrice"] = common.ModelPrice2JSONString() + common.OptionMap["modelRatio"] = common.ModelRatio2JSONString() + common.OptionMap["modelPrice"] = common.ModelPrice2JSONString() common.OptionMap["GroupRatio"] = common.GroupRatio2JSONString() common.OptionMap["TopUpLink"] = common.TopUpLink common.OptionMap["ChatLink"] = common.ChatLink @@ -271,11 +271,11 @@ func updateOptionMap(key string, value string) (err error) { common.DataExportInterval, _ = strconv.Atoi(value) case "DataExportDefaultTime": common.DataExportDefaultTime = value - case "ModelRatio": + case "modelRatio": err = common.UpdateModelRatioByJSONString(value) case "GroupRatio": err = common.UpdateGroupRatioByJSONString(value) - case "ModelPrice": + case "modelPrice": err = common.UpdateModelPriceByJSONString(value) case "TopUpLink": common.TopUpLink = value diff --git a/service/token_counter.go b/service/token_counter.go index 4769dab5..2147e880 100644 --- a/service/token_counter.go +++ b/service/token_counter.go @@ -29,7 +29,7 @@ func InitTokenEncoders() { if err != nil { common.FatalLog(fmt.Sprintf("failed to get gpt-4 token encoder: %s", err.Error())) } - for model, _ := range common.ModelRatio { + for model, _ := range common.DefaultModelRatio { if strings.HasPrefix(model, "gpt-3.5") { tokenEncoderMap[model] = gpt35TokenEncoder } else if strings.HasPrefix(model, "gpt-4") {