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

88
web/hooks/use-chat.ts Normal file
View File

@@ -0,0 +1,88 @@
import { EventStreamContentType, fetchEventSource } from '@microsoft/fetch-event-source';
import { message } from 'antd';
import { useCallback, useEffect, useMemo } from 'react';
import i18n from '@/app/i18n';
type Props = {
queryAgentURL?: string;
};
type ChatParams = {
chatId: string;
data?: Record<string, any>;
onMessage: (message: string) => void;
onClose?: () => void;
onDone?: () => void;
onError?: (content: string, error?: Error) => void;
};
const useChat = ({ queryAgentURL = '/api/v1/chat/completions' }: Props) => {
const ctrl = useMemo(() => new AbortController(), []);
const chat = useCallback(
async ({ data, chatId, onMessage, onClose, onDone, onError }: ChatParams) => {
if (!data?.user_input && !data?.doc_id) {
message.warning(i18n.t('NoContextTip'));
return;
}
const parmas = {
...data,
conv_uid: chatId,
};
if (!parmas.conv_uid) {
message.error('conv_uid 不存在,请刷新后重试');
return;
}
try {
await fetchEventSource(`${process.env.API_BASE_URL ?? ''}${queryAgentURL}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(parmas),
signal: ctrl.signal,
openWhenHidden: true,
async onopen(response) {
if (response.ok && response.headers.get('content-type') === EventStreamContentType) {
return;
}
},
onclose() {
ctrl.abort();
onClose?.();
},
onerror(err) {
throw new Error(err);
},
onmessage: (event) => {
const message = event.data?.replaceAll('\\n', '\n');
if (message === '[DONE]') {
onDone?.();
} else if (message?.startsWith('[ERROR]')) {
onError?.(message?.replace('[ERROR]', ''));
} else {
onMessage?.(message);
}
},
});
} catch (err) {
ctrl.abort();
onError?.('Sorry, We meet some error, please try agin later.', err as Error);
}
},
[queryAgentURL],
);
useEffect(() => {
return () => {
ctrl.abort();
};
}, []);
return chat;
};
export default useChat;

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;