mirror of
https://git.mirrors.martin98.com/https://github.com/infiniflow/ragflow.git
synced 2025-05-24 05:29:03 +08:00
### What problem does this PR solve? feat: Click on the relevant question tag to continue searching for answers #2247 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
parent
22acd0ac67
commit
fbe68034aa
@ -217,6 +217,10 @@ export const useSendMessageWithSse = (
|
|||||||
const [answer, setAnswer] = useState<IAnswer>({} as IAnswer);
|
const [answer, setAnswer] = useState<IAnswer>({} as IAnswer);
|
||||||
const [done, setDone] = useState(true);
|
const [done, setDone] = useState(true);
|
||||||
|
|
||||||
|
const resetAnswer = useCallback(() => {
|
||||||
|
setAnswer({} as IAnswer);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const send = useCallback(
|
const send = useCallback(
|
||||||
async (
|
async (
|
||||||
body: any,
|
body: any,
|
||||||
@ -251,7 +255,7 @@ export const useSendMessageWithSse = (
|
|||||||
const val = JSON.parse(value?.data || '');
|
const val = JSON.parse(value?.data || '');
|
||||||
const d = val?.data;
|
const d = val?.data;
|
||||||
if (typeof d !== 'boolean') {
|
if (typeof d !== 'boolean') {
|
||||||
// console.info('data:', d);
|
console.info('data:', d);
|
||||||
setAnswer({
|
setAnswer({
|
||||||
...d,
|
...d,
|
||||||
conversationId: body?.conversation_id,
|
conversationId: body?.conversation_id,
|
||||||
@ -264,18 +268,16 @@ export const useSendMessageWithSse = (
|
|||||||
}
|
}
|
||||||
console.info('done?');
|
console.info('done?');
|
||||||
setDone(true);
|
setDone(true);
|
||||||
setAnswer({} as IAnswer);
|
|
||||||
return { data: await res, response };
|
return { data: await res, response };
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setDone(true);
|
setDone(true);
|
||||||
setAnswer({} as IAnswer);
|
|
||||||
console.warn(e);
|
console.warn(e);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[url],
|
[url],
|
||||||
);
|
);
|
||||||
|
|
||||||
return { send, answer, done, setDone };
|
return { send, answer, done, setDone, resetAnswer };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useSpeechWithSse = (url: string = api.tts) => {
|
export const useSpeechWithSse = (url: string = api.tts) => {
|
||||||
|
@ -2,14 +2,14 @@ import { ReactComponent as FileIcon } from '@/assets/svg/file-management.svg';
|
|||||||
import { ReactComponent as GraphIcon } from '@/assets/svg/graph.svg';
|
import { ReactComponent as GraphIcon } from '@/assets/svg/graph.svg';
|
||||||
import { ReactComponent as KnowledgeBaseIcon } from '@/assets/svg/knowledge-base.svg';
|
import { ReactComponent as KnowledgeBaseIcon } from '@/assets/svg/knowledge-base.svg';
|
||||||
import { useTranslate } from '@/hooks/common-hooks';
|
import { useTranslate } from '@/hooks/common-hooks';
|
||||||
|
import { useFetchAppConf } from '@/hooks/logic-hooks';
|
||||||
import { useNavigateWithFromState } from '@/hooks/route-hook';
|
import { useNavigateWithFromState } from '@/hooks/route-hook';
|
||||||
|
import { MessageOutlined, SearchOutlined } from '@ant-design/icons';
|
||||||
import { Flex, Layout, Radio, Space, theme } from 'antd';
|
import { Flex, Layout, Radio, Space, theme } from 'antd';
|
||||||
import { useCallback, useMemo } from 'react';
|
import { useCallback, useMemo } from 'react';
|
||||||
import { useLocation } from 'umi';
|
import { useLocation } from 'umi';
|
||||||
import Toolbar from '../right-toolbar';
|
import Toolbar from '../right-toolbar';
|
||||||
|
|
||||||
import { useFetchAppConf } from '@/hooks/logic-hooks';
|
|
||||||
import { MessageOutlined } from '@ant-design/icons';
|
|
||||||
import styles from './index.less';
|
import styles from './index.less';
|
||||||
|
|
||||||
const { Header } = Layout;
|
const { Header } = Layout;
|
||||||
@ -27,7 +27,7 @@ const RagHeader = () => {
|
|||||||
() => [
|
() => [
|
||||||
{ path: '/knowledge', name: t('knowledgeBase'), icon: KnowledgeBaseIcon },
|
{ path: '/knowledge', name: t('knowledgeBase'), icon: KnowledgeBaseIcon },
|
||||||
{ path: '/chat', name: t('chat'), icon: MessageOutlined },
|
{ path: '/chat', name: t('chat'), icon: MessageOutlined },
|
||||||
// { path: '/search', name: t('search'), icon: SearchOutlined },
|
{ path: '/search', name: t('search'), icon: SearchOutlined },
|
||||||
{ path: '/flow', name: t('flow'), icon: GraphIcon },
|
{ path: '/flow', name: t('flow'), icon: GraphIcon },
|
||||||
{ path: '/file', name: t('fileManager'), icon: FileIcon },
|
{ path: '/file', name: t('fileManager'), icon: FileIcon },
|
||||||
],
|
],
|
||||||
|
@ -19,21 +19,12 @@ import {
|
|||||||
} from '@/hooks/common-hooks';
|
} from '@/hooks/common-hooks';
|
||||||
import {
|
import {
|
||||||
useRegenerateMessage,
|
useRegenerateMessage,
|
||||||
useRemoveMessageById,
|
|
||||||
useRemoveMessagesAfterCurrentMessage,
|
|
||||||
useScrollToBottom,
|
|
||||||
useSelectDerivedMessages,
|
useSelectDerivedMessages,
|
||||||
useSendMessageWithSse,
|
useSendMessageWithSse,
|
||||||
} from '@/hooks/logic-hooks';
|
} from '@/hooks/logic-hooks';
|
||||||
import {
|
import { IConversation, IDialog, Message } from '@/interfaces/database/chat';
|
||||||
IAnswer,
|
|
||||||
IConversation,
|
|
||||||
IDialog,
|
|
||||||
Message,
|
|
||||||
} from '@/interfaces/database/chat';
|
|
||||||
import { IChunk } from '@/interfaces/database/knowledge';
|
import { IChunk } from '@/interfaces/database/knowledge';
|
||||||
import { getFileExtension } from '@/utils';
|
import { getFileExtension } from '@/utils';
|
||||||
import { buildMessageUuid } from '@/utils/chat';
|
|
||||||
import { useMutationState } from '@tanstack/react-query';
|
import { useMutationState } from '@tanstack/react-query';
|
||||||
import { get } from 'lodash';
|
import { get } from 'lodash';
|
||||||
import trim from 'lodash/trim';
|
import trim from 'lodash/trim';
|
||||||
@ -251,118 +242,6 @@ export const useSetConversation = () => {
|
|||||||
return { setConversation };
|
return { setConversation };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useSelectCurrentConversation = () => {
|
|
||||||
const [currentConversation, setCurrentConversation] =
|
|
||||||
useState<IClientConversation>({} as IClientConversation);
|
|
||||||
const { data: conversation, loading } = useFetchNextConversation();
|
|
||||||
const { data: dialog } = useFetchNextDialog();
|
|
||||||
const { conversationId, dialogId } = useGetChatSearchParams();
|
|
||||||
const { removeMessageById } = useRemoveMessageById(setCurrentConversation);
|
|
||||||
const { removeMessagesAfterCurrentMessage } =
|
|
||||||
useRemoveMessagesAfterCurrentMessage(setCurrentConversation);
|
|
||||||
|
|
||||||
// Show the entered message in the conversation immediately after sending the message
|
|
||||||
const addNewestConversation = useCallback(
|
|
||||||
(message: Message, answer: string = '') => {
|
|
||||||
setCurrentConversation((pre) => {
|
|
||||||
return {
|
|
||||||
...pre,
|
|
||||||
message: [
|
|
||||||
...pre.message,
|
|
||||||
{
|
|
||||||
...message,
|
|
||||||
id: buildMessageUuid(message),
|
|
||||||
} as IMessage,
|
|
||||||
{
|
|
||||||
role: MessageType.Assistant,
|
|
||||||
content: answer,
|
|
||||||
id: buildMessageUuid({ ...message, role: MessageType.Assistant }),
|
|
||||||
reference: {},
|
|
||||||
} as IMessage,
|
|
||||||
],
|
|
||||||
};
|
|
||||||
});
|
|
||||||
},
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
|
|
||||||
// Add the streaming message to the last item in the message list
|
|
||||||
const addNewestAnswer = useCallback((answer: IAnswer) => {
|
|
||||||
setCurrentConversation((pre) => {
|
|
||||||
const latestMessage = pre.message?.at(-1);
|
|
||||||
|
|
||||||
if (latestMessage) {
|
|
||||||
return {
|
|
||||||
...pre,
|
|
||||||
message: [
|
|
||||||
...pre.message.slice(0, -1),
|
|
||||||
{
|
|
||||||
...latestMessage,
|
|
||||||
content: answer.answer,
|
|
||||||
reference: answer.reference,
|
|
||||||
id: buildMessageUuid({
|
|
||||||
id: answer.id,
|
|
||||||
role: MessageType.Assistant,
|
|
||||||
}),
|
|
||||||
prompt: answer.prompt,
|
|
||||||
} as IMessage,
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return pre;
|
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const removeLatestMessage = useCallback(() => {
|
|
||||||
setCurrentConversation((pre) => {
|
|
||||||
const nextMessages = pre.message?.slice(0, -2) ?? [];
|
|
||||||
return {
|
|
||||||
...pre,
|
|
||||||
message: nextMessages,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const addPrologue = useCallback(() => {
|
|
||||||
if (dialogId !== '' && conversationId === '') {
|
|
||||||
const prologue = dialog.prompt_config?.prologue;
|
|
||||||
|
|
||||||
const nextMessage = {
|
|
||||||
role: MessageType.Assistant,
|
|
||||||
content: prologue,
|
|
||||||
id: uuid(),
|
|
||||||
} as IMessage;
|
|
||||||
|
|
||||||
setCurrentConversation({
|
|
||||||
id: '',
|
|
||||||
dialog_id: dialogId,
|
|
||||||
reference: [],
|
|
||||||
message: [nextMessage],
|
|
||||||
} as any);
|
|
||||||
}
|
|
||||||
}, [conversationId, dialog, dialogId]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
addPrologue();
|
|
||||||
}, [addPrologue]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (conversationId) {
|
|
||||||
setCurrentConversation(conversation);
|
|
||||||
}
|
|
||||||
}, [conversation, conversationId]);
|
|
||||||
|
|
||||||
return {
|
|
||||||
currentConversation,
|
|
||||||
addNewestConversation,
|
|
||||||
removeLatestMessage,
|
|
||||||
addNewestAnswer,
|
|
||||||
removeMessageById,
|
|
||||||
removeMessagesAfterCurrentMessage,
|
|
||||||
loading,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
// export const useScrollToBottom = (currentConversation: IClientConversation) => {
|
// export const useScrollToBottom = (currentConversation: IClientConversation) => {
|
||||||
// const ref = useRef<HTMLDivElement>(null);
|
// const ref = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
@ -430,32 +309,6 @@ export const useSelectNextMessages = () => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useFetchConversationOnMount = () => {
|
|
||||||
const { conversationId } = useGetChatSearchParams();
|
|
||||||
const {
|
|
||||||
currentConversation,
|
|
||||||
addNewestConversation,
|
|
||||||
removeLatestMessage,
|
|
||||||
addNewestAnswer,
|
|
||||||
loading,
|
|
||||||
removeMessageById,
|
|
||||||
removeMessagesAfterCurrentMessage,
|
|
||||||
} = useSelectCurrentConversation();
|
|
||||||
const ref = useScrollToBottom(currentConversation);
|
|
||||||
|
|
||||||
return {
|
|
||||||
currentConversation,
|
|
||||||
addNewestConversation,
|
|
||||||
ref,
|
|
||||||
removeLatestMessage,
|
|
||||||
addNewestAnswer,
|
|
||||||
conversationId,
|
|
||||||
loading,
|
|
||||||
removeMessageById,
|
|
||||||
removeMessagesAfterCurrentMessage,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export const useHandleMessageInputChange = () => {
|
export const useHandleMessageInputChange = () => {
|
||||||
const [value, setValue] = useState('');
|
const [value, setValue] = useState('');
|
||||||
|
|
||||||
@ -477,7 +330,7 @@ export const useSendNextMessage = () => {
|
|||||||
const { conversationId } = useGetChatSearchParams();
|
const { conversationId } = useGetChatSearchParams();
|
||||||
const { handleInputChange, value, setValue } = useHandleMessageInputChange();
|
const { handleInputChange, value, setValue } = useHandleMessageInputChange();
|
||||||
const { handleClickConversation } = useClickConversationCard();
|
const { handleClickConversation } = useClickConversationCard();
|
||||||
const { send, answer, done, setDone } = useSendMessageWithSse();
|
const { send, answer, done, setDone, resetAnswer } = useSendMessageWithSse();
|
||||||
const {
|
const {
|
||||||
ref,
|
ref,
|
||||||
derivedMessages,
|
derivedMessages,
|
||||||
@ -557,10 +410,11 @@ export const useSendNextMessage = () => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// #1289
|
// #1289
|
||||||
|
console.log('🚀 ~ useEffect ~ answer:', answer, done);
|
||||||
if (
|
if (
|
||||||
answer.answer &&
|
answer.answer &&
|
||||||
!done &&
|
(answer?.conversationId === conversationId ||
|
||||||
(answer?.conversationId === conversationId || conversationId === '')
|
(!done && conversationId === ''))
|
||||||
) {
|
) {
|
||||||
addNewestAnswer(answer);
|
addNewestAnswer(answer);
|
||||||
}
|
}
|
||||||
@ -570,8 +424,10 @@ export const useSendNextMessage = () => {
|
|||||||
// #1289 switch to another conversion window when the last conversion answer doesn't finish.
|
// #1289 switch to another conversion window when the last conversion answer doesn't finish.
|
||||||
if (conversationId) {
|
if (conversationId) {
|
||||||
setDone(true);
|
setDone(true);
|
||||||
|
} else {
|
||||||
|
resetAnswer();
|
||||||
}
|
}
|
||||||
}, [setDone, conversationId]);
|
}, [setDone, conversationId, resetAnswer]);
|
||||||
|
|
||||||
const handlePressEnter = useCallback(
|
const handlePressEnter = useCallback(
|
||||||
(documentIds: string[]) => {
|
(documentIds: string[]) => {
|
||||||
|
@ -4,7 +4,7 @@ import { useSendMessageWithSse } from '@/hooks/logic-hooks';
|
|||||||
import { IAnswer } from '@/interfaces/database/chat';
|
import { IAnswer } from '@/interfaces/database/chat';
|
||||||
import api from '@/utils/api';
|
import api from '@/utils/api';
|
||||||
import { isEmpty } from 'lodash';
|
import { isEmpty } from 'lodash';
|
||||||
import { useCallback, useEffect, useState } from 'react';
|
import { ChangeEventHandler, useCallback, useEffect, useState } from 'react';
|
||||||
|
|
||||||
export const useSendQuestion = (kbIds: string[]) => {
|
export const useSendQuestion = (kbIds: string[]) => {
|
||||||
const { send, answer, done } = useSendMessageWithSse(api.ask);
|
const { send, answer, done } = useSendMessageWithSse(api.ask);
|
||||||
@ -18,6 +18,7 @@ export const useSendQuestion = (kbIds: string[]) => {
|
|||||||
data: mindMap,
|
data: mindMap,
|
||||||
loading: mindMapLoading,
|
loading: mindMapLoading,
|
||||||
} = useFetchMindMap();
|
} = useFetchMindMap();
|
||||||
|
const [searchStr, setSearchStr] = useState<string>('');
|
||||||
|
|
||||||
const sendQuestion = useCallback(
|
const sendQuestion = useCallback(
|
||||||
(question: string) => {
|
(question: string) => {
|
||||||
@ -34,10 +35,26 @@ export const useSendQuestion = (kbIds: string[]) => {
|
|||||||
[send, testChunk, kbIds, fetchRelatedQuestions, fetchMindMap],
|
[send, testChunk, kbIds, fetchRelatedQuestions, fetchMindMap],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const handleSearchStrChange: ChangeEventHandler<HTMLInputElement> =
|
||||||
|
useCallback((e) => {
|
||||||
|
setSearchStr(e.target.value);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleClickRelatedQuestion = useCallback(
|
||||||
|
(question: string) => () => {
|
||||||
|
setSearchStr(question);
|
||||||
|
sendQuestion(question);
|
||||||
|
},
|
||||||
|
[sendQuestion],
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isEmpty(answer)) {
|
if (!isEmpty(answer)) {
|
||||||
setCurrentAnswer(answer);
|
setCurrentAnswer(answer);
|
||||||
}
|
}
|
||||||
|
return () => {
|
||||||
|
setCurrentAnswer({} as IAnswer);
|
||||||
|
};
|
||||||
}, [answer]);
|
}, [answer]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -54,5 +71,8 @@ export const useSendQuestion = (kbIds: string[]) => {
|
|||||||
relatedQuestions: relatedQuestions?.slice(0, 5) ?? [],
|
relatedQuestions: relatedQuestions?.slice(0, 5) ?? [],
|
||||||
mindMap,
|
mindMap,
|
||||||
mindMapLoading,
|
mindMapLoading,
|
||||||
|
handleClickRelatedQuestion,
|
||||||
|
searchStr,
|
||||||
|
handleSearchStrChange,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -1,10 +1,17 @@
|
|||||||
.searchPage {
|
.searchPage {
|
||||||
.card {
|
.card {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
:global(.ant-card-body) {
|
||||||
|
padding: 14px;
|
||||||
|
}
|
||||||
|
p {
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.tag {
|
.tag {
|
||||||
padding: 4px 8px;
|
padding: 4px 8px;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,3 +63,12 @@
|
|||||||
padding-right: 10px;
|
padding-right: 10px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.answerWrapper {
|
||||||
|
background-color: #e6f4ff;
|
||||||
|
padding: 14px;
|
||||||
|
margin-top: 16px;
|
||||||
|
border-radius: 8px;
|
||||||
|
& > p {
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -19,11 +19,14 @@ const SearchPage = () => {
|
|||||||
const list = useSelectTestingResult();
|
const list = useSelectTestingResult();
|
||||||
const {
|
const {
|
||||||
sendQuestion,
|
sendQuestion,
|
||||||
|
handleClickRelatedQuestion,
|
||||||
|
handleSearchStrChange,
|
||||||
answer,
|
answer,
|
||||||
sendingLoading,
|
sendingLoading,
|
||||||
relatedQuestions,
|
relatedQuestions,
|
||||||
mindMap,
|
mindMap,
|
||||||
mindMapLoading,
|
mindMapLoading,
|
||||||
|
searchStr,
|
||||||
} = useSendQuestion(checkedList);
|
} = useSendQuestion(checkedList);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -37,18 +40,24 @@ const SearchPage = () => {
|
|||||||
<Flex className={styles.content}>
|
<Flex className={styles.content}>
|
||||||
<section className={styles.main}>
|
<section className={styles.main}>
|
||||||
<Search
|
<Search
|
||||||
|
value={searchStr}
|
||||||
|
onChange={handleSearchStrChange}
|
||||||
placeholder="input search text"
|
placeholder="input search text"
|
||||||
onSearch={sendQuestion}
|
onSearch={sendQuestion}
|
||||||
size="large"
|
size="large"
|
||||||
loading={sendingLoading}
|
loading={sendingLoading}
|
||||||
disabled={checkedList.length === 0}
|
disabled={checkedList.length === 0}
|
||||||
/>
|
/>
|
||||||
<MarkdownContent
|
{answer.answer && (
|
||||||
loading={sendingLoading}
|
<div className={styles.answerWrapper}>
|
||||||
content={answer.answer}
|
<MarkdownContent
|
||||||
reference={answer.reference ?? ({} as IReference)}
|
loading={sendingLoading}
|
||||||
clickDocumentButton={() => {}}
|
content={answer.answer}
|
||||||
></MarkdownContent>
|
reference={answer.reference ?? ({} as IReference)}
|
||||||
|
clickDocumentButton={() => {}}
|
||||||
|
></MarkdownContent>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<List
|
<List
|
||||||
dataSource={list.chunks}
|
dataSource={list.chunks}
|
||||||
renderItem={(item) => (
|
renderItem={(item) => (
|
||||||
@ -68,7 +77,11 @@ const SearchPage = () => {
|
|||||||
<Card>
|
<Card>
|
||||||
<Flex wrap="wrap" gap={'10px 0'}>
|
<Flex wrap="wrap" gap={'10px 0'}>
|
||||||
{relatedQuestions?.map((x, idx) => (
|
{relatedQuestions?.map((x, idx) => (
|
||||||
<Tag key={idx} className={styles.tag}>
|
<Tag
|
||||||
|
key={idx}
|
||||||
|
className={styles.tag}
|
||||||
|
onClick={handleClickRelatedQuestion(x)}
|
||||||
|
>
|
||||||
{x}
|
{x}
|
||||||
</Tag>
|
</Tag>
|
||||||
))}
|
))}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user