Skip to content

Commit

Permalink
feat: support new ratio for all ui
Browse files Browse the repository at this point in the history
  • Loading branch information
WqyJh committed Jan 20, 2025
1 parent 56df7c0 commit f2bff58
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 106 deletions.
8 changes: 4 additions & 4 deletions relay/adaptor/openai/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package openai
import (
"errors"
"fmt"
"math"
"strings"

"github.com/pkoukk/tiktoken-go"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/image"
"github.com/songquanpeng/one-api/common/logger"
billingratio "github.com/songquanpeng/one-api/relay/billing/ratio"
"github.com/songquanpeng/one-api/relay/model"
"math"
"strings"
)

// tokenEncoderMap won't grow after initialization
Expand All @@ -32,7 +32,7 @@ func InitTokenEncoders() {
if err != nil {
logger.FatalLog(fmt.Sprintf("failed to get gpt-4 token encoder: %s", err.Error()))
}
for model := range billingratio.ModelRatio {
for model := range RatioMap {
if strings.HasPrefix(model, "gpt-3.5") {
tokenEncoderMap[model] = gpt35TokenEncoder
} else if strings.HasPrefix(model, "gpt-4o") {
Expand Down
46 changes: 17 additions & 29 deletions web/air/src/components/OperationSetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import React, { useEffect, useState } from 'react';
import { Divider, Form, Grid, Header } from 'semantic-ui-react';
import { API, showError, showSuccess, timestamp2string, verifyJSON } from '../helpers';

const RATIO_MAPPING_EXAMPLE = {
'gpt-4o-mini': {'input': 0.075, 'output': 0.3},
'llama3-8b-8192(33)': {'input': 0.15, 'output': 0.3},
'llama3-70b-8192(33)': {'input': 1.325, 'output': 1.749},
};


const OperationSetting = () => {
let now = new Date();
let [inputs, setInputs] = useState({
Expand All @@ -10,8 +17,7 @@ const OperationSetting = () => {
QuotaForInvitee: 0,
QuotaRemindThreshold: 0,
PreConsumedQuota: 0,
ModelRatio: '',
CompletionRatio: '',
Ratio: '',
GroupRatio: '',
TopUpLink: '',
ChatLink: '',
Expand All @@ -35,7 +41,7 @@ const OperationSetting = () => {
if (success) {
let newInputs = {};
data.forEach((item) => {
if (item.key === 'ModelRatio' || item.key === 'GroupRatio' || item.key === 'CompletionRatio') {
if (item.key === 'GroupRatio' || item.key === 'Ratio') {
item.value = JSON.stringify(JSON.parse(item.value), null, 2);
}
if (item.value === '{}') {
Expand Down Expand Up @@ -91,12 +97,12 @@ const OperationSetting = () => {
}
break;
case 'ratio':
if (originInputs['ModelRatio'] !== inputs.ModelRatio) {
if (!verifyJSON(inputs.ModelRatio)) {
showError('模型倍率不是合法的 JSON 字符串');
if (originInputs['Ratio'] !== inputs.Ratio) {
if (!verifyJSON(inputs.Ratio)) {
showError('自定义倍率不是合法的 JSON 字符串');
return;
}
await updateOption('ModelRatio', inputs.ModelRatio);
await updateOption('Ratio', inputs.Ratio);
}
if (originInputs['GroupRatio'] !== inputs.GroupRatio) {
if (!verifyJSON(inputs.GroupRatio)) {
Expand All @@ -105,13 +111,6 @@ const OperationSetting = () => {
}
await updateOption('GroupRatio', inputs.GroupRatio);
}
if (originInputs['CompletionRatio'] !== inputs.CompletionRatio) {
if (!verifyJSON(inputs.CompletionRatio)) {
showError('补全倍率不是合法的 JSON 字符串');
return;
}
await updateOption('CompletionRatio', inputs.CompletionRatio);
}
break;
case 'quota':
if (originInputs['QuotaForNewUser'] !== inputs.QuotaForNewUser) {
Expand Down Expand Up @@ -346,24 +345,13 @@ const OperationSetting = () => {
</Header>
<Form.Group widths='equal'>
<Form.TextArea
label='模型倍率'
name='ModelRatio'
onChange={handleInputChange}
style={{ minHeight: 250, fontFamily: 'JetBrains Mono, Consolas' }}
autoComplete='new-password'
value={inputs.ModelRatio}
placeholder='为一个 JSON 文本,键为模型名称,值为倍率'
/>
</Form.Group>
<Form.Group widths='equal'>
<Form.TextArea
label='补全倍率'
name='CompletionRatio'
label='自定义倍率'
name='Ratio'
onChange={handleInputChange}
style={{ minHeight: 250, fontFamily: 'JetBrains Mono, Consolas' }}
autoComplete='new-password'
value={inputs.CompletionRatio}
placeholder='为一个 JSON 文本,键为模型名称,值为倍率,此处的倍率设置是模型补全倍率相较于提示倍率的比例,使用该设置可强制覆盖 One API 的内部比例'
value={inputs.Ratio}
placeholder={`为一个 JSON 文本,键为模型名称,值为倍率结构,例如:\n${JSON.stringify(RATIO_MAPPING_EXAMPLE, null, 2)}`}
/>
</Form.Group>
<Form.Group widths='equal'>
Expand Down
52 changes: 18 additions & 34 deletions web/berry/src/views/Setting/component/OperationSetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import { DateTimePicker } from "@mui/x-date-pickers/DateTimePicker";
import dayjs from "dayjs";
require("dayjs/locale/zh-cn");

const RATIO_MAPPING_EXAMPLE = {
'gpt-4o-mini': {'input': 0.075, 'output': 0.3},
'llama3-8b-8192(33)': {'input': 0.15, 'output': 0.3},
'llama3-70b-8192(33)': {'input': 1.325, 'output': 1.749},
};

const OperationSetting = () => {
let now = new Date();
let [inputs, setInputs] = useState({
Expand All @@ -26,8 +32,7 @@ const OperationSetting = () => {
QuotaForInvitee: 0,
QuotaRemindThreshold: 0,
PreConsumedQuota: 0,
ModelRatio: "",
CompletionRatio: "",
Ratio: "",
GroupRatio: "",
TopUpLink: "",
ChatLink: "",
Expand All @@ -53,7 +58,7 @@ const OperationSetting = () => {
if (success) {
let newInputs = {};
data.forEach((item) => {
if (item.key === "ModelRatio" || item.key === "GroupRatio" || item.key === "CompletionRatio") {
if (item.key === "GroupRatio" || item.key === "Ratio") {
item.value = JSON.stringify(JSON.parse(item.value), null, 2);
}
if (item.value === '{}') {
Expand Down Expand Up @@ -123,12 +128,12 @@ const OperationSetting = () => {
}
break;
case "ratio":
if (originInputs["ModelRatio"] !== inputs.ModelRatio) {
if (!verifyJSON(inputs.ModelRatio)) {
showError("模型倍率不是合法的 JSON 字符串");
if (originInputs["Ratio"] !== inputs.Ratio) {
if (!verifyJSON(inputs.Ratio)) {
showError("自定义倍率不是合法的 JSON 字符串");
return;
}
await updateOption("ModelRatio", inputs.ModelRatio);
await updateOption("Ratio", inputs.Ratio);
}
if (originInputs["GroupRatio"] !== inputs.GroupRatio) {
if (!verifyJSON(inputs.GroupRatio)) {
Expand All @@ -137,13 +142,6 @@ const OperationSetting = () => {
}
await updateOption("GroupRatio", inputs.GroupRatio);
}
if (originInputs['CompletionRatio'] !== inputs.CompletionRatio) {
if (!verifyJSON(inputs.CompletionRatio)) {
showError('补全倍率不是合法的 JSON 字符串');
return;
}
await updateOption('CompletionRatio', inputs.CompletionRatio);
}
break;
case "quota":
if (originInputs["QuotaForNewUser"] !== inputs.QuotaForNewUser) {
Expand Down Expand Up @@ -501,28 +499,14 @@ const OperationSetting = () => {
<TextField
multiline
maxRows={15}
id="channel-ModelRatio-label"
label="模型倍率"
value={inputs.ModelRatio}
name="ModelRatio"
onChange={handleInputChange}
aria-describedby="helper-text-channel-ModelRatio-label"
minRows={5}
placeholder="为一个 JSON 文本,键为模型名称,值为倍率"
/>
</FormControl>
<FormControl fullWidth>
<TextField
multiline
maxRows={15}
id="channel-CompletionRatio-label"
label="补全倍率"
value={inputs.CompletionRatio}
name="CompletionRatio"
id="channel-Ratio-label"
label="自定义倍率"
value={inputs.Ratio}
name="Ratio"
onChange={handleInputChange}
aria-describedby="helper-text-channel-CompletionRatio-label"
aria-describedby="helper-text-channel-Ratio-label"
minRows={5}
placeholder="为一个 JSON 文本,键为模型名称,值为倍率,此处的倍率设置是模型补全倍率相较于提示倍率的比例,使用该设置可强制覆盖 One API 的内部比例"
placeholder={`为一个 JSON 文本,键为模型名称,值为倍率结构,例如:\n${JSON.stringify(RATIO_MAPPING_EXAMPLE, null, 2)}`}
/>
</FormControl>
<FormControl fullWidth>
Expand Down
40 changes: 1 addition & 39 deletions web/default/src/components/OperationSetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const OperationSetting = () => {
QuotaForInvitee: 0,
QuotaRemindThreshold: 0,
PreConsumedQuota: 0,
ModelRatio: '', // Deprecated
CompletionRatio: '', // Deprecated
GroupRatio: '',
Ratio: '',
TopUpLink: '',
Expand All @@ -42,7 +40,7 @@ const OperationSetting = () => {
if (success) {
let newInputs = {};
data.forEach((item) => {
if (item.key === 'ModelRatio' || item.key === 'GroupRatio' || item.key === 'CompletionRatio' || item.key === 'Ratio') {
if (item.key === 'GroupRatio' || item.key === 'Ratio') {
item.value = JSON.stringify(JSON.parse(item.value), null, 2);
}
if (item.value === '{}') {
Expand Down Expand Up @@ -98,27 +96,13 @@ const OperationSetting = () => {
}
break;
case 'ratio':
if (originInputs['ModelRatio'] !== inputs.ModelRatio) {
if (!verifyJSON(inputs.ModelRatio)) {
showError('模型倍率不是合法的 JSON 字符串');
return;
}
await updateOption('ModelRatio', inputs.ModelRatio);
}
if (originInputs['GroupRatio'] !== inputs.GroupRatio) {
if (!verifyJSON(inputs.GroupRatio)) {
showError('分组倍率不是合法的 JSON 字符串');
return;
}
await updateOption('GroupRatio', inputs.GroupRatio);
}
if (originInputs['CompletionRatio'] !== inputs.CompletionRatio) {
if (!verifyJSON(inputs.CompletionRatio)) {
showError('补全倍率不是合法的 JSON 字符串');
return;
}
await updateOption('CompletionRatio', inputs.CompletionRatio);
}
if (originInputs['Ratio'] !== inputs.Ratio) {
if (!verifyJSON(inputs.Ratio)) {
showError('倍率不是合法的 JSON 字符串');
Expand Down Expand Up @@ -369,28 +353,6 @@ const OperationSetting = () => {
placeholder={`为一个 JSON 文本,键为模型名称,值为倍率结构,例如:\n${JSON.stringify(RATIO_MAPPING_EXAMPLE, null, 2)}`}
/>
</Form.Group>
<Form.Group widths='equal'>
<Form.TextArea
label='模型倍率(已废弃)'
name='ModelRatio'
onChange={handleInputChange}
style={{ minHeight: 250, fontFamily: 'JetBrains Mono, Consolas' }}
autoComplete='new-password'
value={inputs.ModelRatio}
placeholder='为一个 JSON 文本,键为模型名称,值为倍率'
/>
</Form.Group>
<Form.Group widths='equal'>
<Form.TextArea
label='补全倍率(已废弃)'
name='CompletionRatio'
onChange={handleInputChange}
style={{ minHeight: 250, fontFamily: 'JetBrains Mono, Consolas' }}
autoComplete='new-password'
value={inputs.CompletionRatio}
placeholder='为一个 JSON 文本,键为模型名称,值为倍率,此处的倍率设置是模型补全倍率相较于提示倍率的比例,使用该设置可强制覆盖 One API 的内部比例'
/>
</Form.Group>
<Form.Group widths='equal'>
<Form.TextArea
label='分组倍率'
Expand Down

0 comments on commit f2bff58

Please sign in to comment.