feat: merge branch llm_framework

This commit is contained in:
changhuiping.chp 2023-07-03 18:32:22 +08:00
parent b831ee5863
commit 77a708d24e
4 changed files with 56 additions and 71 deletions

View File

@ -1,14 +1,49 @@
"use client" "use client"
import dynamic from 'next/dynamic' import { useRequest } from 'ahooks';
import { sendGetRequest, sendPostRequest } from '@/utils/request';
import useAgentChat from '@/hooks/useAgentChat';
import ChatBoxComp from '@/components/chatBoxTemp';
import { useDialogueContext } from '@/app/context/dialogue';
import { useSearchParams } from 'next/navigation';
const AgentPage = () => {
const searchParams = useSearchParams();
const { refreshDialogList } = useDialogueContext();
const id = searchParams.get('id');
const scene = searchParams.get('scene');
const DynamicWrapper = dynamic(() => import ('@/components/agentPage'), { const { data: historyList } = useRequest(async () => await sendGetRequest('/v1/chat/dialogue/messages/history', {
loading: () => <p>Loading...</p>, con_uid: id
ssr: false, }), {
ready: !!id,
refreshDeps: [id]
});
const { data: paramsList } = useRequest(async () => await sendPostRequest(`/v1/chat/mode/params/list?chat_mode=${scene}`), {
ready: !!scene,
refreshDeps: [scene]
});
const { history, handleChatSubmit } = useAgentChat({
queryAgentURL: `/v1/chat/completions`,
queryBody: {
conv_uid: id,
chat_mode: scene || 'chat_normal',
},
initHistory: historyList?.data
}); });
const DynamicAgentPage = (props: any) => {
return ( return (
<DynamicWrapper {...props} /> <>
<ChatBoxComp
clearIntialMessage={async () => {
await refreshDialogList();
}}
messages={history || []}
onSubmit={handleChatSubmit}
paramsList={paramsList?.data}
/>
</>
) )
} }
export default DynamicAgentPage;
export default AgentPage;

View File

@ -1,53 +0,0 @@
"use client"
import { useRequest } from 'ahooks';
import { sendGetRequest, sendPostRequest } from '@/utils/request';
import useAgentChat from '@/hooks/useAgentChat';
import ChatBoxComp from '@/components/chatBoxTemp';
import { useDialogueContext } from '@/app/context/dialogue';
const AgentPage = (props: {
searchParams: {
id?: string;
scene?: string;
initMessage?: string;
}
}) => {
const { refreshDialogList } = useDialogueContext();
const { data: historyList } = useRequest(async () => await sendGetRequest('/v1/chat/dialogue/messages/history', {
con_uid: props.searchParams?.id
}), {
ready: !!props.searchParams?.id,
refreshDeps: [props.searchParams?.id]
});
const { data: paramsList } = useRequest(async () => await sendPostRequest(`/v1/chat/mode/params/list?chat_mode=${props.searchParams?.scene}`), {
ready: !!props.searchParams?.scene,
refreshDeps: [props.searchParams?.scene]
});
const { history, handleChatSubmit } = useAgentChat({
queryAgentURL: `/v1/chat/completions`,
queryBody: {
conv_uid: props.searchParams?.id,
chat_mode: props.searchParams?.scene || 'chat_normal',
},
initHistory: historyList?.data
});
return (
<>
<ChatBoxComp
initialMessage={historyList?.data ? (historyList?.data?.length <= 0 ? props.searchParams?.initMessage : undefined) : undefined}
clearIntialMessage={async () => {
await refreshDialogList();
}}
messages={history || []}
onSubmit={handleChatSubmit}
paramsList={paramsList?.data}
/>
</>
)
}
export default AgentPage;

View File

@ -10,11 +10,11 @@ import SmartToyOutlinedIcon from '@mui/icons-material/SmartToyOutlined';
import Markdown from 'markdown-to-jsx'; import Markdown from 'markdown-to-jsx';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { okaidia } from 'react-syntax-highlighter/dist/esm/styles/prism'; import { okaidia } from 'react-syntax-highlighter/dist/esm/styles/prism';
import { useSearchParams } from 'next/navigation';
type Props = { type Props = {
messages: Message[]; messages: Message[];
onSubmit: (message: string, otherQueryBody?: any) => Promise<any>; onSubmit: (message: string, otherQueryBody?: any) => Promise<any>;
initialMessage?: string;
readOnly?: boolean; readOnly?: boolean;
paramsList?: { [key: string]: string }; paramsList?: { [key: string]: string };
clearIntialMessage?: () => void; clearIntialMessage?: () => void;
@ -25,12 +25,13 @@ const Schema = z.object({ query: z.string().min(1) });
const ChatBoxComp = ({ const ChatBoxComp = ({
messages, messages,
onSubmit, onSubmit,
initialMessage,
readOnly, readOnly,
paramsList, paramsList,
clearIntialMessage clearIntialMessage
}: Props) => { }: Props) => {
const { mode } = useColorScheme(); const { mode } = useColorScheme();
const searchParams = useSearchParams();
const initMessage = searchParams.get('initMessage');
const scrollableRef = React.useRef<HTMLDivElement>(null); const scrollableRef = React.useRef<HTMLDivElement>(null);
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [currentParam, setCurrentParam] = useState<string | undefined | null>(); const [currentParam, setCurrentParam] = useState<string | undefined | null>();
@ -42,6 +43,7 @@ const ChatBoxComp = ({
const submit = async ({ query }: z.infer<typeof Schema>) => { const submit = async ({ query }: z.infer<typeof Schema>) => {
try { try {
console.log('submit');
setIsLoading(true); setIsLoading(true);
methods.reset(); methods.reset();
await onSubmit(query, { await onSubmit(query, {
@ -55,10 +57,11 @@ const ChatBoxComp = ({
const handleInitMessage = async () => { const handleInitMessage = async () => {
try { try {
const searchParams = new URLSearchParams(window.location.search); const searchParamsTemp = new URLSearchParams(window.location.search);
searchParams.delete('initMessage'); const initMessage = searchParamsTemp.get('initMessage');
window.history.replaceState(null, null, `?${searchParams.toString()}`); searchParamsTemp.delete('initMessage');
await submit({ query: (initialMessage as string) }); window.history.replaceState(null, null, `?${searchParamsTemp.toString()}`);
await submit({ query: (initMessage as string) });
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} finally { } finally {
@ -85,10 +88,10 @@ const ChatBoxComp = ({
}, [messages?.length]); }, [messages?.length]);
React.useEffect(() => { React.useEffect(() => {
if (initialMessage && messages.length <= 0) { if (initMessage && messages.length <= 0) {
handleInitMessage(); handleInitMessage();
} }
}, [initialMessage]); }, [initMessage, messages.length]);
React.useEffect(() => { React.useEffect(() => {
if (paramsList && Object.keys(paramsList || {})?.length > 0) { if (paramsList && Object.keys(paramsList || {})?.length > 0) {

View File

@ -49,7 +49,7 @@ import { useDialogueContext } from '@/app/context/dialogue';
const ctrl = new AbortController(); const ctrl = new AbortController();
let buffer = ''; let buffer = '';
await fetchEventSource(`${process.env.API_BASE_URL + queryAgentURL}`, { await fetchEventSource(`${process.env.API_BASE_URL + "/api" + queryAgentURL}`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',