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

This commit is contained in:
JustSong 2023-04-28 17:25:05 +08:00
parent 23ec541ba6
commit 480e789cd8
4 changed files with 21 additions and 1 deletions

View File

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

View File

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

View File

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

View File

@ -25,6 +25,7 @@ const SystemSetting = () => {
TurnstileSecretKey: '', TurnstileSecretKey: '',
RegisterEnabled: '', RegisterEnabled: '',
QuotaForNewUser: 0, QuotaForNewUser: 0,
BytesNumber2Quota: 0.8,
TopUpLink: '' TopUpLink: ''
}); });
let originInputs = {}; let originInputs = {};
@ -90,6 +91,7 @@ const SystemSetting = () => {
name === 'TurnstileSiteKey' || name === 'TurnstileSiteKey' ||
name === 'TurnstileSecretKey' || name === 'TurnstileSecretKey' ||
name === 'QuotaForNewUser' || name === 'QuotaForNewUser' ||
name === 'BytesNumber2Quota' ||
name === 'TopUpLink' name === 'TopUpLink'
) { ) {
setInputs((inputs) => ({ ...inputs, [name]: value })); setInputs((inputs) => ({ ...inputs, [name]: value }));
@ -107,6 +109,9 @@ const SystemSetting = () => {
if (originInputs['QuotaForNewUser'] !== inputs.QuotaForNewUser) { if (originInputs['QuotaForNewUser'] !== inputs.QuotaForNewUser) {
await updateOption('QuotaForNewUser', inputs.QuotaForNewUser); await updateOption('QuotaForNewUser', inputs.QuotaForNewUser);
} }
if (originInputs['BytesNumber2Quota'] !== inputs.BytesNumber2Quota) {
await updateOption('BytesNumber2Quota', inputs.BytesNumber2Quota);
}
if (originInputs['TopUpLink'] !== inputs.TopUpLink) { if (originInputs['TopUpLink'] !== inputs.TopUpLink) {
await updateOption('TopUpLink', inputs.TopUpLink); await updateOption('TopUpLink', inputs.TopUpLink);
} }
@ -255,6 +260,17 @@ const SystemSetting = () => {
min='0' min='0'
placeholder='例如100' 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 <Form.Input
label='充值链接' label='充值链接'
name='TopUpLink' name='TopUpLink'