From 1dea2b71b471011a65ca8f1183f8ebb6d329598d Mon Sep 17 00:00:00 2001 From: Archer <545436317@qq.com> Date: Sun, 25 May 2025 20:55:29 +0800 Subject: [PATCH] perf: human check;perf: recursion get node response (#4888) * perf: human check * version * perf: recursion get node response --- .../zh-cn/docs/development/upgrading/4910.md | 4 +++- packages/service/core/chat/utils.ts | 4 ++-- projects/app/package.json | 2 +- .../ChatBox/components/ContextModal.tsx | 13 ++-------- projects/app/src/global/core/chat/utils.ts | 24 ++++++++++--------- .../service/support/permission/auth/chat.ts | 20 +++------------- 6 files changed, 24 insertions(+), 43 deletions(-) diff --git a/docSite/content/zh-cn/docs/development/upgrading/4910.md b/docSite/content/zh-cn/docs/development/upgrading/4910.md index adba6d854..bcde2f46c 100644 --- a/docSite/content/zh-cn/docs/development/upgrading/4910.md +++ b/docSite/content/zh-cn/docs/development/upgrading/4910.md @@ -22,10 +22,12 @@ weight: 790 3. 纠正原先知识库的“表格数据集”名称,改成“备份导入”。同时支持知识库索引的导出和导入。 4. 工作流知识库引用上限,如果工作流中没有相关 AI 节点,则交互模式改成纯手动输入,并且上限为 1000万。 5. 语音输入,移动端判断逻辑,准确判断是否为手机,而不是小屏。 +6. 优化上下文截取算法,至少保证留下一组 Human 信息。 ## 🐛 修复 1. 全文检索多知识库时排序得分排序不正确。 2. 流响应捕获 finish_reason 可能不正确。 3. 工具调用模式,未保存思考输出。 -4. 知识库 indexSize 参数未生效。 \ No newline at end of file +4. 知识库 indexSize 参数未生效。 +5. 工作流嵌套 2 层后,获取预览引用、上下文不正确。 \ No newline at end of file diff --git a/packages/service/core/chat/utils.ts b/packages/service/core/chat/utils.ts index 39e183822..0d22134be 100644 --- a/packages/service/core/chat/utils.ts +++ b/packages/service/core/chat/utils.ts @@ -65,8 +65,8 @@ export const filterGPTMessageByMaxContext = async ({ if (lastMessage.role === ChatCompletionRequestMessageRoleEnum.User) { const tokens = await countGptMessagesTokens([lastMessage, ...tmpChats]); maxContext -= tokens; - // 该轮信息整体 tokens 超出范围,这段数据不要了 - if (maxContext < 0) { + // 该轮信息整体 tokens 超出范围,这段数据不要了。但是至少保证一组。 + if (maxContext < 0 && chats.length > 0) { break; } diff --git a/projects/app/package.json b/projects/app/package.json index 7750c422b..d0bb7b2d5 100644 --- a/projects/app/package.json +++ b/projects/app/package.json @@ -1,6 +1,6 @@ { "name": "app", - "version": "4.9.9", + "version": "4.9.10", "private": false, "scripts": { "dev": "next dev", diff --git a/projects/app/src/components/core/chat/ChatContainer/ChatBox/components/ContextModal.tsx b/projects/app/src/components/core/chat/ChatContainer/ChatBox/components/ContextModal.tsx index dd624907f..a045d1aec 100644 --- a/projects/app/src/components/core/chat/ChatContainer/ChatBox/components/ContextModal.tsx +++ b/projects/app/src/components/core/chat/ChatContainer/ChatBox/components/ContextModal.tsx @@ -7,6 +7,7 @@ import { type ChatHistoryItemResType } from '@fastgpt/global/core/chat/type'; import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant'; import { useRequest2 } from '@fastgpt/web/hooks/useRequest'; import { useTranslation } from 'next-i18next'; +import { getFlatAppResponses } from '@/global/core/chat/utils'; const isLLMNode = (item: ChatHistoryItemResType) => item.moduleType === FlowNodeTypeEnum.chatNode || item.moduleType === FlowNodeTypeEnum.tools; @@ -16,17 +17,7 @@ const ContextModal = ({ onClose, dataId }: { onClose: () => void; dataId: string const { loading: isLoading, data: contextModalData } = useRequest2( () => getHistoryResponseData({ dataId }).then((res) => { - const flatResData: ChatHistoryItemResType[] = - res - ?.map((item) => { - return [ - item, - ...(item.pluginDetail || []), - ...(item.toolDetail || []), - ...(item.loopDetail || []) - ]; - }) - .flat() || []; + const flatResData = getFlatAppResponses(res || []); return flatResData.find(isLLMNode)?.historyPreview || []; }), { manual: false } diff --git a/projects/app/src/global/core/chat/utils.ts b/projects/app/src/global/core/chat/utils.ts index 3972098e7..fc2e0f882 100644 --- a/projects/app/src/global/core/chat/utils.ts +++ b/projects/app/src/global/core/chat/utils.ts @@ -19,23 +19,25 @@ export function transformPreviewHistories( }); } +export const getFlatAppResponses = (res: ChatHistoryItemResType[]): ChatHistoryItemResType[] => { + return res + .map((item) => { + return [ + item, + ...getFlatAppResponses(item.pluginDetail || []), + ...getFlatAppResponses(item.toolDetail || []), + ...getFlatAppResponses(item.loopDetail || []) + ]; + }) + .flat(); +}; export function addStatisticalDataToHistoryItem(historyItem: ChatItemType) { if (historyItem.obj !== ChatRoleEnum.AI) return historyItem; if (historyItem.totalQuoteList !== undefined) return historyItem; if (!historyItem.responseData) return historyItem; // Flat children - const flatResData: ChatHistoryItemResType[] = - historyItem.responseData - ?.map((item) => { - return [ - item, - ...(item.pluginDetail || []), - ...(item.toolDetail || []), - ...(item.loopDetail || []) - ]; - }) - .flat() || []; + const flatResData = getFlatAppResponses(historyItem.responseData || []); return { ...historyItem, diff --git a/projects/app/src/service/support/permission/auth/chat.ts b/projects/app/src/service/support/permission/auth/chat.ts index 275db837a..d8d76ec7f 100644 --- a/projects/app/src/service/support/permission/auth/chat.ts +++ b/projects/app/src/service/support/permission/auth/chat.ts @@ -1,8 +1,4 @@ -import { - type AIChatItemType, - type ChatHistoryItemResType, - type ChatSchema -} from '@fastgpt/global/core/chat/type'; +import { type ChatHistoryItemResType, type ChatSchema } from '@fastgpt/global/core/chat/type'; import { MongoChat } from '@fastgpt/service/core/chat/chatSchema'; import { type AuthModeType } from '@fastgpt/service/support/permission/type'; import { authOutLink } from './outLink'; @@ -12,6 +8,7 @@ import { AuthUserTypeEnum, ReadPermissionVal } from '@fastgpt/global/support/per import { authApp } from '@fastgpt/service/support/permission/app/auth'; import { MongoChatItem } from '@fastgpt/service/core/chat/chatItemSchema'; import { DatasetErrEnum } from '@fastgpt/global/common/error/code/dataset'; +import { getFlatAppResponses } from '@/global/core/chat/utils'; /* 检查chat的权限: @@ -221,18 +218,7 @@ export const authCollectionInChat = async ({ if (!chatItem) return Promise.reject(DatasetErrEnum.unAuthDatasetCollection); // 找 responseData 里,是否有该文档 id - const responseData = chatItem.responseData || []; - const flatResData: ChatHistoryItemResType[] = - responseData - ?.map((item) => { - return [ - item, - ...(item.pluginDetail || []), - ...(item.toolDetail || []), - ...(item.loopDetail || []) - ]; - }) - .flat() || []; + const flatResData = getFlatAppResponses(chatItem.responseData || []); const quoteListSet = new Set( flatResData