Files
ragflow/web/src/pages/add-knowledge/components/knowledge-testing/index.tsx
cutiechi 789ae87727 Fix: Prevent Duplicate Retrieval Requests on Knowledge Testing (#8683)
### What problem does this PR solve?

Previously, when testing knowledge retrieval and clicking the test
button, the component would trigger two API requests instead of one.
This led to redundant network calls and inconsistent results being
displayed.

Before:


![image](https://github.com/user-attachments/assets/530d9a97-04f7-4db4-8489-0a7b67c78194)

After:


![image](https://github.com/user-attachments/assets/d17caf18-a6b1-46bc-b077-d81de0a73818)


### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
2025-07-07 13:07:34 +08:00

69 lines
1.8 KiB
TypeScript

import {
useTestChunkAllRetrieval,
useTestChunkRetrieval,
} from '@/hooks/knowledge-hooks';
import { Flex, Form } from 'antd';
import { useMemo, useState } from 'react';
import TestingControl from './testing-control';
import TestingResult from './testing-result';
import styles from './index.less';
const KnowledgeTesting = () => {
const [form] = Form.useForm();
const {
data: retrievalData,
testChunk,
loading: retrievalLoading,
} = useTestChunkRetrieval();
const {
data: allRetrievalData,
testChunkAll,
loading: allRetrievalLoading,
} = useTestChunkAllRetrieval();
const [selectedDocumentIds, setSelectedDocumentIds] = useState<string[]>([]);
const handleTesting = async (documentIds: string[] = []) => {
const values = await form.validateFields();
const params = {
...values,
vector_similarity_weight: 1 - values.vector_similarity_weight,
};
if (Array.isArray(documentIds) && documentIds.length > 0) {
testChunk({
...params,
doc_ids: documentIds,
});
} else {
testChunkAll({
...params,
doc_ids: [],
});
}
};
const testingResult = useMemo(() => {
return selectedDocumentIds.length > 0 ? retrievalData : allRetrievalData;
}, [allRetrievalData, retrievalData, selectedDocumentIds.length]);
return (
<Flex className={styles.testingWrapper} gap={16}>
<TestingControl
form={form}
handleTesting={handleTesting}
selectedDocumentIds={selectedDocumentIds}
></TestingControl>
<TestingResult
data={testingResult}
loading={retrievalLoading || allRetrievalLoading}
handleTesting={handleTesting}
selectedDocumentIds={selectedDocumentIds}
setSelectedDocumentIds={setSelectedDocumentIds}
></TestingResult>
</Flex>
);
};
export default KnowledgeTesting;