refactor: Add frontend code to DB-GPT (#912)

This commit is contained in:
katakuri
2023-12-11 00:05:42 +08:00
committed by GitHub
parent b8dc9cf11e
commit 43190ca333
189 changed files with 19179 additions and 16 deletions

38
web/hooks/use-summary.ts Normal file
View File

@@ -0,0 +1,38 @@
import { ChatContext } from '@/app/chat-context';
import { ChatHistoryResponse } from '@/types/chat';
import { useCallback, useContext } from 'react';
import useChat from './use-chat';
import { apiInterceptors, getChatHistory } from '@/client/api';
const useSummary = () => {
const { history, setHistory, chatId, model, docId } = useContext(ChatContext);
const chat = useChat({ queryAgentURL: '/knowledge/document/summary' });
const summary = useCallback(
async (curDocId?: number) => {
const [, res] = await apiInterceptors(getChatHistory(chatId));
const tempHistory: ChatHistoryResponse = [
...res!,
{ role: 'human', context: '', model_name: model, order: 0, time_stamp: 0 },
{ role: 'view', context: '', model_name: model, order: 0, time_stamp: 0, retry: true },
];
const index = tempHistory.length - 1;
setHistory([...tempHistory]);
await chat({
data: {
doc_id: curDocId || docId,
model_name: model,
},
chatId,
onMessage: (message) => {
tempHistory[index].context = message;
setHistory([...tempHistory]);
},
});
},
[history, model, docId, chatId],
);
return summary;
};
export default useSummary;