Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Retrieval chunks by page #2247 #2373

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions web/src/components/retrieval-documents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ import { Collapse, Flex, Space } from 'antd';
import SelectFiles from './select-files';

import { useSelectTestingResult } from '@/hooks/knowledge-hooks';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import styles from './index.less';

interface IProps {
selectedDocumentIdsLength?: number;
onTesting(documentIds: string[]): void;
setSelectedDocumentIds(documentIds: string[]): void;
selectedDocumentIds: string[];
}

const RetrievalDocuments = ({ onTesting }: IProps) => {
const RetrievalDocuments = ({
onTesting,
selectedDocumentIds,
setSelectedDocumentIds,
}: IProps) => {
const { t } = useTranslation();
const { documents } = useSelectTestingResult();
const [selectedDocumentIds, setSelectedDocumentIds] = useState<string[]>([]);

return (
<Collapse
Expand Down
4 changes: 2 additions & 2 deletions web/src/layouts/components/header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ReactComponent as KnowledgeBaseIcon } from '@/assets/svg/knowledge-base
import { useTranslate } from '@/hooks/common-hooks';
import { useFetchAppConf } from '@/hooks/logic-hooks';
import { useNavigateWithFromState } from '@/hooks/route-hook';
import { MessageOutlined } from '@ant-design/icons';
import { MessageOutlined, SearchOutlined } from '@ant-design/icons';
import { Flex, Layout, Radio, Space, theme } from 'antd';
import { useCallback, useMemo } from 'react';
import { useLocation } from 'umi';
Expand All @@ -27,7 +27,7 @@ const RagHeader = () => {
() => [
{ path: '/knowledge', name: t('knowledgeBase'), icon: KnowledgeBaseIcon },
{ 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: '/file', name: t('fileManager'), icon: FileIcon },
],
Expand Down
60 changes: 56 additions & 4 deletions web/src/pages/search/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { useFetchMindMap, useFetchRelatedQuestions } from '@/hooks/chat-hooks';
import { useTestChunkRetrieval } from '@/hooks/knowledge-hooks';
import { useSendMessageWithSse } from '@/hooks/logic-hooks';
import {
useGetPaginationWithRouter,
useSendMessageWithSse,
} from '@/hooks/logic-hooks';
import { IAnswer } from '@/interfaces/database/chat';
import api from '@/utils/api';
import { get, isEmpty, trim } from 'lodash';
Expand All @@ -20,6 +23,9 @@ export const useSendQuestion = (kbIds: string[]) => {
} = useFetchMindMap();
const [searchStr, setSearchStr] = useState<string>('');
const [isFirstRender, setIsFirstRender] = useState(true);
const [selectedDocumentIds, setSelectedDocumentIds] = useState<string[]>([]);

const { pagination } = useGetPaginationWithRouter();

const sendQuestion = useCallback(
(question: string) => {
Expand Down Expand Up @@ -55,18 +61,20 @@ export const useSendQuestion = (kbIds: string[]) => {
);

const handleTestChunk = useCallback(
(documentIds: string[]) => {
(documentIds: string[], page: number = 1, size: number = 10) => {
const q = trim(searchStr);
if (sendingLoading || isEmpty(q)) return;

testChunk({
kb_id: kbIds,
highlight: true,
question: q,
doc_ids: Array.isArray(documentIds) ? documentIds : [],
doc_ids: documentIds ?? selectedDocumentIds,
page,
size,
});
},
[sendingLoading, searchStr, kbIds, testChunk],
[sendingLoading, searchStr, kbIds, testChunk, selectedDocumentIds],
);

useEffect(() => {
Expand All @@ -89,6 +97,7 @@ export const useSendQuestion = (kbIds: string[]) => {
handleSearchStrChange,
handleClickRelatedQuestion,
handleTestChunk,
setSelectedDocumentIds,
loading,
sendingLoading,
answer: currentAnswer,
Expand All @@ -97,6 +106,7 @@ export const useSendQuestion = (kbIds: string[]) => {
mindMapLoading,
searchStr,
isFirstRender,
selectedDocumentIds,
};
};

Expand Down Expand Up @@ -124,3 +134,45 @@ export const useFetchBackgroundImage = () => {

return `https://cn.bing.com${imgUrl}`;
};

export const useTestRetrieval = (
kbIds: string[],
searchStr: string,
sendingLoading: boolean,
) => {
const { testChunk, loading } = useTestChunkRetrieval();
const { pagination } = useGetPaginationWithRouter();

const [selectedDocumentIds, setSelectedDocumentIds] = useState<string[]>([]);

const handleTestChunk = useCallback(() => {
const q = trim(searchStr);
if (sendingLoading || isEmpty(q)) return;

testChunk({
kb_id: kbIds,
highlight: true,
question: q,
doc_ids: Array.isArray(selectedDocumentIds) ? selectedDocumentIds : [],
page: pagination.current,
size: pagination.pageSize,
});
}, [
sendingLoading,
searchStr,
kbIds,
testChunk,
selectedDocumentIds,
pagination,
]);

useEffect(() => {
handleTestChunk();
}, [handleTestChunk]);

return {
loading,
selectedDocumentIds,
setSelectedDocumentIds,
};
};
7 changes: 7 additions & 0 deletions web/src/pages/search/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
// background-color: aqua;
overflow: auto;
padding: 20px 10px 10px;
.chunks {
// overflow: auto;
// height: 60vh;
}
}

.graph {
Expand Down Expand Up @@ -100,6 +104,9 @@

.globalInput {
width: 600px;
position: sticky;
top: 0;
z-index: 1;
.input();
}
.partialInput {
Expand Down
35 changes: 27 additions & 8 deletions web/src/pages/search/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import HightLightMarkdown from '@/components/highlight-markdown';
import { ImageWithPopover } from '@/components/image';
import IndentedTree from '@/components/indented-tree/indented-tree';
import PdfDrawer from '@/components/pdf-drawer';
import { useClickDrawer } from '@/components/pdf-drawer/hooks';
import RetrievalDocuments from '@/components/retrieval-documents';
import { useSelectTestingResult } from '@/hooks/knowledge-hooks';
import { useGetPaginationWithRouter } from '@/hooks/logic-hooks';
import { IReference } from '@/interfaces/database/chat';
import {
Card,
Expand All @@ -10,19 +14,18 @@ import {
Input,
Layout,
List,
Pagination,
PaginationProps,
Skeleton,
Space,
Tag,
} from 'antd';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import MarkdownContent from '../chat/markdown-content';
import { useFetchBackgroundImage, useSendQuestion } from './hooks';
import SearchSidebar from './sidebar';

import PdfDrawer from '@/components/pdf-drawer';
import { useClickDrawer } from '@/components/pdf-drawer/hooks';
import RetrievalDocuments from '@/components/retrieval-documents';
import { useTranslation } from 'react-i18next';
import styles from './index.less';

const { Content } = Layout;
Expand All @@ -31,13 +34,14 @@ const { Search } = Input;
const SearchPage = () => {
const { t } = useTranslation();
const [checkedList, setCheckedList] = useState<string[]>([]);
const list = useSelectTestingResult();
const { chunks, total } = useSelectTestingResult();
// const appConf = useFetchAppConf();
const {
sendQuestion,
handleClickRelatedQuestion,
handleSearchStrChange,
handleTestChunk,
setSelectedDocumentIds,
answer,
sendingLoading,
relatedQuestions,
Expand All @@ -46,10 +50,17 @@ const SearchPage = () => {
searchStr,
loading,
isFirstRender,
selectedDocumentIds,
} = useSendQuestion(checkedList);
const { visible, hideModal, documentId, selectedChunk, clickDocumentButton } =
useClickDrawer();
const imgUrl = useFetchBackgroundImage();
const { pagination } = useGetPaginationWithRouter();

const onChange: PaginationProps['onChange'] = (pageNumber, pageSize) => {
pagination.onChange?.(pageNumber, pageSize);
handleTestChunk(selectedDocumentIds, pageNumber, pageSize);
};

const InputSearch = (
<Search
Expand Down Expand Up @@ -106,14 +117,16 @@ const SearchPage = () => {
)}
<Divider></Divider>
<RetrievalDocuments
selectedDocumentIdsLength={0}
selectedDocumentIds={selectedDocumentIds}
setSelectedDocumentIds={setSelectedDocumentIds}
onTesting={handleTestChunk}
></RetrievalDocuments>
<Divider></Divider>
{list.chunks.length > 0 && (
{chunks.length > 0 && (
<List
dataSource={list.chunks}
dataSource={chunks}
loading={loading}
className={styles.chunks}
renderItem={(item) => (
<List.Item>
<Card className={styles.card}>
Expand Down Expand Up @@ -145,6 +158,12 @@ const SearchPage = () => {
</Flex>
</Card>
)}
<Divider></Divider>
<Pagination
{...pagination}
total={total}
onChange={onChange}
/>
</section>
<section className={styles.graph}>
{mindMapLoading ? (
Expand Down