Skip to content

Commit 925dd2a

Browse files
authored
feat: Search for the answers you want based on the selected knowledge base #2247 (#2287)
### What problem does this PR solve? feat: Search for the answers you want based on the selected knowledge base #2247 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
1 parent b5a2711 commit 925dd2a

File tree

8 files changed

+45
-102
lines changed

8 files changed

+45
-102
lines changed

web/src/components/message-item/index.tsx

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReactComponent as AssistantIcon } from '@/assets/svg/assistant.svg';
22
import { MessageType } from '@/constants/chat';
3-
import { useSetModalState, useTranslate } from '@/hooks/common-hooks';
3+
import { useSetModalState } from '@/hooks/common-hooks';
44
import { useSelectFileThumbnails } from '@/hooks/knowledge-hooks';
55
import { IReference } from '@/interfaces/database/chat';
66
import { IChunk } from '@/interfaces/database/knowledge';
@@ -50,7 +50,6 @@ const MessageItem = ({
5050
}: IProps) => {
5151
const isAssistant = item.role === MessageType.Assistant;
5252
const isUser = item.role === MessageType.User;
53-
const { t } = useTranslate('chat');
5453
const fileThumbnails = useSelectFileThumbnails();
5554
const { data: documentList, setDocumentIds } = useFetchDocumentInfosByIds();
5655
const { data: documentThumbnails, setDocumentIds: setIds } =
@@ -62,14 +61,6 @@ const MessageItem = ({
6261
return reference?.doc_aggs ?? [];
6362
}, [reference?.doc_aggs]);
6463

65-
const content = useMemo(() => {
66-
let text = item.content;
67-
if (text === '') {
68-
text = t('searching');
69-
}
70-
return loading ? text?.concat('~~2$$') : text;
71-
}, [item.content, loading, t]);
72-
7364
const handleUserDocumentClick = useCallback(
7465
(id: string) => () => {
7566
setClickedDocumentId(id);
@@ -154,7 +145,8 @@ const MessageItem = ({
154145
}
155146
>
156147
<MarkdownContent
157-
content={content}
148+
loading={loading}
149+
content={item.content}
158150
reference={reference}
159151
clickDocumentButton={clickDocumentButton}
160152
></MarkdownContent>

web/src/hooks/knowledge-hooks.ts

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ export const useTestChunkRetrieval = (): ResponsePostType<ITestingResult> & {
206206
mutateAsync,
207207
} = useMutation({
208208
mutationKey: ['testChunk'], // This method is invalid
209+
gcTime: 0,
209210
mutationFn: async (values: any) => {
210211
const { data } = await kbService.retrieval_test({
211212
...values,

web/src/hooks/logic-hooks.ts

-63
Original file line numberDiff line numberDiff line change
@@ -297,69 +297,6 @@ export const useSpeechWithSse = (url: string = api.tts) => {
297297
return { read };
298298
};
299299

300-
export const useFetchAudioWithSse = (url: string = api.tts) => {
301-
// const [answer, setAnswer] = useState<IAnswer>({} as IAnswer);
302-
const [done, setDone] = useState(true);
303-
304-
const read = useCallback(
305-
async (
306-
body: any,
307-
): Promise<{ response: Response; data: ResponseType } | undefined> => {
308-
try {
309-
setDone(false);
310-
const response = await fetch(url, {
311-
method: 'POST',
312-
headers: {
313-
[Authorization]: getAuthorization(),
314-
'Content-Type': 'application/json',
315-
},
316-
body: JSON.stringify(body),
317-
});
318-
319-
const res = response.clone().json();
320-
321-
const reader = response?.body?.getReader();
322-
323-
while (true) {
324-
const x = await reader?.read();
325-
if (x) {
326-
const { done, value } = x;
327-
try {
328-
// const val = JSON.parse(value || '');
329-
const val = value;
330-
// const d = val?.data;
331-
// if (typeof d !== 'boolean') {
332-
// console.info('data:', d);
333-
// setAnswer({
334-
// ...d,
335-
// conversationId: body?.conversation_id,
336-
// });
337-
// }
338-
} catch (e) {
339-
console.warn(e);
340-
}
341-
if (done) {
342-
console.info('done');
343-
break;
344-
}
345-
}
346-
}
347-
console.info('done?');
348-
setDone(true);
349-
// setAnswer({} as IAnswer);
350-
return { data: await res, response };
351-
} catch (e) {
352-
setDone(true);
353-
// setAnswer({} as IAnswer);
354-
console.warn(e);
355-
}
356-
},
357-
[url],
358-
);
359-
360-
return { read, done, setDone };
361-
};
362-
363300
//#region chat hooks
364301

365302
export const useScrollToBottom = (messages?: unknown) => {

web/src/layouts/components/header/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useLocation } from 'umi';
99
import Toolbar from '../right-toolbar';
1010

1111
import { useFetchAppConf } from '@/hooks/logic-hooks';
12-
import { MessageOutlined } from '@ant-design/icons';
12+
import { MessageOutlined, SearchOutlined } from '@ant-design/icons';
1313
import styles from './index.less';
1414

1515
const { Header } = Layout;
@@ -27,7 +27,7 @@ const RagHeader = () => {
2727
() => [
2828
{ path: '/knowledge', name: t('knowledgeBase'), icon: KnowledgeBaseIcon },
2929
{ path: '/chat', name: t('chat'), icon: MessageOutlined },
30-
// { path: '/search', name: t('search'), icon: SearchOutlined },
30+
{ path: '/search', name: t('search'), icon: SearchOutlined },
3131
{ path: '/flow', name: t('flow'), icon: GraphIcon },
3232
{ path: '/file', name: t('fileManager'), icon: FileIcon },
3333
],

web/src/pages/chat/markdown-content/index.tsx

+14-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import { getExtension } from '@/utils/document-util';
77
import { InfoCircleOutlined } from '@ant-design/icons';
88
import { Button, Flex, Popover, Space } from 'antd';
99
import DOMPurify from 'dompurify';
10-
import { useCallback } from 'react';
10+
import { useCallback, useMemo } from 'react';
1111
import Markdown from 'react-markdown';
1212
import reactStringReplace from 'react-string-replace';
1313
import SyntaxHighlighter from 'react-syntax-highlighter';
1414
import remarkGfm from 'remark-gfm';
1515
import { visitParents } from 'unist-util-visit-parents';
1616

17+
import { useTranslation } from 'react-i18next';
1718
import styles from './index.less';
1819

1920
const reg = /(#{2}\d+\${2})/g;
@@ -25,11 +26,22 @@ const MarkdownContent = ({
2526
reference,
2627
clickDocumentButton,
2728
content,
29+
loading,
2830
}: {
2931
content: string;
32+
loading: boolean;
3033
reference: IReference;
3134
clickDocumentButton?: (documentId: string, chunk: IChunk) => void;
3235
}) => {
36+
const { t } = useTranslation();
37+
const contentWithCursor = useMemo(() => {
38+
let text = content;
39+
if (text === '') {
40+
text = t('chat.searching');
41+
}
42+
return loading ? text?.concat('~~2$$') : text;
43+
}, [content, loading, t]);
44+
3345
const fileThumbnails = useSelectFileThumbnails();
3446

3547
const handleDocumentButtonClick = useCallback(
@@ -173,7 +185,7 @@ const MarkdownContent = ({
173185
} as any
174186
}
175187
>
176-
{content}
188+
{contentWithCursor}
177189
</Markdown>
178190
);
179191
};

web/src/pages/search/hooks.ts

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
1-
import { MessageType } from '@/constants/chat';
21
import { useTestChunkRetrieval } from '@/hooks/knowledge-hooks';
32
import { useSendMessageWithSse } from '@/hooks/logic-hooks';
3+
import { IAnswer } from '@/interfaces/database/chat';
44
import api from '@/utils/api';
5-
import { useCallback, useEffect, useMemo, useState } from 'react';
6-
import { IMessage } from '../chat/interface';
5+
import { isEmpty } from 'lodash';
6+
import { useCallback, useEffect, useState } from 'react';
77

88
export const useSendQuestion = (kbIds: string[]) => {
99
const { send, answer, done } = useSendMessageWithSse(api.ask);
1010
const { testChunk, loading } = useTestChunkRetrieval();
1111
const [sendingLoading, setSendingLoading] = useState(false);
12-
13-
const message: IMessage = useMemo(() => {
14-
return {
15-
id: '',
16-
content: answer.answer,
17-
role: MessageType.Assistant,
18-
reference: answer.reference,
19-
};
20-
}, [answer]);
12+
const [currentAnswer, setCurrentAnswer] = useState({} as IAnswer);
2113

2214
const sendQuestion = useCallback(
2315
(question: string) => {
16+
setCurrentAnswer({} as IAnswer);
2417
setSendingLoading(true);
2518
send({ kb_ids: kbIds, question });
2619
testChunk({ kb_id: kbIds, highlight: true, question });
2720
},
2821
[send, testChunk, kbIds],
2922
);
3023

24+
useEffect(() => {
25+
if (!isEmpty(answer)) {
26+
setCurrentAnswer(answer);
27+
}
28+
}, [answer]);
29+
3130
useEffect(() => {
3231
if (done) {
3332
setSendingLoading(false);
3433
}
3534
}, [done]);
3635

37-
return { sendQuestion, message, loading, sendingLoading };
36+
return { sendQuestion, loading, sendingLoading, answer: currentAnswer };
3837
};

web/src/pages/search/index.less

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.searchPage {
2-
// height: 100%;
2+
.card {
3+
width: 100%;
4+
}
35
}
46

57
.searchSide {

web/src/pages/search/index.tsx

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import HightLightMarkdown from '@/components/highlight-markdown';
22
import { ImageWithPopover } from '@/components/image';
3-
import MessageItem from '@/components/message-item';
43
import { useSelectTestingResult } from '@/hooks/knowledge-hooks';
54
import { IReference } from '@/interfaces/database/chat';
65
import { Card, Flex, Input, Layout, List, Space } from 'antd';
76
import { useState } from 'react';
7+
import MarkdownContent from '../chat/markdown-content';
88
import { useSendQuestion } from './hooks';
99
import SearchSidebar from './sidebar';
1010

@@ -16,8 +16,7 @@ const { Search } = Input;
1616
const SearchPage = () => {
1717
const [checkedList, setCheckedList] = useState<string[]>([]);
1818
const list = useSelectTestingResult();
19-
const { sendQuestion, message, sendingLoading } =
20-
useSendQuestion(checkedList);
19+
const { sendQuestion, answer, sendingLoading } = useSendQuestion(checkedList);
2120

2221
return (
2322
<Layout className={styles.searchPage}>
@@ -33,19 +32,20 @@ const SearchPage = () => {
3332
placeholder="input search text"
3433
onSearch={sendQuestion}
3534
size="large"
35+
loading={sendingLoading}
36+
disabled={checkedList.length === 0}
3637
/>
37-
<MessageItem
38-
item={message}
39-
nickname="You"
40-
reference={message.reference ?? ({} as IReference)}
38+
<MarkdownContent
4139
loading={sendingLoading}
42-
index={0}
43-
></MessageItem>
40+
content={answer.answer}
41+
reference={answer.reference ?? ({} as IReference)}
42+
clickDocumentButton={() => {}}
43+
></MarkdownContent>
4444
<List
4545
dataSource={list.chunks}
4646
renderItem={(item) => (
4747
<List.Item>
48-
<Card>
48+
<Card className={styles.card}>
4949
<Space>
5050
<ImageWithPopover id={item.img_id}></ImageWithPopover>
5151
<HightLightMarkdown>

0 commit comments

Comments
 (0)