feat: 拆分文本账单结算

This commit is contained in:
archer
2023-03-25 23:02:55 +08:00
parent 02cee35a45
commit 75cf3d1e9f
17 changed files with 191 additions and 89 deletions

View File

@@ -9,7 +9,7 @@ import { jsonRes } from '@/service/response';
import type { ModelSchema } from '@/types/mongoSchema';
import { PassThrough } from 'stream';
import { modelList } from '@/constants/model';
import { pushBill } from '@/service/events/pushChatBill';
import { pushChatBill } from '@/service/events/pushBill';
/* 发送提示词 */
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
@@ -91,7 +91,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
messages: formatPrompts,
frequency_penalty: 0.5, // 越大,重复内容越少
presence_penalty: -0.5, // 越大,越容易出现新内容
stream: true
stream: true,
stop: ['。!?.!.']
},
{
timeout: 40000,
@@ -149,7 +150,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const promptsContent = formatPrompts.map((item) => item.content).join('');
// 只有使用平台的 key 才计费
!userApiKey &&
pushBill({
pushChatBill({
modelName: model.service.modelName,
userId,
chatId,

View File

@@ -8,7 +8,7 @@ import { jsonRes } from '@/service/response';
import type { ModelSchema } from '@/types/mongoSchema';
import { PassThrough } from 'stream';
import { modelList } from '@/constants/model';
import { pushBill } from '@/service/events/pushChatBill';
import { pushChatBill } from '@/service/events/pushBill';
/* 发送提示词 */
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
@@ -142,7 +142,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// 只有使用平台的 key 才计费
!userApiKey &&
pushBill({
pushChatBill({
modelName: model.service.modelName,
userId,
chatId,

View File

@@ -1,7 +1,7 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase, Data } from '@/service/mongo';
import { connectToDatabase, Data, DataItem } from '@/service/mongo';
import { authToken } from '@/service/utils/tools';
import type { DataListItem } from '@/types/data';
import type { PagingData } from '@/types';
@@ -27,6 +27,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
isDeleted: true
});
// 改变 dataItem 状态为 0
await DataItem.updateMany(
{
dataId
},
{
status: 0
}
);
jsonRes<PagingData<DataListItem>>(res);
} catch (err) {
jsonRes(res, {

View File

@@ -26,7 +26,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
dataId,
status: 0
})
.sort({ time: -1 }) // 按照创建时间倒序排列
.sort({ _id: -1 }) // 按照创建时间倒序排列
.skip((pageNum - 1) * pageSize)
.limit(pageSize);

View File

@@ -58,7 +58,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
$filter: {
input: '$items',
as: 'item',
cond: { $eq: ['$$item.status', 1] } // 统计status为1的数量
cond: { $ne: ['$$item.status', 0] } // 统计 status 不为0的数量
}
}
}

View File

@@ -25,7 +25,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const bills = await Bill.find<BillSchema>({
userId
})
.sort({ time: -1 }) // 按照创建时间倒序排列
.sort({ _id: -1 }) // 按照创建时间倒序排列
.skip((pageNum - 1) * pageSize)
.limit(pageSize);

View File

@@ -8,7 +8,6 @@ import {
ModalBody,
ModalCloseButton,
Button,
Input,
Box,
Flex,
Textarea
@@ -21,10 +20,20 @@ import { postSplitData } from '@/api/data';
import { useMutation } from '@tanstack/react-query';
import { useToast } from '@/hooks/useToast';
import { useLoading } from '@/hooks/useLoading';
import { formatPrice } from '@/utils/user';
import { modelList, ChatModelNameEnum } from '@/constants/model';
const fileExtension = '.txt,.doc,.docx,.pdf,.md';
const ImportDataModal = ({ dataId, onClose }: { dataId: string; onClose: () => void }) => {
const ImportDataModal = ({
dataId,
onClose,
onSuccess
}: {
dataId: string;
onClose: () => void;
onSuccess: () => void;
}) => {
const { openConfirm, ConfirmChild } = useConfirm({
content: '确认提交生成任务?该任务无法终止!'
});
@@ -60,6 +69,7 @@ const ImportDataModal = ({ dataId, onClose }: { dataId: string; onClose: () => v
status: 'success'
});
onClose();
onSuccess();
},
onError(err: any) {
toast({
@@ -110,7 +120,16 @@ const ImportDataModal = ({ dataId, onClose }: { dataId: string; onClose: () => v
<Modal isOpen={true} onClose={onClose}>
<ModalOverlay />
<ModalContent position={'relative'} maxW={['90vw', '800px']}>
<ModalHeader>QA</ModalHeader>
<ModalHeader>
QA
<Box ml={2} as={'span'} fontSize={'sm'} color={'blackAlpha.600'}>
{formatPrice(
modelList.find((item) => item.model === ChatModelNameEnum.GPT35)?.price || 0,
1000
)}
/1K tokens
</Box>
</ModalHeader>
<ModalCloseButton />
<ModalBody display={'flex'}>
@@ -132,13 +151,16 @@ const ImportDataModal = ({ dataId, onClose }: { dataId: string; onClose: () => v
<Box flex={'1 0 0'} w={0} ml={3} minH={'200px'}>
{activeTab === 'text' && (
<Textarea
h={'100%'}
maxLength={-1}
value={textInput}
placeholder={'请粘贴或输入需要处理的文本'}
onChange={(e) => setTextInput(e.target.value)}
/>
<>
<Textarea
h={'100%'}
maxLength={-1}
value={textInput}
placeholder={'请粘贴或输入需要处理的文本'}
onChange={(e) => setTextInput(e.target.value)}
/>
<Box mt={2}> {textInput.length} </Box>
</>
)}
{activeTab === 'doc' && (
<Flex

View File

@@ -209,7 +209,11 @@ const DataList = () => {
</Card>
{ImportDataId && (
<ImportDataModal dataId={ImportDataId} onClose={() => setImportDataId(undefined)} />
<ImportDataModal
dataId={ImportDataId}
onClose={() => setImportDataId(undefined)}
onSuccess={() => getData(1, true)}
/>
)}
{isOpenCreateDataModal && (
<CreateDataModal onClose={onCloseCreateDataModal} onSuccess={() => getData(1, true)} />

View File

@@ -33,6 +33,7 @@ import dayjs from 'dayjs';
import { formatPrice } from '@/utils/user';
import WxConcat from '@/components/WxConcat';
import ScrollData from '@/components/ScrollData';
import { BillTypeMap } from '@/constants/user';
const PayModal = dynamic(() => import('./components/PayModal'));
@@ -266,6 +267,7 @@ const NumberSetting = () => {
<Thead>
<Tr>
<Th></Th>
<Th></Th>
<Th></Th>
<Th>Tokens </Th>
<Th></Th>
@@ -275,6 +277,7 @@ const NumberSetting = () => {
{bills.map((item) => (
<Tr key={item.id}>
<Td>{item.time}</Td>
<Td>{BillTypeMap[item.type]}</Td>
<Td>{item.textLen}</Td>
<Td>{item.tokenLen}</Td>
<Td>{item.price}</Td>