perf: bill framwork

This commit is contained in:
archer
2023-05-21 13:07:40 +08:00
parent e45c1eb1e0
commit 98444fd04b
19 changed files with 68 additions and 78 deletions

View File

@@ -9,6 +9,7 @@ import { pushChatBill } from '@/service/events/pushBill';
import { resStreamResponse } from '@/service/utils/chat';
import { searchKb } from '@/service/plugins/searchKb';
import { ChatRoleEnum } from '@/constants/chat';
import { BillTypeEnum } from '@/constants/user';
/* 发送提示词 */
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
@@ -108,7 +109,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
userId,
chatId,
textLen: finishMessages.map((item) => item.value).join('').length,
tokens: totalTokens
tokens: totalTokens,
type: BillTypeEnum.chat
});
} catch (err: any) {
if (step === 1) {

View File

@@ -9,6 +9,7 @@ import { pushChatBill, updateShareChatBill } from '@/service/events/pushBill';
import { resStreamResponse } from '@/service/utils/chat';
import { searchKb } from '@/service/plugins/searchKb';
import { ChatRoleEnum } from '@/constants/chat';
import { BillTypeEnum } from '@/constants/user';
/* 发送提示词 */
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
@@ -98,7 +99,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
chatModel: model.chat.chatModel,
userId,
textLen: finishMessages.map((item) => item.value).join('').length,
tokens: totalTokens
tokens: totalTokens,
type: BillTypeEnum.chat
});
updateShareChatBill({
shareId,

View File

@@ -9,6 +9,7 @@ import { pushChatBill } from '@/service/events/pushBill';
import { searchKb } from '@/service/plugins/searchKb';
import { ChatRoleEnum } from '@/constants/chat';
import { withNextCors } from '@/service/utils/tools';
import { BillTypeEnum } from '@/constants/user';
/* 发送提示词 */
export default withNextCors(async function handler(req: NextApiRequest, res: NextApiResponse) {
@@ -45,7 +46,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
let startTime = Date.now();
/* 凭证校验 */
const { userId } = await authUser({ req, authOpenApi: true });
const { userId } = await authUser({ req });
const { model } = await authModel({
userId,
@@ -74,7 +75,9 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
// search result is empty
if (code === 201) {
return res.send(searchPrompts[0]?.value);
return isStream
? res.send(searchPrompts[0]?.value)
: jsonRes(res, { data: searchPrompts[0]?.value });
}
prompts.splice(prompts.length - 3, 0, ...searchPrompts);
} else {
@@ -129,7 +132,8 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
chatModel: model.chat.chatModel,
userId,
textLen,
tokens
tokens,
type: BillTypeEnum.openapiChat
});
} catch (err: any) {
if (step === 1) {

View File

@@ -15,7 +15,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
}
// 凭证校验
const { userId } = await authUser({ req, authToken: true });
const { userId } = await authUser({ req });
await PgClient.delete('modelData', {
where: [['user_id', userId], 'AND', ['id', dataId]]

View File

@@ -15,7 +15,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
}
// 凭证校验
const { userId } = await authUser({ req, authToken: true });
const { userId } = await authUser({ req });
// 更新 pg 内容.仅修改a不需要更新向量。
await PgClient.update('modelData', {

View File

@@ -22,7 +22,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
}
await connectToDatabase();
const { userId } = await authUser({ req, authToken: true });
const { userId } = await authUser({ req });
// 验证是否是该用户的 model
await authKb({

View File

@@ -3,7 +3,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase, Bill } from '@/service/mongo';
import { authUser } from '@/service/utils/auth';
import type { BillSchema } from '@/types/mongoSchema';
import { adaptBill } from '@/utils/adapt';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
@@ -17,7 +17,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await connectToDatabase();
// 根据 id 获取用户账单
const bills = await Bill.find<BillSchema>({
const bills = await Bill.find({
userId
})
.sort({ _id: -1 }) // 按照创建时间倒序排列
@@ -33,7 +33,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
data: {
pageNum,
pageSize,
data: bills,
data: bills.map(adaptBill),
total
}
});

View File

@@ -5,6 +5,7 @@ import { getUserBills } from '@/api/user';
import type { UserBillType } from '@/types/user';
import { usePagination } from '@/hooks/usePagination';
import { useLoading } from '@/hooks/useLoading';
import dayjs from 'dayjs';
const BillTable = () => {
const { Loading } = useLoading();
@@ -28,6 +29,7 @@ const BillTable = () => {
<Tr>
<Th></Th>
<Th></Th>
<Th></Th>
<Th></Th>
<Th>Tokens </Th>
<Th></Th>
@@ -36,8 +38,9 @@ const BillTable = () => {
<Tbody fontSize={'sm'}>
{bills.map((item) => (
<Tr key={item.id}>
<Td>{item.time}</Td>
<Td>{BillTypeMap[item.type]}</Td>
<Td>{dayjs(item.time).format('YYYY/MM/DD HH:mm:ss')}</Td>
<Td>{BillTypeMap[item.type] || '-'}</Td>
<Td>{item.modelName}</Td>
<Td>{item.textLen}</Td>
<Td>{item.tokenLen}</Td>
<Td>{item.price}</Td>