Skip to content

Commit

Permalink
feat: able to configure ratio for different models (close #26)
Browse files Browse the repository at this point in the history
  • Loading branch information
songquanpeng committed Apr 28, 2023
1 parent b08cd7e commit d93cb8f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
6 changes: 5 additions & 1 deletion common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ var TurnstileSiteKey = ""
var TurnstileSecretKey = ""

var QuotaForNewUser = 100
var BytesNumber2Quota = 0.8

// https://platform.openai.com/docs/models/model-endpoint-compatibility
var RatioGPT3dot5 float64 = 2
var RatioGPT4 float64 = 30
var RatioGPT4_32k float64 = 60

const (
RoleGuestUser = 0
Expand Down
9 changes: 9 additions & 0 deletions controller/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ func relayHelper(c *gin.Context) error {
} else {
quota = textResponse.Usage.TotalTokens
}
ratio := common.RatioGPT3dot5
if strings.HasPrefix(textRequest.Model, "gpt-4-32k") {
ratio = common.RatioGPT4_32k
} else if strings.HasPrefix(textRequest.Model, "gpt-4") {
ratio = common.RatioGPT4
} else {
ratio = common.RatioGPT3dot5
}
quota = int(float64(quota) * ratio)
err := model.ConsumeTokenQuota(tokenId, quota)
if err != nil {
common.SysError("Error consuming token remain quota: " + err.Error())
Expand Down
12 changes: 9 additions & 3 deletions model/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ 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["RatioGPT3dot5"] = strconv.FormatFloat(common.RatioGPT3dot5, 'f', -1, 64)
common.OptionMap["RatioGPT4"] = strconv.FormatFloat(common.RatioGPT4, 'f', -1, 64)
common.OptionMap["RatioGPT4_32k"] = strconv.FormatFloat(common.RatioGPT4_32k, 'f', -1, 64)
common.OptionMap["TopUpLink"] = common.TopUpLink
common.OptionMapRWMutex.Unlock()
options, _ := AllOption()
Expand Down Expand Up @@ -136,8 +138,12 @@ 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 "RatioGPT3dot5":
common.RatioGPT3dot5, _ = strconv.ParseFloat(value, 64)
case "RatioGPT4":
common.RatioGPT4, _ = strconv.ParseFloat(value, 64)
case "RatioGPT4_32k":
common.RatioGPT4_32k, _ = strconv.ParseFloat(value, 64)
case "TopUpLink":
common.TopUpLink = value
}
Expand Down
58 changes: 45 additions & 13 deletions web/src/components/SystemSetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const SystemSetting = () => {
TurnstileSecretKey: '',
RegisterEnabled: '',
QuotaForNewUser: 0,
BytesNumber2Quota: 0.8,
RatioGPT3dot5: 2,
RatioGPT4: 30,
RatioGPT4_32k: 60,
TopUpLink: ''
});
let originInputs = {};
Expand Down Expand Up @@ -91,7 +93,7 @@ const SystemSetting = () => {
name === 'TurnstileSiteKey' ||
name === 'TurnstileSecretKey' ||
name === 'QuotaForNewUser' ||
name === 'BytesNumber2Quota' ||
name.startsWith('Ratio') ||
name === 'TopUpLink'
) {
setInputs((inputs) => ({ ...inputs, [name]: value }));
Expand All @@ -109,8 +111,14 @@ const SystemSetting = () => {
if (originInputs['QuotaForNewUser'] !== inputs.QuotaForNewUser) {
await updateOption('QuotaForNewUser', inputs.QuotaForNewUser);
}
if (originInputs['BytesNumber2Quota'] !== inputs.BytesNumber2Quota) {
await updateOption('BytesNumber2Quota', inputs.BytesNumber2Quota);
if (originInputs['RatioGPT3dot5'] !== inputs.RatioGPT3dot5) {
await updateOption('RatioGPT3dot5', inputs.RatioGPT3dot5);
}
if (originInputs['RatioGPT4'] !== inputs.RatioGPT4) {
await updateOption('RatioGPT4', inputs.RatioGPT4);
}
if (originInputs['RatioGPT4_32k'] !== inputs.RatioGPT4_32k) {
await updateOption('RatioGPT4_32k', inputs.RatioGPT4_32k);
}
if (originInputs['TopUpLink'] !== inputs.TopUpLink) {
await updateOption('TopUpLink', inputs.TopUpLink);
Expand Down Expand Up @@ -261,24 +269,48 @@ const SystemSetting = () => {
placeholder='例如:100'
/>
<Form.Input
label='Stream 模式下估算 token 时所使用的倍率'
name='BytesNumber2Quota'
label='充值链接'
name='TopUpLink'
onChange={handleInputChange}
autoComplete='off'
value={inputs.TopUpLink}
type='link'
placeholder='例如发卡网站的购买链接'
/>
</Form.Group>
<Form.Group widths={3}>
<Form.Input
label='GPT-3.5 系列模型倍率'
name='RatioGPT3dot5'
onChange={handleInputChange}
autoComplete='off'
value={inputs.BytesNumber2Quota}
value={inputs.RatioGPT3dot5}
type='number'
step='0.01'
min='0'
placeholder='例如:0.8'
placeholder='例如:2'
/>
<Form.Input
label='充值链接'
name='TopUpLink'
label='GPT-4 系列模型倍率'
name='RatioGPT4'
onChange={handleInputChange}
autoComplete='off'
value={inputs.TopUpLink}
type='link'
placeholder='例如发卡网站的购买链接'
value={inputs.RatioGPT4}
type='number'
step='0.01'
min='0'
placeholder='例如:30'
/>
<Form.Input
label='GPT-4 32k 系列模型倍率'
name='RatioGPT4_32k'
onChange={handleInputChange}
autoComplete='off'
value={inputs.RatioGPT4_32k}
type='number'
step='0.01'
min='0'
placeholder='例如:60'
/>
</Form.Group>
<Form.Button onClick={submitOperationConfig}>保存运营设置</Form.Button>
Expand Down

0 comments on commit d93cb8f

Please sign in to comment.