Просмотр исходного кода

feat: support configuring ratio when estimating token number in stream mode

JustSong 2 лет назад
Родитель
Сommit
480e789cd8
4 измененных файлов с 21 добавлено и 1 удалено
  1. 1 0
      common/constants.go
  2. 1 1
      controller/relay.go
  3. 3 0
      model/option.go
  4. 16 0
      web/src/components/SystemSetting.js

+ 1 - 0
common/constants.go

@@ -48,6 +48,7 @@ var TurnstileSiteKey = ""
 var TurnstileSecretKey = ""
 
 var QuotaForNewUser = 100
+var BytesNumber2Quota = 0.8
 
 const (
 	RoleGuestUser  = 0

+ 1 - 1
controller/relay.go

@@ -110,7 +110,7 @@ func relayHelper(c *gin.Context) error {
 		if consumeQuota {
 			quota := 0
 			if isStream {
-				quota = int(float64(len(streamResponseText)) * 0.8)
+				quota = int(float64(len(streamResponseText)) * common.BytesNumber2Quota)
 			} else {
 				quota = textResponse.Usage.TotalTokens
 			}

+ 3 - 0
model/option.go

@@ -47,6 +47,7 @@ func InitOptionMap() {
 	common.OptionMap["TurnstileSiteKey"] = ""
 	common.OptionMap["TurnstileSecretKey"] = ""
 	common.OptionMap["QuotaForNewUser"] = strconv.Itoa(common.QuotaForNewUser)
+	common.OptionMap["BytesNumber2Quota"] = strconv.FormatFloat(common.BytesNumber2Quota, 'f', -1, 64)
 	common.OptionMap["TopUpLink"] = common.TopUpLink
 	common.OptionMapRWMutex.Unlock()
 	options, _ := AllOption()
@@ -135,6 +136,8 @@ func updateOptionMap(key string, value string) {
 		common.TurnstileSecretKey = value
 	case "QuotaForNewUser":
 		common.QuotaForNewUser, _ = strconv.Atoi(value)
+	case "BytesNumber2Quota":
+		common.BytesNumber2Quota, _ = strconv.ParseFloat(value, 64)
 	case "TopUpLink":
 		common.TopUpLink = value
 	}

+ 16 - 0
web/src/components/SystemSetting.js

@@ -25,6 +25,7 @@ const SystemSetting = () => {
     TurnstileSecretKey: '',
     RegisterEnabled: '',
     QuotaForNewUser: 0,
+    BytesNumber2Quota: 0.8,
     TopUpLink: ''
   });
   let originInputs = {};
@@ -90,6 +91,7 @@ const SystemSetting = () => {
       name === 'TurnstileSiteKey' ||
       name === 'TurnstileSecretKey' ||
       name === 'QuotaForNewUser' ||
+      name === 'BytesNumber2Quota' ||
       name === 'TopUpLink'
     ) {
       setInputs((inputs) => ({ ...inputs, [name]: value }));
@@ -107,6 +109,9 @@ const SystemSetting = () => {
     if (originInputs['QuotaForNewUser'] !== inputs.QuotaForNewUser) {
       await updateOption('QuotaForNewUser', inputs.QuotaForNewUser);
     }
+    if (originInputs['BytesNumber2Quota'] !== inputs.BytesNumber2Quota) {
+      await updateOption('BytesNumber2Quota', inputs.BytesNumber2Quota);
+    }
     if (originInputs['TopUpLink'] !== inputs.TopUpLink) {
       await updateOption('TopUpLink', inputs.TopUpLink);
     }
@@ -255,6 +260,17 @@ const SystemSetting = () => {
               min='0'
               placeholder='例如:100'
             />
+            <Form.Input
+              label='Stream 模式下估算 token 时所使用的倍率'
+              name='BytesNumber2Quota'
+              onChange={handleInputChange}
+              autoComplete='off'
+              value={inputs.BytesNumber2Quota}
+              type='number'
+              step='0.01'
+              min='0'
+              placeholder='例如:0.8'
+            />
             <Form.Input
               label='充值链接'
               name='TopUpLink'