feat: 修改计费模式为tokens

This commit is contained in:
archer
2023-03-25 14:43:32 +08:00
parent 4eaf3a1be0
commit 6bba859060
18 changed files with 97 additions and 38 deletions

View File

@@ -143,15 +143,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
!stream.destroyed && stream.push(null);
stream.destroy();
const promptsLen = formatPrompts.reduce((sum, item) => sum + item.content.length, 0);
console.log(`responseLen: ${responseContent.length}`, `promptLen: ${promptsLen}`);
const promptsContent = formatPrompts.map((item) => item.content).join('');
console.log(`responseLen: ${responseContent.length}`, `promptLen: ${promptsContent.length}`);
// 只有使用平台的 key 才计费
!userApiKey &&
pushBill({
modelName: model.service.modelName,
userId,
chatId,
textLen: promptsLen + responseContent.length
text: promptsContent + responseContent
});
} catch (err: any) {
if (step === 1) {

View File

@@ -54,21 +54,20 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
);
const responseMessage = response.data.choices[0]?.text || '';
const responseContent = response.data.choices[0]?.text || '';
const promptsLen = prompt.reduce((sum, item) => sum + item.value.length, 0);
console.log(`responseLen: ${responseMessage.length}`, `promptLen: ${promptsLen}`);
console.log(`responseLen: ${responseContent.length}`, `promptLen: ${formatPrompts.length}`);
// 只有使用平台的 key 才计费
!userApiKey &&
pushBill({
modelName: model.service.modelName,
userId,
chatId,
textLen: promptsLen + responseMessage.length
text: formatPrompts + responseContent
});
jsonRes(res, {
data: responseMessage
data: responseContent
});
} catch (err: any) {
jsonRes(res, {

View File

@@ -21,6 +21,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
// 根据 userId 获取模型信息
const models = await Model.find({
userId
}).sort({
_id: -1
});
jsonRes(res, {

View File

@@ -12,12 +12,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
await connectToDatabase();
await Bill.updateMany(
{},
{
type: 'chat',
modelName: 'gpt-3.5-turbo'
}
const bills = await Bill.find({
tokenLen: { $exists: false }
});
await Promise.all(
bills.map((bill) =>
Bill.findByIdAndUpdate(bill._id, {
tokenLen: bill.textLen
})
)
);
jsonRes(res, {

View File

@@ -5,6 +5,7 @@ import axios from 'axios';
import { authToken } from '@/service/utils/tools';
import { customAlphabet } from 'nanoid';
import { connectToDatabase, Pay } from '@/service/mongo';
import { PRICE_SCALE } from '@/utils/user';
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 20);
@@ -35,7 +36,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// 充值记录 + 1
const payOrder = await Pay.create({
userId,
price: amount * 100000,
price: amount * PRICE_SCALE,
orderId: id
});

View File

@@ -32,7 +32,8 @@ const DataList = () => {
isLoadAll,
requesting,
data: dataList,
getData
getData,
initRequesting
} = usePaging<DataListItem>({
api: getDataList,
pageSize: 20
@@ -76,7 +77,13 @@ const DataList = () => {
</Card>
{/* 数据表 */}
<Card mt={3} flex={'1 0 0'} h={['auto', '0']} px={6} py={4}>
<ScrollData h={'100%'} nextPage={nextPage} isLoadAll={isLoadAll} requesting={requesting}>
<ScrollData
h={'100%'}
nextPage={nextPage}
isLoadAll={isLoadAll}
requesting={requesting}
initRequesting={initRequesting}
>
<TableContainer>
<Table>
<Thead>
@@ -96,7 +103,7 @@ const DataList = () => {
defaultValue={item.name}
size={'sm'}
onBlur={(e) => {
if (!e.target.value) return;
if (!e.target.value || e.target.value === item.name) return;
updateDataName(item._id, e.target.value);
}}
/>

View File

@@ -112,7 +112,7 @@ const CreateModel = ({
{formatPrice(
modelList.find((item) => item.model === getValues('serviceModelName'))?.price || 0
) * 1000}
/1000()
/1K tokens()
</Box>
</ModalBody>

View File

@@ -195,7 +195,7 @@ const ModelDetail = ({ modelId }: { modelId: string }) => {
return () => {
window.onbeforeunload = null;
};
}, []);
}, [router]);
return (
<>
@@ -246,7 +246,13 @@ const ModelDetail = ({ modelId }: { modelId: string }) => {
</Card>
<Grid mt={5} gridTemplateColumns={media('1fr 1fr', '1fr')} gridGap={5}>
<ModelEditForm formHooks={formHooks} />
<Card p={4}>{!!model && <Training model={model} />}</Card>
{canTrain && (
<Card p={4}>
<Training model={model} />
</Card>
)}
<Card p={4}>
<Box fontWeight={'bold'} fontSize={'lg'}>

View File

@@ -100,7 +100,7 @@ const PayModal = ({ onClose }: { onClose: () => void }) => {
<Thead>
<Tr>
<Th></Th>
<Th>(/1000)</Th>
<Th>(/1K tokens)</Th>
</Tr>
</Thead>
<Tbody>