perf: abort

This commit is contained in:
archer
2023-07-18 15:20:58 +08:00
parent 8a25aeabc4
commit 2330186a09
8 changed files with 50 additions and 28 deletions

View File

@@ -1,6 +1,6 @@
{ {
"vectorMaxProcess": 10, "vectorMaxProcess": 15,
"qaMaxProcess": 10, "qaMaxProcess": 15,
"pgIvfflatProbe": 10, "pgIvfflatProbe": 20,
"sensitiveCheck": false "sensitiveCheck": false
} }

View File

@@ -5,7 +5,8 @@ import React, {
useMemo, useMemo,
forwardRef, forwardRef,
useImperativeHandle, useImperativeHandle,
ForwardedRef ForwardedRef,
useEffect
} from 'react'; } from 'react';
import { throttle } from 'lodash'; import { throttle } from 'lodash';
import { ChatItemType, ChatSiteItemType, ExportChatType } from '@/types/chat'; import { ChatItemType, ChatSiteItemType, ExportChatType } from '@/types/chat';
@@ -31,6 +32,7 @@ import { MessageItemType } from '@/pages/api/openapi/v1/chat/completions';
import MyTooltip from '../MyTooltip'; import MyTooltip from '../MyTooltip';
import { fileDownload } from '@/utils/file'; import { fileDownload } from '@/utils/file';
import { htmlTemplate } from '@/constants/common'; import { htmlTemplate } from '@/constants/common';
import { useRouter } from 'next/router';
import dynamic from 'next/dynamic'; import dynamic from 'next/dynamic';
const QuoteModal = dynamic(() => import('./QuoteModal')); const QuoteModal = dynamic(() => import('./QuoteModal'));
@@ -133,6 +135,7 @@ const ChatBox = (
) => { ) => {
const ChatBoxRef = useRef<HTMLDivElement>(null); const ChatBoxRef = useRef<HTMLDivElement>(null);
const theme = useTheme(); const theme = useTheme();
const router = useRouter();
const { copyData } = useCopyData(); const { copyData } = useCopyData();
const { toast } = useToast(); const { toast } = useToast();
const { userInfo } = useUserStore(); const { userInfo } = useUserStore();
@@ -392,6 +395,12 @@ const ChatBox = (
[chatHistory.length, showEmptyIntro, variableModules, welcomeText] [chatHistory.length, showEmptyIntro, variableModules, welcomeText]
); );
useEffect(() => {
return () => {
controller.current?.abort();
};
}, [router.query]);
return ( return (
<Flex flexDirection={'column'} h={'100%'}> <Flex flexDirection={'column'} h={'100%'}>
<Box ref={ChatBoxRef} flex={'1 0 0'} h={0} overflow={'overlay'} px={[2, 5, 7]} pt={[0, 5]}> <Box ref={ChatBoxRef} flex={'1 0 0'} h={0} overflow={'overlay'} px={[2, 5, 7]} pt={[0, 5]}>

View File

@@ -1,4 +1,4 @@
import React, { useRef } from 'react'; import React, { useMemo, useRef } from 'react';
import ReactMarkdown from 'react-markdown'; import ReactMarkdown from 'react-markdown';
import RemarkGfm from 'remark-gfm'; import RemarkGfm from 'remark-gfm';
import RemarkMath from 'remark-math'; import RemarkMath from 'remark-math';
@@ -48,13 +48,16 @@ const Markdown = ({
isChatting?: boolean; isChatting?: boolean;
onClick?: (e: any) => void; onClick?: (e: any) => void;
}) => { }) => {
const components = useRef({ const components = useMemo(
a: Link, () => ({
img: Image, a: Link,
pre: 'div', img: Image,
p: 'div', pre: 'div',
code: (props: any) => <Code {...props} onClick={onClick} /> p: 'div',
}); code: (props: any) => <Code {...props} onClick={onClick} />
}),
[onClick]
);
return ( return (
<ReactMarkdown <ReactMarkdown
@@ -64,7 +67,7 @@ const Markdown = ({
remarkPlugins={[RemarkGfm, RemarkMath, RemarkBreaks]} remarkPlugins={[RemarkGfm, RemarkMath, RemarkBreaks]}
rehypePlugins={[RehypeKatex]} rehypePlugins={[RehypeKatex]}
// @ts-ignore // @ts-ignore
components={components.current} components={components}
> >
{source} {source}
</ReactMarkdown> </ReactMarkdown>

View File

@@ -89,6 +89,14 @@ export async function chatCompletion({
// FastGpt temperature range: 1~10 // FastGpt temperature range: 1~10
temperature = +(modelConstantsData.maxTemperature * (temperature / 10)).toFixed(2); temperature = +(modelConstantsData.maxTemperature * (temperature / 10)).toFixed(2);
const limitText = (() => {
if (limitPrompt) return limitPrompt;
if (quotePrompt && !limitPrompt) {
return '根据知识库内容回答问题,仅回复知识库提供的内容。';
}
return '';
})();
const messages: ChatItemType[] = [ const messages: ChatItemType[] = [
...(quotePrompt ...(quotePrompt
? [ ? [
@@ -107,11 +115,11 @@ export async function chatCompletion({
] ]
: []), : []),
...history, ...history,
...(limitPrompt ...(limitText
? [ ? [
{ {
obj: ChatRoleEnum.System, obj: ChatRoleEnum.System,
value: limitPrompt value: limitText
} }
] ]
: []), : []),

View File

@@ -145,7 +145,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
}); });
} }
console.log(`finish time: ${(Date.now() - startTime) / 100}s`); console.log(`finish time: ${(Date.now() - startTime) / 1000}s`);
if (stream) { if (stream) {
sseResponse({ sseResponse({

View File

@@ -23,6 +23,7 @@ import { postCreateApp } from '@/api/app';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import { appTemplates } from '@/constants/flow/ModuleTemplate'; import { appTemplates } from '@/constants/flow/ModuleTemplate';
import Avatar from '@/components/Avatar'; import Avatar from '@/components/Avatar';
import MyTooltip from '@/components/MyTooltip';
type FormType = { type FormType = {
avatar: string; avatar: string;
@@ -102,21 +103,22 @@ const CreateModal = ({ onClose, onSuccess }: { onClose: () => void; onSuccess: (
<Modal isOpen onClose={onClose}> <Modal isOpen onClose={onClose}>
<ModalOverlay /> <ModalOverlay />
<ModalContent w={'700px'} maxW={'90vw'}> <ModalContent w={'700px'} maxW={'90vw'}>
<ModalHeader fontSize={'2xl'}> AI </ModalHeader> <ModalHeader fontSize={'2xl'}> AI </ModalHeader>
<ModalBody> <ModalBody>
<Box color={'myGray.800'} fontWeight={'bold'}> <Box color={'myGray.800'} fontWeight={'bold'}>
</Box> </Box>
<Flex mt={3} alignItems={'center'}> <Flex mt={3} alignItems={'center'}>
<Avatar <MyTooltip label={'点击设置头像'}>
src={getValues('avatar')} <Avatar
w={['32px', '36px']} src={getValues('avatar')}
h={['32px', '36px']} w={['32px', '36px']}
cursor={'pointer'} h={['32px', '36px']}
title={'点击选择头像'} cursor={'pointer'}
borderRadius={'md'} borderRadius={'md'}
onClick={onOpenSelectFile} onClick={onOpenSelectFile}
/> />
</MyTooltip>
<Input <Input
ml={4} ml={4}
bg={'myWhite.600'} bg={'myWhite.600'}

View File

@@ -66,7 +66,7 @@ const Chat = () => {
const newTitle = prompts[0].content?.slice(0, 20) || '新对话'; const newTitle = prompts[0].content?.slice(0, 20) || '新对话';
// update history // update history
if (newChatId) { if (newChatId && !controller.signal.aborted) {
forbidRefresh.current = true; forbidRefresh.current = true;
const newHistory: ChatHistoryItemType = { const newHistory: ChatHistoryItemType = {
_id: newChatId, _id: newChatId,

View File

@@ -69,7 +69,7 @@ const ShareChat = ({ shareId, chatId }: { shareId: string; chatId: string }) =>
shareId shareId
}); });
if (newChatId) { if (newChatId && !controller.signal.aborted) {
router.replace({ router.replace({
query: { query: {
shareId, shareId,