From cc57a7e27ef28d29f3d23ab331d9faf680034e36 Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Tue, 15 Aug 2023 09:55:00 +0800 Subject: [PATCH] perf: response tag;feat: history quote --- .../src/components/ChatBox/ResponseTags.tsx | 2 +- client/src/components/ChatBox/index.tsx | 2 ++ .../account/components/PayRecordTable.tsx | 19 ++++++++++--------- client/src/pages/api/chat/chatTest.ts | 8 +++----- .../pages/api/openapi/v1/chat/getHistory.ts | 5 ++++- .../components/AdEdit/components/ChatTest.tsx | 6 +++--- .../app/detail/components/BasicEdit/index.tsx | 6 +++--- .../src/service/moduleDispatch/chat/oneapi.ts | 14 ++++++++++++-- .../src/service/moduleDispatch/kb/search.ts | 9 +-------- 9 files changed, 39 insertions(+), 32 deletions(-) diff --git a/client/src/components/ChatBox/ResponseTags.tsx b/client/src/components/ChatBox/ResponseTags.tsx index b5e2de128..722f4d217 100644 --- a/client/src/components/ChatBox/ResponseTags.tsx +++ b/client/src/components/ChatBox/ResponseTags.tsx @@ -51,7 +51,7 @@ const ResponseTags = ({ bg: 'transparent' }; - return ( + return responseData.length === 0 ? null : ( {quoteList.length > 0 && ( diff --git a/client/src/components/ChatBox/index.tsx b/client/src/components/ChatBox/index.tsx index 5582c50f9..204510d6c 100644 --- a/client/src/components/ChatBox/index.tsx +++ b/client/src/components/ChatBox/index.tsx @@ -54,6 +54,7 @@ import styles from './index.module.scss'; const textareaMinH = '22px'; type generatingMessageProps = { text?: string; name?: string; status?: 'running' | 'finish' }; export type StartChatFnProps = { + chatList: ChatSiteItemType[]; messages: MessageItemType[]; controller: AbortController; variables: Record; @@ -311,6 +312,7 @@ const ChatBox = ( const messages = adaptChatItem_openAI({ messages: newChatList, reserveId: true }); const { responseData } = await onStartChat({ + chatList: newChatList, messages, controller: abortSignal, generatingMessage, diff --git a/client/src/pages/account/components/PayRecordTable.tsx b/client/src/pages/account/components/PayRecordTable.tsx index 4d5aed3bb..34a4f1f3e 100644 --- a/client/src/pages/account/components/PayRecordTable.tsx +++ b/client/src/pages/account/components/PayRecordTable.tsx @@ -26,6 +26,12 @@ const PayRecordTable = () => { const [payOrders, setPayOrders] = useState([]); const { toast } = useToast(); + const { isInitialLoading, refetch } = useQuery(['initPayOrder'], getPayOrders, { + onSuccess(res) { + setPayOrders(res); + } + }); + const handleRefreshPayOrder = useCallback( async (payId: string) => { setIsLoading(true); @@ -36,8 +42,6 @@ const PayRecordTable = () => { title: data, status: 'success' }); - const res = await getPayOrders(); - setPayOrders(res); } catch (error: any) { toast({ title: error?.message, @@ -45,18 +49,15 @@ const PayRecordTable = () => { }); console.log(error); } + try { + refetch(); + } catch (error) {} setIsLoading(false); }, - [setIsLoading, toast] + [refetch, setIsLoading, toast] ); - const { isInitialLoading } = useQuery(['initPayOrder'], getPayOrders, { - onSuccess(res) { - setPayOrders(res); - } - }); - return ( {!isInitialLoading && payOrders.length === 0 ? ( diff --git a/client/src/pages/api/chat/chatTest.ts b/client/src/pages/api/chat/chatTest.ts index 4817e2257..ce3728ee1 100644 --- a/client/src/pages/api/chat/chatTest.ts +++ b/client/src/pages/api/chat/chatTest.ts @@ -4,16 +4,14 @@ import { authUser } from '@/service/utils/auth'; import { sseErrRes } from '@/service/response'; import { sseResponseEventEnum } from '@/constants/chat'; import { sseResponse } from '@/service/utils/tools'; -import { type ChatCompletionRequestMessage } from 'openai'; import { AppModuleItemType } from '@/types/app'; import { dispatchModules } from '../openapi/v1/chat/completions'; -import { gptMessage2ChatType } from '@/utils/adapt'; import { pushTaskBill } from '@/service/events/pushBill'; import { BillSourceEnum } from '@/constants/user'; +import { ChatItemType } from '@/types/chat'; -export type MessageItemType = ChatCompletionRequestMessage & { _id?: string }; export type Props = { - history: MessageItemType[]; + history: ChatItemType[]; prompt: string; modules: AppModuleItemType[]; variables: Record; @@ -51,7 +49,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) variables, user, params: { - history: gptMessage2ChatType(history), + history, userChatInput: prompt }, stream: true, diff --git a/client/src/pages/api/openapi/v1/chat/getHistory.ts b/client/src/pages/api/openapi/v1/chat/getHistory.ts index f14f3f945..974f2bac7 100644 --- a/client/src/pages/api/openapi/v1/chat/getHistory.ts +++ b/client/src/pages/api/openapi/v1/chat/getHistory.ts @@ -5,6 +5,7 @@ import { authUser } from '@/service/utils/auth'; import { connectToDatabase, Chat } from '@/service/mongo'; import { Types } from 'mongoose'; import type { ChatItemType } from '@/types/chat'; +import { TaskResponseKeyEnum } from '@/constants/chat'; export type Props = { chatId?: string; @@ -55,10 +56,12 @@ export async function getChatHistory({ { $project: { obj: '$content.obj', - value: '$content.value' + value: '$content.value', + [TaskResponseKeyEnum.responseData]: `$content.responseData` } } ]); + console.log(history); return { history }; } diff --git a/client/src/pages/app/detail/components/AdEdit/components/ChatTest.tsx b/client/src/pages/app/detail/components/AdEdit/components/ChatTest.tsx index 92862aece..40a22f1c2 100644 --- a/client/src/pages/app/detail/components/AdEdit/components/ChatTest.tsx +++ b/client/src/pages/app/detail/components/AdEdit/components/ChatTest.tsx @@ -38,19 +38,19 @@ const ChatTest = ( const isOpen = useMemo(() => modules && modules.length > 0, [modules]); const startChat = useCallback( - async ({ messages, controller, generatingMessage, variables }: StartChatFnProps) => { + async ({ chatList, controller, generatingMessage, variables }: StartChatFnProps) => { const historyMaxLen = modules ?.find((item) => item.flowType === FlowModuleTypeEnum.historyNode) ?.inputs?.find((item) => item.key === 'maxContext')?.value || 0; - const history = messages.slice(-historyMaxLen - 2, -2); + const history = chatList.slice(-historyMaxLen - 2, -2); // 流请求,获取数据 const { responseText, responseData } = await streamFetch({ url: '/api/chat/chatTest', data: { history, - prompt: messages[messages.length - 2].content, + prompt: chatList[chatList.length - 2].value, modules, variables, appId: app._id, diff --git a/client/src/pages/app/detail/components/BasicEdit/index.tsx b/client/src/pages/app/detail/components/BasicEdit/index.tsx index 12dd7e68a..6ebeef621 100644 --- a/client/src/pages/app/detail/components/BasicEdit/index.tsx +++ b/client/src/pages/app/detail/components/BasicEdit/index.tsx @@ -572,19 +572,19 @@ const ChatTest = ({ appId }: { appId: string }) => { const [modules, setModules] = useState([]); const startChat = useCallback( - async ({ messages, controller, generatingMessage, variables }: StartChatFnProps) => { + async ({ chatList, controller, generatingMessage, variables }: StartChatFnProps) => { const historyMaxLen = modules ?.find((item) => item.flowType === FlowModuleTypeEnum.historyNode) ?.inputs?.find((item) => item.key === 'maxContext')?.value || 0; - const history = messages.slice(-historyMaxLen - 2, -2); + const history = chatList.slice(-historyMaxLen - 2, -2); // 流请求,获取数据 const { responseText, responseData } = await streamFetch({ url: '/api/chat/chatTest', data: { history, - prompt: messages[messages.length - 2].content, + prompt: chatList[chatList.length - 2].value, modules, variables, appId, diff --git a/client/src/service/moduleDispatch/chat/oneapi.ts b/client/src/service/moduleDispatch/chat/oneapi.ts index ad4b745d6..70cf39442 100644 --- a/client/src/service/moduleDispatch/chat/oneapi.ts +++ b/client/src/service/moduleDispatch/chat/oneapi.ts @@ -63,6 +63,7 @@ export const dispatchChatCompletion = async (props: Record): Promis } const { filterQuoteQA, quotePrompt } = filterQuote({ + history, quoteQA, model: modelConstantsData }); @@ -181,23 +182,32 @@ export const dispatchChatCompletion = async (props: Record): Promis }; function filterQuote({ + history = [], quoteQA = [], model }: { + history: ChatProps['history']; quoteQA: ChatProps['quoteQA']; model: ChatModelItemType; }) { + // concat history quote + const historyQuote = + history[history.length - 1]?.responseData + ?.find((item) => item.moduleName === ChatModuleEnum.AIChat) + ?.quoteList?.filter((item) => !quoteQA.find((quote) => quote.id === item.id)) || []; + const concatQuote = quoteQA.concat(historyQuote.slice(0, 3)); + const sliceResult = modelToolMap.tokenSlice({ model: model.model, maxToken: model.quoteMaxToken, - messages: quoteQA.map((item, i) => ({ + messages: concatQuote.map((item, i) => ({ obj: ChatRoleEnum.System, value: item.a ? `{instruction:${item.q},output:${item.a}}` : `{instruction:${item.q}}` })) }); // slice filterSearch - const filterQuoteQA = quoteQA.slice(0, sliceResult.length); + const filterQuoteQA = concatQuote.slice(0, sliceResult.length); const quotePrompt = filterQuoteQA.length > 0 diff --git a/client/src/service/moduleDispatch/kb/search.ts b/client/src/service/moduleDispatch/kb/search.ts index 8f63ba36c..562e709c7 100644 --- a/client/src/service/moduleDispatch/kb/search.ts +++ b/client/src/service/moduleDispatch/kb/search.ts @@ -9,7 +9,6 @@ import { PgTrainingTableName } from '@/constants/plugin'; type KBSearchProps = { kbList: SelectedKbType; - history: ChatItemType[]; similarity: number; limit: number; userChatInput: string; @@ -22,13 +21,7 @@ export type KBSearchResponse = { }; export async function dispatchKBSearch(props: Record): Promise { - const { - kbList = [], - history = [], - similarity = 0.8, - limit = 5, - userChatInput - } = props as KBSearchProps; + const { kbList = [], similarity = 0.8, limit = 5, userChatInput } = props as KBSearchProps; if (kbList.length === 0) { return Promise.reject("You didn't choose the knowledge base");