feat: Search for the answers you want based on the selected knowledge base #2247 (#2287)

### What problem does this PR solve?

feat: Search for the answers you want based on the selected knowledge
base #2247

### Type of change


- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
balibabu
2024-09-06 15:42:55 +08:00
committed by GitHub
parent b5a2711c05
commit 925dd2aa85
8 changed files with 46 additions and 103 deletions

View File

@@ -1,26 +1,19 @@
import { MessageType } from '@/constants/chat';
import { useTestChunkRetrieval } from '@/hooks/knowledge-hooks';
import { useSendMessageWithSse } from '@/hooks/logic-hooks';
import { IAnswer } from '@/interfaces/database/chat';
import api from '@/utils/api';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { IMessage } from '../chat/interface';
import { isEmpty } from 'lodash';
import { useCallback, useEffect, useState } from 'react';
export const useSendQuestion = (kbIds: string[]) => {
const { send, answer, done } = useSendMessageWithSse(api.ask);
const { testChunk, loading } = useTestChunkRetrieval();
const [sendingLoading, setSendingLoading] = useState(false);
const message: IMessage = useMemo(() => {
return {
id: '',
content: answer.answer,
role: MessageType.Assistant,
reference: answer.reference,
};
}, [answer]);
const [currentAnswer, setCurrentAnswer] = useState({} as IAnswer);
const sendQuestion = useCallback(
(question: string) => {
setCurrentAnswer({} as IAnswer);
setSendingLoading(true);
send({ kb_ids: kbIds, question });
testChunk({ kb_id: kbIds, highlight: true, question });
@@ -28,11 +21,17 @@ export const useSendQuestion = (kbIds: string[]) => {
[send, testChunk, kbIds],
);
useEffect(() => {
if (!isEmpty(answer)) {
setCurrentAnswer(answer);
}
}, [answer]);
useEffect(() => {
if (done) {
setSendingLoading(false);
}
}, [done]);
return { sendQuestion, message, loading, sendingLoading };
return { sendQuestion, loading, sendingLoading, answer: currentAnswer };
};