File size: 3,456 Bytes
f9e19e4 3927930 d4bff6a 3927930 17c457d e68f488 3927930 d4bff6a f9e19e4 e68f488 0dad3f5 3927930 0dad3f5 d4bff6a 3927930 0dad3f5 f9e19e4 0dad3f5 f9e19e4 0dad3f5 3927930 f9e19e4 3927930 e68f488 fa5e9f6 e68f488 fa5e9f6 e68f488 d4bff6a e68f488 d4bff6a 3927930 f9e19e4 0dad3f5 fa5e9f6 f9e19e4 e68f488 0dad3f5 f9e19e4 3927930 17c457d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
import { useFetchMindMap, useFetchRelatedQuestions } from '@/hooks/chat-hooks';
import { useTestChunkRetrieval } from '@/hooks/knowledge-hooks';
import { useSendMessageWithSse } from '@/hooks/logic-hooks';
import { IAnswer } from '@/interfaces/database/chat';
import api from '@/utils/api';
import { get, isEmpty, trim } from 'lodash';
import { ChangeEventHandler, 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 [currentAnswer, setCurrentAnswer] = useState({} as IAnswer);
const { fetchRelatedQuestions, data: relatedQuestions } =
useFetchRelatedQuestions();
const {
fetchMindMap,
data: mindMap,
loading: mindMapLoading,
} = useFetchMindMap();
const [searchStr, setSearchStr] = useState<string>('');
const [isFirstRender, setIsFirstRender] = useState(true);
const sendQuestion = useCallback(
(question: string) => {
const q = trim(question);
if (isEmpty(q)) return;
setIsFirstRender(false);
setCurrentAnswer({} as IAnswer);
setSendingLoading(true);
send({ kb_ids: kbIds, question: q });
testChunk({ kb_id: kbIds, highlight: true, question: q });
fetchMindMap({
question: q,
kb_ids: kbIds,
});
fetchRelatedQuestions(q);
},
[send, testChunk, kbIds, fetchRelatedQuestions, fetchMindMap],
);
const handleSearchStrChange: ChangeEventHandler<HTMLInputElement> =
useCallback((e) => {
setSearchStr(e.target.value);
}, []);
const handleClickRelatedQuestion = useCallback(
(question: string) => () => {
if (sendingLoading) return;
setSearchStr(question);
sendQuestion(question);
},
[sendQuestion, sendingLoading],
);
const handleTestChunk = useCallback(
(documentIds: string[]) => {
const q = trim(searchStr);
if (sendingLoading || isEmpty(q)) return;
testChunk({
kb_id: kbIds,
highlight: true,
question: q,
doc_ids: Array.isArray(documentIds) ? documentIds : [],
});
},
[sendingLoading, searchStr, kbIds, testChunk],
);
useEffect(() => {
if (!isEmpty(answer)) {
setCurrentAnswer(answer);
}
return () => {
setCurrentAnswer({} as IAnswer);
};
}, [answer]);
useEffect(() => {
if (done) {
setSendingLoading(false);
}
}, [done]);
return {
sendQuestion,
handleSearchStrChange,
handleClickRelatedQuestion,
handleTestChunk,
loading,
sendingLoading,
answer: currentAnswer,
relatedQuestions: relatedQuestions?.slice(0, 5) ?? [],
mindMap,
mindMapLoading,
searchStr,
isFirstRender,
};
};
export const useFetchBackgroundImage = () => {
const [imgUrl, setImgUrl] = useState<string>('');
const fetchImage = useCallback(async () => {
try {
const res = await fetch(
'/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN',
);
const ret = await res.json();
const url = get(ret, 'images.0.url');
if (url) {
setImgUrl(url);
}
} catch (error) {
console.log('🚀 ~ fetchImage ~ error:', error);
}
}, []);
useEffect(() => {
fetchImage();
}, [fetchImage]);
return `https://cn.bing.com${imgUrl}`;
};
|