You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { ReactComponent as SelectedFilesCollapseIcon } from '@/assets/svg/selected-files-collapse.svg';
  2. import Image from '@/components/image';
  3. import { useTranslate } from '@/hooks/common-hooks';
  4. import { ITestingChunk } from '@/interfaces/database/knowledge';
  5. import {
  6. Card,
  7. Collapse,
  8. Empty,
  9. Flex,
  10. Pagination,
  11. PaginationProps,
  12. Popover,
  13. Space,
  14. } from 'antd';
  15. import camelCase from 'lodash/camelCase';
  16. import SelectFiles from './select-files';
  17. import {
  18. useSelectIsTestingSuccess,
  19. useSelectTestingResult,
  20. } from '@/hooks/knowledge-hooks';
  21. import { useGetPaginationWithRouter } from '@/hooks/logic-hooks';
  22. import { useCallback, useState } from 'react';
  23. import styles from './index.less';
  24. const similarityList: Array<{ field: keyof ITestingChunk; label: string }> = [
  25. { field: 'similarity', label: 'Hybrid Similarity' },
  26. { field: 'term_similarity', label: 'Term Similarity' },
  27. { field: 'vector_similarity', label: 'Vector Similarity' },
  28. ];
  29. const ChunkTitle = ({ item }: { item: ITestingChunk }) => {
  30. const { t } = useTranslate('knowledgeDetails');
  31. return (
  32. <Flex gap={10}>
  33. {similarityList.map((x) => (
  34. <Space key={x.field}>
  35. <span className={styles.similarityCircle}>
  36. {((item[x.field] as number) * 100).toFixed(2)}
  37. </span>
  38. <span className={styles.similarityText}>{t(camelCase(x.field))}</span>
  39. </Space>
  40. ))}
  41. </Flex>
  42. );
  43. };
  44. interface IProps {
  45. handleTesting: (documentIds?: string[]) => Promise<any>;
  46. }
  47. const TestingResult = ({ handleTesting }: IProps) => {
  48. const [selectedDocumentIds, setSelectedDocumentIds] = useState<string[]>([]);
  49. const { documents, chunks, total } = useSelectTestingResult();
  50. const { t } = useTranslate('knowledgeDetails');
  51. const { pagination, setPagination } = useGetPaginationWithRouter();
  52. const isSuccess = useSelectIsTestingSuccess();
  53. const onChange: PaginationProps['onChange'] = (pageNumber, pageSize) => {
  54. pagination.onChange?.(pageNumber, pageSize);
  55. handleTesting(selectedDocumentIds);
  56. };
  57. const onTesting = useCallback(
  58. (ids: string[]) => {
  59. setPagination({ page: 1 });
  60. handleTesting(ids);
  61. },
  62. [setPagination, handleTesting],
  63. );
  64. return (
  65. <section className={styles.testingResultWrapper}>
  66. <Collapse
  67. expandIcon={() => (
  68. <SelectedFilesCollapseIcon></SelectedFilesCollapseIcon>
  69. )}
  70. className={styles.selectFilesCollapse}
  71. items={[
  72. {
  73. key: '1',
  74. label: (
  75. <Flex
  76. justify={'space-between'}
  77. align="center"
  78. className={styles.selectFilesTitle}
  79. >
  80. <Space>
  81. <span>
  82. {selectedDocumentIds?.length ?? 0}/{documents?.length ?? 0}
  83. </span>
  84. {t('filesSelected')}
  85. </Space>
  86. </Flex>
  87. ),
  88. children: (
  89. <div>
  90. <SelectFiles
  91. setSelectedDocumentIds={setSelectedDocumentIds}
  92. handleTesting={onTesting}
  93. ></SelectFiles>
  94. </div>
  95. ),
  96. },
  97. ]}
  98. />
  99. <Flex
  100. gap={'large'}
  101. vertical
  102. flex={1}
  103. className={styles.selectFilesCollapse}
  104. >
  105. {isSuccess && chunks.length > 0 ? (
  106. chunks?.map((x) => (
  107. <Card key={x.chunk_id} title={<ChunkTitle item={x}></ChunkTitle>}>
  108. <Flex gap={'middle'}>
  109. {x.img_id && (
  110. <Popover
  111. placement="left"
  112. content={
  113. <Image
  114. id={x.img_id}
  115. className={styles.imagePreview}
  116. ></Image>
  117. }
  118. >
  119. <Image id={x.img_id} className={styles.image}></Image>
  120. </Popover>
  121. )}
  122. <div>{x.content_with_weight}</div>
  123. </Flex>
  124. </Card>
  125. ))
  126. ) : isSuccess && chunks.length === 0 ? (
  127. <Empty></Empty>
  128. ) : null}
  129. </Flex>
  130. <Pagination
  131. {...pagination}
  132. size={'small'}
  133. total={total}
  134. onChange={onChange}
  135. />
  136. </section>
  137. );
  138. };
  139. export default TestingResult;