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"
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'), {
loading: () => <p>Loading...</p>,
ssr: false,
});
const { data: historyList } = useRequest(async () => await sendGetRequest('/v1/chat/dialogue/messages/history', {
con_uid: id
}), {
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 (
<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 { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { okaidia } from 'react-syntax-highlighter/dist/esm/styles/prism';
import { useSearchParams } from 'next/navigation';
type Props = {
messages: Message[];
onSubmit: (message: string, otherQueryBody?: any) => Promise<any>;
initialMessage?: string;
readOnly?: boolean;
paramsList?: { [key: string]: string };
clearIntialMessage?: () => void;
@ -25,12 +25,13 @@ const Schema = z.object({ query: z.string().min(1) });
const ChatBoxComp = ({
messages,
onSubmit,
initialMessage,
readOnly,
paramsList,
clearIntialMessage
}: Props) => {
const { mode } = useColorScheme();
const searchParams = useSearchParams();
const initMessage = searchParams.get('initMessage');
const scrollableRef = React.useRef<HTMLDivElement>(null);
const [isLoading, setIsLoading] = useState(false);
const [currentParam, setCurrentParam] = useState<string | undefined | null>();
@ -42,6 +43,7 @@ const ChatBoxComp = ({
const submit = async ({ query }: z.infer<typeof Schema>) => {
try {
console.log('submit');
setIsLoading(true);
methods.reset();
await onSubmit(query, {
@ -55,10 +57,11 @@ const ChatBoxComp = ({
const handleInitMessage = async () => {
try {
const searchParams = new URLSearchParams(window.location.search);
searchParams.delete('initMessage');
window.history.replaceState(null, null, `?${searchParams.toString()}`);
await submit({ query: (initialMessage as string) });
const searchParamsTemp = new URLSearchParams(window.location.search);
const initMessage = searchParamsTemp.get('initMessage');
searchParamsTemp.delete('initMessage');
window.history.replaceState(null, null, `?${searchParamsTemp.toString()}`);
await submit({ query: (initMessage as string) });
} catch (err) {
console.log(err);
} finally {
@ -83,12 +86,12 @@ const ChatBoxComp = ({
scrollableRef.current.scrollTo(0, scrollableRef.current.scrollHeight);
}, [messages?.length]);
React.useEffect(() => {
if (initialMessage && messages.length <= 0) {
if (initMessage && messages.length <= 0) {
handleInitMessage();
}
}, [initialMessage]);
}, [initMessage, messages.length]);
React.useEffect(() => {
if (paramsList && Object.keys(paramsList || {})?.length > 0) {

View File

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