Skip to content

Commit d93cb8f

Browse files
committed
feat: able to configure ratio for different models (close #26)
1 parent b08cd7e commit d93cb8f

File tree

4 files changed

+68
-17
lines changed

4 files changed

+68
-17
lines changed

common/constants.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ var TurnstileSiteKey = ""
4848
var TurnstileSecretKey = ""
4949

5050
var QuotaForNewUser = 100
51-
var BytesNumber2Quota = 0.8
51+
52+
// https://platform.openai.com/docs/models/model-endpoint-compatibility
53+
var RatioGPT3dot5 float64 = 2
54+
var RatioGPT4 float64 = 30
55+
var RatioGPT4_32k float64 = 60
5256

5357
const (
5458
RoleGuestUser = 0

controller/relay.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ func relayHelper(c *gin.Context) error {
129129
} else {
130130
quota = textResponse.Usage.TotalTokens
131131
}
132+
ratio := common.RatioGPT3dot5
133+
if strings.HasPrefix(textRequest.Model, "gpt-4-32k") {
134+
ratio = common.RatioGPT4_32k
135+
} else if strings.HasPrefix(textRequest.Model, "gpt-4") {
136+
ratio = common.RatioGPT4
137+
} else {
138+
ratio = common.RatioGPT3dot5
139+
}
140+
quota = int(float64(quota) * ratio)
132141
err := model.ConsumeTokenQuota(tokenId, quota)
133142
if err != nil {
134143
common.SysError("Error consuming token remain quota: " + err.Error())

model/option.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ func InitOptionMap() {
4747
common.OptionMap["TurnstileSiteKey"] = ""
4848
common.OptionMap["TurnstileSecretKey"] = ""
4949
common.OptionMap["QuotaForNewUser"] = strconv.Itoa(common.QuotaForNewUser)
50-
common.OptionMap["BytesNumber2Quota"] = strconv.FormatFloat(common.BytesNumber2Quota, 'f', -1, 64)
50+
common.OptionMap["RatioGPT3dot5"] = strconv.FormatFloat(common.RatioGPT3dot5, 'f', -1, 64)
51+
common.OptionMap["RatioGPT4"] = strconv.FormatFloat(common.RatioGPT4, 'f', -1, 64)
52+
common.OptionMap["RatioGPT4_32k"] = strconv.FormatFloat(common.RatioGPT4_32k, 'f', -1, 64)
5153
common.OptionMap["TopUpLink"] = common.TopUpLink
5254
common.OptionMapRWMutex.Unlock()
5355
options, _ := AllOption()
@@ -136,8 +138,12 @@ func updateOptionMap(key string, value string) {
136138
common.TurnstileSecretKey = value
137139
case "QuotaForNewUser":
138140
common.QuotaForNewUser, _ = strconv.Atoi(value)
139-
case "BytesNumber2Quota":
140-
common.BytesNumber2Quota, _ = strconv.ParseFloat(value, 64)
141+
case "RatioGPT3dot5":
142+
common.RatioGPT3dot5, _ = strconv.ParseFloat(value, 64)
143+
case "RatioGPT4":
144+
common.RatioGPT4, _ = strconv.ParseFloat(value, 64)
145+
case "RatioGPT4_32k":
146+
common.RatioGPT4_32k, _ = strconv.ParseFloat(value, 64)
141147
case "TopUpLink":
142148
common.TopUpLink = value
143149
}

web/src/components/SystemSetting.js

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ const SystemSetting = () => {
2525
TurnstileSecretKey: '',
2626
RegisterEnabled: '',
2727
QuotaForNewUser: 0,
28-
BytesNumber2Quota: 0.8,
28+
RatioGPT3dot5: 2,
29+
RatioGPT4: 30,
30+
RatioGPT4_32k: 60,
2931
TopUpLink: ''
3032
});
3133
let originInputs = {};
@@ -91,7 +93,7 @@ const SystemSetting = () => {
9193
name === 'TurnstileSiteKey' ||
9294
name === 'TurnstileSecretKey' ||
9395
name === 'QuotaForNewUser' ||
94-
name === 'BytesNumber2Quota' ||
96+
name.startsWith('Ratio') ||
9597
name === 'TopUpLink'
9698
) {
9799
setInputs((inputs) => ({ ...inputs, [name]: value }));
@@ -109,8 +111,14 @@ const SystemSetting = () => {
109111
if (originInputs['QuotaForNewUser'] !== inputs.QuotaForNewUser) {
110112
await updateOption('QuotaForNewUser', inputs.QuotaForNewUser);
111113
}
112-
if (originInputs['BytesNumber2Quota'] !== inputs.BytesNumber2Quota) {
113-
await updateOption('BytesNumber2Quota', inputs.BytesNumber2Quota);
114+
if (originInputs['RatioGPT3dot5'] !== inputs.RatioGPT3dot5) {
115+
await updateOption('RatioGPT3dot5', inputs.RatioGPT3dot5);
116+
}
117+
if (originInputs['RatioGPT4'] !== inputs.RatioGPT4) {
118+
await updateOption('RatioGPT4', inputs.RatioGPT4);
119+
}
120+
if (originInputs['RatioGPT4_32k'] !== inputs.RatioGPT4_32k) {
121+
await updateOption('RatioGPT4_32k', inputs.RatioGPT4_32k);
114122
}
115123
if (originInputs['TopUpLink'] !== inputs.TopUpLink) {
116124
await updateOption('TopUpLink', inputs.TopUpLink);
@@ -261,24 +269,48 @@ const SystemSetting = () => {
261269
placeholder='例如:100'
262270
/>
263271
<Form.Input
264-
label='Stream 模式下估算 token 时所使用的倍率'
265-
name='BytesNumber2Quota'
272+
label='充值链接'
273+
name='TopUpLink'
274+
onChange={handleInputChange}
275+
autoComplete='off'
276+
value={inputs.TopUpLink}
277+
type='link'
278+
placeholder='例如发卡网站的购买链接'
279+
/>
280+
</Form.Group>
281+
<Form.Group widths={3}>
282+
<Form.Input
283+
label='GPT-3.5 系列模型倍率'
284+
name='RatioGPT3dot5'
266285
onChange={handleInputChange}
267286
autoComplete='off'
268-
value={inputs.BytesNumber2Quota}
287+
value={inputs.RatioGPT3dot5}
269288
type='number'
270289
step='0.01'
271290
min='0'
272-
placeholder='例如:0.8'
291+
placeholder='例如:2'
273292
/>
274293
<Form.Input
275-
label='充值链接'
276-
name='TopUpLink'
294+
label='GPT-4 系列模型倍率'
295+
name='RatioGPT4'
277296
onChange={handleInputChange}
278297
autoComplete='off'
279-
value={inputs.TopUpLink}
280-
type='link'
281-
placeholder='例如发卡网站的购买链接'
298+
value={inputs.RatioGPT4}
299+
type='number'
300+
step='0.01'
301+
min='0'
302+
placeholder='例如:30'
303+
/>
304+
<Form.Input
305+
label='GPT-4 32k 系列模型倍率'
306+
name='RatioGPT4_32k'
307+
onChange={handleInputChange}
308+
autoComplete='off'
309+
value={inputs.RatioGPT4_32k}
310+
type='number'
311+
step='0.01'
312+
min='0'
313+
placeholder='例如:60'
282314
/>
283315
</Form.Group>
284316
<Form.Button onClick={submitOperationConfig}>保存运营设置</Form.Button>

0 commit comments

Comments
 (0)