balibabu commited on
Commit
e890f0a
·
1 Parent(s): bd18037

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

Browse files

### What problem does this PR solve?

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

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

web/src/components/pdf-previewer/hooks.ts ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import axios from 'axios';
2
+ import { useCallback, useEffect, useState } from 'react';
3
+
4
+ export const useCatchDocumentError = (url: string) => {
5
+ const [error, setError] = useState<string>('');
6
+
7
+ const fetchDocument = useCallback(async () => {
8
+ const { data } = await axios.get(url);
9
+ if (data.retcode !== 0) {
10
+ setError(data?.retmsg);
11
+ }
12
+ }, [url]);
13
+ useEffect(() => {
14
+ fetchDocument();
15
+ }, [fetchDocument]);
16
+
17
+ return error;
18
+ };
web/src/components/pdf-previewer/index.tsx CHANGED
@@ -14,6 +14,8 @@ import {
14
  Popup,
15
  } from 'react-pdf-highlighter';
16
 
 
 
17
  import styles from './index.less';
18
 
19
  interface IProps {
@@ -38,6 +40,8 @@ const DocumentPreviewer = ({ chunk, documentId, visible }: IProps) => {
38
  const { highlights: state, setWidthAndHeight } = useGetChunkHighlights(chunk);
39
  const ref = useRef<(highlight: IHighlight) => void>(() => {});
40
  const [loaded, setLoaded] = useState(false);
 
 
41
 
42
  const resetHash = () => {};
43
 
@@ -55,9 +59,10 @@ const DocumentPreviewer = ({ chunk, documentId, visible }: IProps) => {
55
  return (
56
  <div className={styles.documentContainer}>
57
  <PdfLoader
58
- url={getDocumentUrl()}
59
  beforeLoad={<Skeleton active />}
60
  workerSrc="/pdfjs-dist/pdf.worker.min.js"
 
61
  >
62
  {(pdfDocument) => {
63
  pdfDocument.getPage(1).then((page) => {
 
14
  Popup,
15
  } from 'react-pdf-highlighter';
16
 
17
+ import FileError from '@/pages/document-viewer/file-error';
18
+ import { useCatchDocumentError } from './hooks';
19
  import styles from './index.less';
20
 
21
  interface IProps {
 
40
  const { highlights: state, setWidthAndHeight } = useGetChunkHighlights(chunk);
41
  const ref = useRef<(highlight: IHighlight) => void>(() => {});
42
  const [loaded, setLoaded] = useState(false);
43
+ const url = getDocumentUrl();
44
+ const error = useCatchDocumentError(url);
45
 
46
  const resetHash = () => {};
47
 
 
59
  return (
60
  <div className={styles.documentContainer}>
61
  <PdfLoader
62
+ url={url}
63
  beforeLoad={<Skeleton active />}
64
  workerSrc="/pdfjs-dist/pdf.worker.min.js"
65
+ errorMessage={<FileError>{error}</FileError>}
66
  >
67
  {(pdfDocument) => {
68
  pdfDocument.getPage(1).then((page) => {
web/src/pages/add-knowledge/components/knowledge-chunk/components/document-preview/preview.tsx CHANGED
@@ -11,6 +11,8 @@ import {
11
  import { useGetChunkHighlights } from '../../hooks';
12
  import { useGetDocumentUrl } from './hooks';
13
 
 
 
14
  import styles from './index.less';
15
 
16
  interface IProps {
@@ -30,9 +32,11 @@ const HighlightPopup = ({
30
  // TODO: merge with DocumentPreviewer
31
  const Preview = ({ selectedChunkId }: IProps) => {
32
  const url = useGetDocumentUrl();
 
33
  const { highlights: state, setWidthAndHeight } =
34
  useGetChunkHighlights(selectedChunkId);
35
  const ref = useRef<(highlight: IHighlight) => void>(() => {});
 
36
 
37
  const resetHash = () => {};
38
 
@@ -48,6 +52,7 @@ const Preview = ({ selectedChunkId }: IProps) => {
48
  url={url}
49
  beforeLoad={<Skeleton active />}
50
  workerSrc="/pdfjs-dist/pdf.worker.min.js"
 
51
  >
52
  {(pdfDocument) => {
53
  pdfDocument.getPage(1).then((page) => {
 
11
  import { useGetChunkHighlights } from '../../hooks';
12
  import { useGetDocumentUrl } from './hooks';
13
 
14
+ import { useCatchDocumentError } from '@/components/pdf-previewer/hooks';
15
+ import FileError from '@/pages/document-viewer/file-error';
16
  import styles from './index.less';
17
 
18
  interface IProps {
 
32
  // TODO: merge with DocumentPreviewer
33
  const Preview = ({ selectedChunkId }: IProps) => {
34
  const url = useGetDocumentUrl();
35
+ useCatchDocumentError(url);
36
  const { highlights: state, setWidthAndHeight } =
37
  useGetChunkHighlights(selectedChunkId);
38
  const ref = useRef<(highlight: IHighlight) => void>(() => {});
39
+ const error = useCatchDocumentError(url);
40
 
41
  const resetHash = () => {};
42
 
 
52
  url={url}
53
  beforeLoad={<Skeleton active />}
54
  workerSrc="/pdfjs-dist/pdf.worker.min.js"
55
+ errorMessage={<FileError>{error}</FileError>}
56
  >
57
  {(pdfDocument) => {
58
  pdfDocument.getPage(1).then((page) => {
web/src/pages/add-knowledge/components/knowledge-testing/testing-control/index.tsx CHANGED
@@ -1,5 +1,5 @@
1
  import SimilaritySlider from '@/components/similarity-slider';
2
- import { Button, Card, Divider, Flex, Form, Input, Slider } from 'antd';
3
  import { FormInstance } from 'antd/lib';
4
 
5
  import { useTranslate } from '@/hooks/commonHooks';
@@ -9,7 +9,6 @@ import styles from './index.less';
9
  type FieldType = {
10
  similarity_threshold?: number;
11
  vector_similarity_weight?: number;
12
- top_k?: number;
13
  question: string;
14
  };
15
 
@@ -36,22 +35,8 @@ const TestingControl = ({ form, handleTesting }: IProps) => {
36
  <p>{t('testingDescription')}</p>
37
  <Divider></Divider>
38
  <section>
39
- <Form
40
- name="testing"
41
- layout="vertical"
42
- form={form}
43
- initialValues={{
44
- top_k: 1024,
45
- }}
46
- >
47
  <SimilaritySlider isTooltipShown></SimilaritySlider>
48
- <Form.Item<FieldType>
49
- label="Top K"
50
- name={'top_k'}
51
- tooltip={t('topKTip')}
52
- >
53
- <Slider marks={{ 0: 0, 2048: 2048 }} max={2048} />
54
- </Form.Item>
55
  <Card size="small" title={t('testText')}>
56
  <Form.Item<FieldType>
57
  name={'question'}
 
1
  import SimilaritySlider from '@/components/similarity-slider';
2
+ import { Button, Card, Divider, Flex, Form, Input } from 'antd';
3
  import { FormInstance } from 'antd/lib';
4
 
5
  import { useTranslate } from '@/hooks/commonHooks';
 
9
  type FieldType = {
10
  similarity_threshold?: number;
11
  vector_similarity_weight?: number;
 
12
  question: string;
13
  };
14
 
 
35
  <p>{t('testingDescription')}</p>
36
  <Divider></Divider>
37
  <section>
38
+ <Form name="testing" layout="vertical" form={form}>
 
 
 
 
 
 
 
39
  <SimilaritySlider isTooltipShown></SimilaritySlider>
 
 
 
 
 
 
 
40
  <Card size="small" title={t('testText')}>
41
  <Form.Item<FieldType>
42
  name={'question'}
web/src/pages/document-viewer/file-error/index.tsx CHANGED
@@ -1,13 +1,17 @@
1
  import { Alert, Flex } from 'antd';
2
 
3
  import { useTranslate } from '@/hooks/commonHooks';
 
4
  import styles from './index.less';
5
 
6
- const FileError = () => {
7
  const { t } = useTranslate('fileManager');
8
  return (
9
  <Flex align="center" justify="center" className={styles.errorWrapper}>
10
- <Alert type="error" message={<h1>{t('fileError')}</h1>}></Alert>
 
 
 
11
  </Flex>
12
  );
13
  };
 
1
  import { Alert, Flex } from 'antd';
2
 
3
  import { useTranslate } from '@/hooks/commonHooks';
4
+ import React from 'react';
5
  import styles from './index.less';
6
 
7
+ const FileError = ({ children }: React.PropsWithChildren) => {
8
  const { t } = useTranslate('fileManager');
9
  return (
10
  <Flex align="center" justify="center" className={styles.errorWrapper}>
11
+ <Alert
12
+ type="error"
13
+ message={<h2>{children || t('fileError')}</h2>}
14
+ ></Alert>
15
  </Flex>
16
  );
17
  };