balibabu commited on
Commit
c39b5d3
·
1 Parent(s): 74e840b

feat: Retrieval chunks by page #2247 (#2373)

Browse files

### What problem does this PR solve?

feat: Retrieval chunks by page #2247

### Type of change

- [x] New Feature (non-breaking change which adds functionality)

web/src/components/retrieval-documents/index.tsx CHANGED
@@ -3,19 +3,22 @@ import { Collapse, Flex, Space } from 'antd';
3
  import SelectFiles from './select-files';
4
 
5
  import { useSelectTestingResult } from '@/hooks/knowledge-hooks';
6
- import { useState } from 'react';
7
  import { useTranslation } from 'react-i18next';
8
  import styles from './index.less';
9
 
10
  interface IProps {
11
- selectedDocumentIdsLength?: number;
12
  onTesting(documentIds: string[]): void;
 
 
13
  }
14
 
15
- const RetrievalDocuments = ({ onTesting }: IProps) => {
 
 
 
 
16
  const { t } = useTranslation();
17
  const { documents } = useSelectTestingResult();
18
- const [selectedDocumentIds, setSelectedDocumentIds] = useState<string[]>([]);
19
 
20
  return (
21
  <Collapse
 
3
  import SelectFiles from './select-files';
4
 
5
  import { useSelectTestingResult } from '@/hooks/knowledge-hooks';
 
6
  import { useTranslation } from 'react-i18next';
7
  import styles from './index.less';
8
 
9
  interface IProps {
 
10
  onTesting(documentIds: string[]): void;
11
+ setSelectedDocumentIds(documentIds: string[]): void;
12
+ selectedDocumentIds: string[];
13
  }
14
 
15
+ const RetrievalDocuments = ({
16
+ onTesting,
17
+ selectedDocumentIds,
18
+ setSelectedDocumentIds,
19
+ }: IProps) => {
20
  const { t } = useTranslation();
21
  const { documents } = useSelectTestingResult();
 
22
 
23
  return (
24
  <Collapse
web/src/layouts/components/header/index.tsx CHANGED
@@ -4,7 +4,7 @@ import { ReactComponent as KnowledgeBaseIcon } from '@/assets/svg/knowledge-base
4
  import { useTranslate } from '@/hooks/common-hooks';
5
  import { useFetchAppConf } from '@/hooks/logic-hooks';
6
  import { useNavigateWithFromState } from '@/hooks/route-hook';
7
- import { MessageOutlined } from '@ant-design/icons';
8
  import { Flex, Layout, Radio, Space, theme } from 'antd';
9
  import { useCallback, useMemo } from 'react';
10
  import { useLocation } from 'umi';
@@ -27,7 +27,7 @@ const RagHeader = () => {
27
  () => [
28
  { path: '/knowledge', name: t('knowledgeBase'), icon: KnowledgeBaseIcon },
29
  { path: '/chat', name: t('chat'), icon: MessageOutlined },
30
- // { path: '/search', name: t('search'), icon: SearchOutlined },
31
  { path: '/flow', name: t('flow'), icon: GraphIcon },
32
  { path: '/file', name: t('fileManager'), icon: FileIcon },
33
  ],
 
4
  import { useTranslate } from '@/hooks/common-hooks';
5
  import { useFetchAppConf } from '@/hooks/logic-hooks';
6
  import { useNavigateWithFromState } from '@/hooks/route-hook';
7
+ import { MessageOutlined, SearchOutlined } from '@ant-design/icons';
8
  import { Flex, Layout, Radio, Space, theme } from 'antd';
9
  import { useCallback, useMemo } from 'react';
10
  import { useLocation } from 'umi';
 
27
  () => [
28
  { path: '/knowledge', name: t('knowledgeBase'), icon: KnowledgeBaseIcon },
29
  { path: '/chat', name: t('chat'), icon: MessageOutlined },
30
+ { path: '/search', name: t('search'), icon: SearchOutlined },
31
  { path: '/flow', name: t('flow'), icon: GraphIcon },
32
  { path: '/file', name: t('fileManager'), icon: FileIcon },
33
  ],
web/src/pages/search/hooks.ts CHANGED
@@ -1,6 +1,9 @@
1
  import { useFetchMindMap, useFetchRelatedQuestions } from '@/hooks/chat-hooks';
2
  import { useTestChunkRetrieval } from '@/hooks/knowledge-hooks';
3
- import { useSendMessageWithSse } from '@/hooks/logic-hooks';
 
 
 
4
  import { IAnswer } from '@/interfaces/database/chat';
5
  import api from '@/utils/api';
6
  import { get, isEmpty, trim } from 'lodash';
@@ -20,6 +23,9 @@ export const useSendQuestion = (kbIds: string[]) => {
20
  } = useFetchMindMap();
21
  const [searchStr, setSearchStr] = useState<string>('');
22
  const [isFirstRender, setIsFirstRender] = useState(true);
 
 
 
23
 
24
  const sendQuestion = useCallback(
25
  (question: string) => {
@@ -55,7 +61,7 @@ export const useSendQuestion = (kbIds: string[]) => {
55
  );
56
 
57
  const handleTestChunk = useCallback(
58
- (documentIds: string[]) => {
59
  const q = trim(searchStr);
60
  if (sendingLoading || isEmpty(q)) return;
61
 
@@ -63,10 +69,12 @@ export const useSendQuestion = (kbIds: string[]) => {
63
  kb_id: kbIds,
64
  highlight: true,
65
  question: q,
66
- doc_ids: Array.isArray(documentIds) ? documentIds : [],
 
 
67
  });
68
  },
69
- [sendingLoading, searchStr, kbIds, testChunk],
70
  );
71
 
72
  useEffect(() => {
@@ -89,6 +97,7 @@ export const useSendQuestion = (kbIds: string[]) => {
89
  handleSearchStrChange,
90
  handleClickRelatedQuestion,
91
  handleTestChunk,
 
92
  loading,
93
  sendingLoading,
94
  answer: currentAnswer,
@@ -97,6 +106,7 @@ export const useSendQuestion = (kbIds: string[]) => {
97
  mindMapLoading,
98
  searchStr,
99
  isFirstRender,
 
100
  };
101
  };
102
 
@@ -124,3 +134,45 @@ export const useFetchBackgroundImage = () => {
124
 
125
  return `https://cn.bing.com${imgUrl}`;
126
  };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import { useFetchMindMap, useFetchRelatedQuestions } from '@/hooks/chat-hooks';
2
  import { useTestChunkRetrieval } from '@/hooks/knowledge-hooks';
3
+ import {
4
+ useGetPaginationWithRouter,
5
+ useSendMessageWithSse,
6
+ } from '@/hooks/logic-hooks';
7
  import { IAnswer } from '@/interfaces/database/chat';
8
  import api from '@/utils/api';
9
  import { get, isEmpty, trim } from 'lodash';
 
23
  } = useFetchMindMap();
24
  const [searchStr, setSearchStr] = useState<string>('');
25
  const [isFirstRender, setIsFirstRender] = useState(true);
26
+ const [selectedDocumentIds, setSelectedDocumentIds] = useState<string[]>([]);
27
+
28
+ const { pagination } = useGetPaginationWithRouter();
29
 
30
  const sendQuestion = useCallback(
31
  (question: string) => {
 
61
  );
62
 
63
  const handleTestChunk = useCallback(
64
+ (documentIds: string[], page: number = 1, size: number = 10) => {
65
  const q = trim(searchStr);
66
  if (sendingLoading || isEmpty(q)) return;
67
 
 
69
  kb_id: kbIds,
70
  highlight: true,
71
  question: q,
72
+ doc_ids: documentIds ?? selectedDocumentIds,
73
+ page,
74
+ size,
75
  });
76
  },
77
+ [sendingLoading, searchStr, kbIds, testChunk, selectedDocumentIds],
78
  );
79
 
80
  useEffect(() => {
 
97
  handleSearchStrChange,
98
  handleClickRelatedQuestion,
99
  handleTestChunk,
100
+ setSelectedDocumentIds,
101
  loading,
102
  sendingLoading,
103
  answer: currentAnswer,
 
106
  mindMapLoading,
107
  searchStr,
108
  isFirstRender,
109
+ selectedDocumentIds,
110
  };
111
  };
112
 
 
134
 
135
  return `https://cn.bing.com${imgUrl}`;
136
  };
137
+
138
+ export const useTestRetrieval = (
139
+ kbIds: string[],
140
+ searchStr: string,
141
+ sendingLoading: boolean,
142
+ ) => {
143
+ const { testChunk, loading } = useTestChunkRetrieval();
144
+ const { pagination } = useGetPaginationWithRouter();
145
+
146
+ const [selectedDocumentIds, setSelectedDocumentIds] = useState<string[]>([]);
147
+
148
+ const handleTestChunk = useCallback(() => {
149
+ const q = trim(searchStr);
150
+ if (sendingLoading || isEmpty(q)) return;
151
+
152
+ testChunk({
153
+ kb_id: kbIds,
154
+ highlight: true,
155
+ question: q,
156
+ doc_ids: Array.isArray(selectedDocumentIds) ? selectedDocumentIds : [],
157
+ page: pagination.current,
158
+ size: pagination.pageSize,
159
+ });
160
+ }, [
161
+ sendingLoading,
162
+ searchStr,
163
+ kbIds,
164
+ testChunk,
165
+ selectedDocumentIds,
166
+ pagination,
167
+ ]);
168
+
169
+ useEffect(() => {
170
+ handleTestChunk();
171
+ }, [handleTestChunk]);
172
+
173
+ return {
174
+ loading,
175
+ selectedDocumentIds,
176
+ setSelectedDocumentIds,
177
+ };
178
+ };
web/src/pages/search/index.less CHANGED
@@ -62,6 +62,10 @@
62
  // background-color: aqua;
63
  overflow: auto;
64
  padding: 20px 10px 10px;
 
 
 
 
65
  }
66
 
67
  .graph {
@@ -100,6 +104,9 @@
100
 
101
  .globalInput {
102
  width: 600px;
 
 
 
103
  .input();
104
  }
105
  .partialInput {
 
62
  // background-color: aqua;
63
  overflow: auto;
64
  padding: 20px 10px 10px;
65
+ .chunks {
66
+ // overflow: auto;
67
+ // height: 60vh;
68
+ }
69
  }
70
 
71
  .graph {
 
104
 
105
  .globalInput {
106
  width: 600px;
107
+ position: sticky;
108
+ top: 0;
109
+ z-index: 1;
110
  .input();
111
  }
112
  .partialInput {
web/src/pages/search/index.tsx CHANGED
@@ -1,7 +1,11 @@
1
  import HightLightMarkdown from '@/components/highlight-markdown';
2
  import { ImageWithPopover } from '@/components/image';
3
  import IndentedTree from '@/components/indented-tree/indented-tree';
 
 
 
4
  import { useSelectTestingResult } from '@/hooks/knowledge-hooks';
 
5
  import { IReference } from '@/interfaces/database/chat';
6
  import {
7
  Card,
@@ -10,19 +14,18 @@ import {
10
  Input,
11
  Layout,
12
  List,
 
 
13
  Skeleton,
14
  Space,
15
  Tag,
16
  } from 'antd';
17
  import { useState } from 'react';
 
18
  import MarkdownContent from '../chat/markdown-content';
19
  import { useFetchBackgroundImage, useSendQuestion } from './hooks';
20
  import SearchSidebar from './sidebar';
21
 
22
- import PdfDrawer from '@/components/pdf-drawer';
23
- import { useClickDrawer } from '@/components/pdf-drawer/hooks';
24
- import RetrievalDocuments from '@/components/retrieval-documents';
25
- import { useTranslation } from 'react-i18next';
26
  import styles from './index.less';
27
 
28
  const { Content } = Layout;
@@ -31,13 +34,14 @@ const { Search } = Input;
31
  const SearchPage = () => {
32
  const { t } = useTranslation();
33
  const [checkedList, setCheckedList] = useState<string[]>([]);
34
- const list = useSelectTestingResult();
35
  // const appConf = useFetchAppConf();
36
  const {
37
  sendQuestion,
38
  handleClickRelatedQuestion,
39
  handleSearchStrChange,
40
  handleTestChunk,
 
41
  answer,
42
  sendingLoading,
43
  relatedQuestions,
@@ -46,10 +50,17 @@ const SearchPage = () => {
46
  searchStr,
47
  loading,
48
  isFirstRender,
 
49
  } = useSendQuestion(checkedList);
50
  const { visible, hideModal, documentId, selectedChunk, clickDocumentButton } =
51
  useClickDrawer();
52
  const imgUrl = useFetchBackgroundImage();
 
 
 
 
 
 
53
 
54
  const InputSearch = (
55
  <Search
@@ -106,14 +117,16 @@ const SearchPage = () => {
106
  )}
107
  <Divider></Divider>
108
  <RetrievalDocuments
109
- selectedDocumentIdsLength={0}
 
110
  onTesting={handleTestChunk}
111
  ></RetrievalDocuments>
112
  <Divider></Divider>
113
- {list.chunks.length > 0 && (
114
  <List
115
- dataSource={list.chunks}
116
  loading={loading}
 
117
  renderItem={(item) => (
118
  <List.Item>
119
  <Card className={styles.card}>
@@ -145,6 +158,12 @@ const SearchPage = () => {
145
  </Flex>
146
  </Card>
147
  )}
 
 
 
 
 
 
148
  </section>
149
  <section className={styles.graph}>
150
  {mindMapLoading ? (
 
1
  import HightLightMarkdown from '@/components/highlight-markdown';
2
  import { ImageWithPopover } from '@/components/image';
3
  import IndentedTree from '@/components/indented-tree/indented-tree';
4
+ import PdfDrawer from '@/components/pdf-drawer';
5
+ import { useClickDrawer } from '@/components/pdf-drawer/hooks';
6
+ import RetrievalDocuments from '@/components/retrieval-documents';
7
  import { useSelectTestingResult } from '@/hooks/knowledge-hooks';
8
+ import { useGetPaginationWithRouter } from '@/hooks/logic-hooks';
9
  import { IReference } from '@/interfaces/database/chat';
10
  import {
11
  Card,
 
14
  Input,
15
  Layout,
16
  List,
17
+ Pagination,
18
+ PaginationProps,
19
  Skeleton,
20
  Space,
21
  Tag,
22
  } from 'antd';
23
  import { useState } from 'react';
24
+ import { useTranslation } from 'react-i18next';
25
  import MarkdownContent from '../chat/markdown-content';
26
  import { useFetchBackgroundImage, useSendQuestion } from './hooks';
27
  import SearchSidebar from './sidebar';
28
 
 
 
 
 
29
  import styles from './index.less';
30
 
31
  const { Content } = Layout;
 
34
  const SearchPage = () => {
35
  const { t } = useTranslation();
36
  const [checkedList, setCheckedList] = useState<string[]>([]);
37
+ const { chunks, total } = useSelectTestingResult();
38
  // const appConf = useFetchAppConf();
39
  const {
40
  sendQuestion,
41
  handleClickRelatedQuestion,
42
  handleSearchStrChange,
43
  handleTestChunk,
44
+ setSelectedDocumentIds,
45
  answer,
46
  sendingLoading,
47
  relatedQuestions,
 
50
  searchStr,
51
  loading,
52
  isFirstRender,
53
+ selectedDocumentIds,
54
  } = useSendQuestion(checkedList);
55
  const { visible, hideModal, documentId, selectedChunk, clickDocumentButton } =
56
  useClickDrawer();
57
  const imgUrl = useFetchBackgroundImage();
58
+ const { pagination } = useGetPaginationWithRouter();
59
+
60
+ const onChange: PaginationProps['onChange'] = (pageNumber, pageSize) => {
61
+ pagination.onChange?.(pageNumber, pageSize);
62
+ handleTestChunk(selectedDocumentIds, pageNumber, pageSize);
63
+ };
64
 
65
  const InputSearch = (
66
  <Search
 
117
  )}
118
  <Divider></Divider>
119
  <RetrievalDocuments
120
+ selectedDocumentIds={selectedDocumentIds}
121
+ setSelectedDocumentIds={setSelectedDocumentIds}
122
  onTesting={handleTestChunk}
123
  ></RetrievalDocuments>
124
  <Divider></Divider>
125
+ {chunks.length > 0 && (
126
  <List
127
+ dataSource={chunks}
128
  loading={loading}
129
+ className={styles.chunks}
130
  renderItem={(item) => (
131
  <List.Item>
132
  <Card className={styles.card}>
 
158
  </Flex>
159
  </Card>
160
  )}
161
+ <Divider></Divider>
162
+ <Pagination
163
+ {...pagination}
164
+ total={total}
165
+ onChange={onChange}
166
+ />
167
  </section>
168
  <section className={styles.graph}>
169
  {mindMapLoading ? (