From 89ab17ea2eca62afcf827a1a65ea9d7f3da134a5 Mon Sep 17 00:00:00 2001 From: Archer <545436317@qq.com> Date: Fri, 26 Apr 2024 10:54:39 +0800 Subject: [PATCH] perf: tool value type and complections body size (#1291) --- .../dispatch/agent/runTool/functionCall.ts | 16 +++++++-- .../dispatch/agent/runTool/toolChoice.ts | 33 +++++++++++++++++-- .../core/workflow/dispatch/tools/http468.ts | 3 +- .../src/pages/api/common/file/uploadImage.ts | 1 + 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/packages/service/core/workflow/dispatch/agent/runTool/functionCall.ts b/packages/service/core/workflow/dispatch/agent/runTool/functionCall.ts index e0d21a298ec4..fc5f42e5d377 100644 --- a/packages/service/core/workflow/dispatch/agent/runTool/functionCall.ts +++ b/packages/service/core/workflow/dispatch/agent/runTool/functionCall.ts @@ -56,7 +56,7 @@ export const runToolWithFunctionCall = async ( > = {}; item.toolParams.forEach((item) => { properties[item.key] = { - type: 'string', + type: item.valueType || 'string', description: item.toolDescription || '' }; }); @@ -76,6 +76,18 @@ export const runToolWithFunctionCall = async ( messages, maxTokens: toolModel.maxContext - 500 // filter token. not response maxToken }); + const formativeMessages = filterMessages.map((item) => { + if (item.role === ChatCompletionRequestMessageRoleEnum.Assistant && item.function_call) { + return { + ...item, + function_call: { + name: item.function_call?.name, + arguments: item.function_call?.arguments + } + }; + } + return item; + }); /* Run llm */ const ai = getAIApi({ @@ -87,7 +99,7 @@ export const runToolWithFunctionCall = async ( model: toolModel.model, temperature: 0, stream, - messages: filterMessages, + messages: formativeMessages, functions, function_call: 'auto' }, diff --git a/packages/service/core/workflow/dispatch/agent/runTool/toolChoice.ts b/packages/service/core/workflow/dispatch/agent/runTool/toolChoice.ts index 86203eeaa243..f3f619abd113 100644 --- a/packages/service/core/workflow/dispatch/agent/runTool/toolChoice.ts +++ b/packages/service/core/workflow/dispatch/agent/runTool/toolChoice.ts @@ -63,7 +63,7 @@ export const runToolWithToolChoice = async ( > = {}; item.toolParams.forEach((item) => { properties[item.key] = { - type: 'string', + type: item.valueType || 'string', description: item.toolDescription || '' }; }); @@ -86,7 +86,34 @@ export const runToolWithToolChoice = async ( messages, maxTokens: toolModel.maxContext - 300 // filter token. not response maxToken }); - + const formativeMessages = filterMessages.map((item) => { + if (item.role === 'assistant' && item.tool_calls) { + return { + ...item, + tool_calls: item.tool_calls.map((tool) => ({ + id: tool.id, + type: tool.type, + function: tool.function + })) + }; + } + return item; + }); + // console.log( + // JSON.stringify( + // { + // ...toolModel?.defaultConfig, + // model: toolModel.model, + // temperature: 0, + // stream, + // messages: formativeMessages, + // tools, + // tool_choice: 'auto' + // }, + // null, + // 2 + // ) + // ); /* Run llm */ const ai = getAIApi({ timeout: 480000 @@ -97,7 +124,7 @@ export const runToolWithToolChoice = async ( model: toolModel.model, temperature: 0, stream, - messages: filterMessages, + messages: formativeMessages, tools, tool_choice: 'auto' }, diff --git a/packages/service/core/workflow/dispatch/tools/http468.ts b/packages/service/core/workflow/dispatch/tools/http468.ts index 7deb9606f161..a06e5856b575 100644 --- a/packages/service/core/workflow/dispatch/tools/http468.ts +++ b/packages/service/core/workflow/dispatch/tools/http468.ts @@ -149,7 +149,8 @@ export const dispatchHttp468Request = async (props: HttpRequestProps): Promise 0 ? headers : undefined, httpResult: rawResponse }, - [DispatchNodeResponseKeyEnum.toolResponses]: results, + [DispatchNodeResponseKeyEnum.toolResponses]: + Object.keys(results).length > 0 ? results : rawResponse, [NodeOutputKeyEnum.httpRawResponse]: rawResponse, ...results }; diff --git a/projects/app/src/pages/api/common/file/uploadImage.ts b/projects/app/src/pages/api/common/file/uploadImage.ts index 637b70adaf31..e88ac33cc77f 100644 --- a/projects/app/src/pages/api/common/file/uploadImage.ts +++ b/projects/app/src/pages/api/common/file/uploadImage.ts @@ -28,6 +28,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) export const config = { api: { + sizeLimit: '10mb', bodyParser: { sizeLimit: '16mb' }