perf: search prompt

This commit is contained in:
archer
2023-05-09 16:32:02 +08:00
parent f52f514f5f
commit 4dc541e0a6
13 changed files with 82 additions and 59 deletions

View File

@@ -55,7 +55,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// 使用了知识库搜索
if (model.chat.useKb) {
const { code, searchPrompt } = await searchKb({
const { code, searchPrompts } = await searchKb({
userOpenAiKey,
prompts,
similarity: ModelVectorSearchModeMap[model.chat.searchMode]?.similarity,
@@ -65,10 +65,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// search result is empty
if (code === 201) {
return res.send(searchPrompt?.value);
return res.send(searchPrompts[0]?.value);
}
searchPrompt && prompts.unshift(searchPrompt);
prompts.splice(prompts.length - 1, 0, ...searchPrompts);
} else {
// 没有用知识库搜索,仅用系统提示词
model.chat.systemPrompt &&
@@ -103,8 +103,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
stream,
chatResponse: streamResponse,
prompts,
systemPrompt:
showModelDetail && prompts[0].obj === ChatRoleEnum.System ? prompts[0].value : ''
systemPrompt: showModelDetail
? prompts
.filter((item) => item.obj === ChatRoleEnum.System)
.map((item) => item.value)
.join('\n')
: ''
});
// 只有使用平台的 key 才计费

View File

@@ -73,7 +73,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (model.chat.useKb) {
const similarity = ModelVectorSearchModeMap[model.chat.searchMode]?.similarity || 0.22;
const { code, searchPrompt } = await searchKb({
const { code, searchPrompts } = await searchKb({
prompts,
similarity,
model,
@@ -82,10 +82,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// search result is empty
if (code === 201) {
return res.send(searchPrompt?.value);
return res.send(searchPrompts[0]?.value);
}
searchPrompt && prompts.unshift(searchPrompt);
prompts.splice(prompts.length - 1, 0, ...searchPrompts);
} else {
// 没有用知识库搜索,仅用系统提示词
if (model.chat.systemPrompt) {

View File

@@ -122,14 +122,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const prompts = [prompt];
// 获取向量匹配到的提示词
const { searchPrompt } = await searchKb({
const { searchPrompts } = await searchKb({
similarity: ModelVectorSearchModeMap[model.chat.searchMode]?.similarity,
prompts,
model,
userId
});
searchPrompt && prompts.unshift(searchPrompt);
prompts.splice(prompts.length - 1, 0, ...searchPrompts);
// 计算温度
const temperature = (modelConstantsData.maxTemperature * (model.chat.temperature / 10)).toFixed(

View File

@@ -508,7 +508,7 @@ const Chat = ({
isLeavePage.current = true;
controller.current?.abort();
};
}, []);
}, [modelId, chatId]);
return (
<Flex

View File

@@ -279,9 +279,9 @@ const ModelDataCard = ({ modelId, isOwner }: { modelId: string; isOwner: boolean
</Tbody>
</Table>
</TableContainer>
<Box mt={2} textAlign={'end'}>
<Flex mt={2} justifyContent={'flex-end'}>
<Pagination />
</Box>
</Flex>
</Box>
<Loading loading={isLoading} fixed={false} />

View File

@@ -61,13 +61,7 @@ const SelectFileModal = ({
const { openConfirm, ConfirmChild } = useConfirm({
content: `确认导入该文件,需要一定时间进行拆解,该任务无法终止!如果余额不足,未完成的任务会被直接清除。一共 ${
splitRes.chunks.length
} 组。${
splitRes.tokens
? `大约 ${splitRes.tokens} 个tokens, 约 ${formatPrice(
splitRes.tokens * modeMap[mode].price
)}`
: ''
}`
} 组。${splitRes.tokens ? `大约 ${splitRes.tokens} 个tokens。` : ''}`
});
const onSelectFile = useCallback(

View File

@@ -101,9 +101,9 @@ const modelList = () => {
<Grid templateColumns={['1fr', '1fr 1fr', '1fr 1fr 1fr']} gridGap={4} mt={4}>
<ShareModelList models={models} onclickCollection={onclickCollection} />
</Grid>
<Box mt={4}>
<Flex mt={4} justifyContent={'flex-end'}>
<Pagination />
</Box>
</Flex>
</Card>
<Loading loading={isLoading} />

View File

@@ -1,5 +1,5 @@
import React from 'react';
import { Card, Box, Table, Thead, Tbody, Tr, Th, Td, TableContainer } from '@chakra-ui/react';
import { Card, Box, Table, Thead, Tbody, Tr, Th, Td, TableContainer, Flex } from '@chakra-ui/react';
import { BillTypeMap } from '@/constants/user';
import { getUserBills } from '@/api/user';
import type { UserBillType } from '@/types/user';
@@ -48,9 +48,9 @@ const BillTable = () => {
<Loading loading={isLoading} fixed={false} />
</TableContainer>
<Box mt={4} mr={4} textAlign={'end'}>
<Flex mt={4} px={4} justifyContent={'flex-end'}>
<Pagination />
</Box>
</Flex>
</Card>
);
};

View File

@@ -139,9 +139,9 @@ const OpenApi = () => {
<Loading loading={isLoading} fixed={false} />
</TableContainer>
<Box mt={4} mr={4} textAlign={'end'}>
<Flex mt={4} px={4} justifyContent={'flex-end'}>
<Pagination />
</Box>
</Flex>
</Card>
<Modal isOpen={isOpenWithdraw} onClose={onCloseWithdraw}>
<ModalOverlay />