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

fix: remove Top K in retrieval testing #770 and if the document parsing fails, the error message returned by the backend is displayed #782

Merged
merged 2 commits into from
May 15, 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
18 changes: 18 additions & 0 deletions web/src/components/pdf-previewer/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import axios from 'axios';
import { useCallback, useEffect, useState } from 'react';

export const useCatchDocumentError = (url: string) => {
const [error, setError] = useState<string>('');

const fetchDocument = useCallback(async () => {
const { data } = await axios.get(url);
if (data.retcode !== 0) {
setError(data?.retmsg);
}
}, [url]);
useEffect(() => {
fetchDocument();
}, [fetchDocument]);

return error;
};
7 changes: 6 additions & 1 deletion web/src/components/pdf-previewer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
Popup,
} from 'react-pdf-highlighter';

import FileError from '@/pages/document-viewer/file-error';
import { useCatchDocumentError } from './hooks';
import styles from './index.less';

interface IProps {
Expand All @@ -38,6 +40,8 @@ const DocumentPreviewer = ({ chunk, documentId, visible }: IProps) => {
const { highlights: state, setWidthAndHeight } = useGetChunkHighlights(chunk);
const ref = useRef<(highlight: IHighlight) => void>(() => {});
const [loaded, setLoaded] = useState(false);
const url = getDocumentUrl();
const error = useCatchDocumentError(url);

const resetHash = () => {};

Expand All @@ -55,9 +59,10 @@ const DocumentPreviewer = ({ chunk, documentId, visible }: IProps) => {
return (
<div className={styles.documentContainer}>
<PdfLoader
url={getDocumentUrl()}
url={url}
beforeLoad={<Skeleton active />}
workerSrc="/pdfjs-dist/pdf.worker.min.js"
errorMessage={<FileError>{error}</FileError>}
>
{(pdfDocument) => {
pdfDocument.getPage(1).then((page) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
import { useGetChunkHighlights } from '../../hooks';
import { useGetDocumentUrl } from './hooks';

import { useCatchDocumentError } from '@/components/pdf-previewer/hooks';
import FileError from '@/pages/document-viewer/file-error';
import styles from './index.less';

interface IProps {
Expand All @@ -30,9 +32,11 @@ const HighlightPopup = ({
// TODO: merge with DocumentPreviewer
const Preview = ({ selectedChunkId }: IProps) => {
const url = useGetDocumentUrl();
useCatchDocumentError(url);
const { highlights: state, setWidthAndHeight } =
useGetChunkHighlights(selectedChunkId);
const ref = useRef<(highlight: IHighlight) => void>(() => {});
const error = useCatchDocumentError(url);

const resetHash = () => {};

Expand All @@ -48,6 +52,7 @@ const Preview = ({ selectedChunkId }: IProps) => {
url={url}
beforeLoad={<Skeleton active />}
workerSrc="/pdfjs-dist/pdf.worker.min.js"
errorMessage={<FileError>{error}</FileError>}
>
{(pdfDocument) => {
pdfDocument.getPage(1).then((page) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import SimilaritySlider from '@/components/similarity-slider';
import { Button, Card, Divider, Flex, Form, Input, Slider } from 'antd';
import { Button, Card, Divider, Flex, Form, Input } from 'antd';
import { FormInstance } from 'antd/lib';

import { useTranslate } from '@/hooks/commonHooks';
Expand All @@ -9,7 +9,6 @@ import styles from './index.less';
type FieldType = {
similarity_threshold?: number;
vector_similarity_weight?: number;
top_k?: number;
question: string;
};

Expand All @@ -36,22 +35,8 @@ const TestingControl = ({ form, handleTesting }: IProps) => {
<p>{t('testingDescription')}</p>
<Divider></Divider>
<section>
<Form
name="testing"
layout="vertical"
form={form}
initialValues={{
top_k: 1024,
}}
>
<Form name="testing" layout="vertical" form={form}>
<SimilaritySlider isTooltipShown></SimilaritySlider>
<Form.Item<FieldType>
label="Top K"
name={'top_k'}
tooltip={t('topKTip')}
>
<Slider marks={{ 0: 0, 2048: 2048 }} max={2048} />
</Form.Item>
<Card size="small" title={t('testText')}>
<Form.Item<FieldType>
name={'question'}
Expand Down
8 changes: 6 additions & 2 deletions web/src/pages/document-viewer/file-error/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Alert, Flex } from 'antd';

import { useTranslate } from '@/hooks/commonHooks';
import React from 'react';
import styles from './index.less';

const FileError = () => {
const FileError = ({ children }: React.PropsWithChildren) => {
const { t } = useTranslate('fileManager');
return (
<Flex align="center" justify="center" className={styles.errorWrapper}>
<Alert type="error" message={<h1>{t('fileError')}</h1>}></Alert>
<Alert
type="error"
message={<h2>{children || t('fileError')}</h2>}
></Alert>
</Flex>
);
};
Expand Down