From 5c47a5f691d42dae84a9df8a32a5ea600d83f6dd Mon Sep 17 00:00:00 2001 From: oleg Date: Wed, 11 Sep 2024 15:49:38 +0200 Subject: [PATCH] feat(OpenAI Node): Add Max Tools Iteration parameter and prevent tool calling after execution is aborted (#10735) --- .../OpenAi/actions/text/message.operation.ts | 30 +++++++++++++++++-- .../OpenAi/actions/versionDescription.ts | 2 +- .../vendors/OpenAi/test/OpenAi.node.test.ts | 3 ++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/text/message.operation.ts b/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/text/message.operation.ts index d37be5a06..47be9a9ab 100644 --- a/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/text/message.operation.ts +++ b/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/text/message.operation.ts @@ -6,13 +6,13 @@ import type { } from 'n8n-workflow'; import { jsonParse, updateDisplayOptions } from 'n8n-workflow'; import type { Tool } from '@langchain/core/tools'; +import _omit from 'lodash/omit'; import { apiRequest } from '../../transport'; import type { ChatCompletion } from '../../helpers/interfaces'; import { formatToOpenAIAssistantTool } from '../../helpers/utils'; import { modelRLC } from '../descriptions'; import { getConnectedTools } from '../../../../../utils/helpers'; import { MODELS_NOT_SUPPORT_FUNCTION_CALLS } from '../../helpers/constants'; - const properties: INodeProperties[] = [ modelRLC('modelSearch'), { @@ -170,6 +170,19 @@ const properties: INodeProperties[] = [ 'An alternative to sampling with temperature, controls diversity via nucleus sampling: 0.5 means half of all likelihood-weighted options are considered. We generally recommend altering this or temperature but not both.', type: 'number', }, + { + displayName: 'Max Tool Calls Iterations', + name: 'maxToolsIterations', + type: 'number', + default: 15, + description: + 'The maximum number of tool iteration cycles the LLM will run before stopping. A single iteration can contain multiple tool calls. Set to 0 for no limit.', + displayOptions: { + show: { + '@version': [{ _cnd: { gte: 1.5 } }], + }, + }, + }, ], }, ]; @@ -189,6 +202,10 @@ export async function execute(this: IExecuteFunctions, i: number): Promise= 1.5 ? (this.getNodeParameter('options.maxToolsIterations', i, 15) as number) : 0; + + const abortSignal = this.getExecutionCancelSignal(); if (options.maxTokens !== undefined) { options.max_tokens = options.maxTokens; @@ -231,7 +248,7 @@ export async function execute(this: IExecuteFunctions, i: number): Promise 0 && currentIteration >= maxToolsIterations) + ) { + break; + } messages.push(response.choices[0].message); for (const toolCall of toolCalls) { @@ -274,6 +299,7 @@ export async function execute(this: IExecuteFunctions, i: number): Promise { const nodeParameters = parameters; return { + getExecutionCancelSignal() { + return new AbortController().signal; + }, getNodeParameter(parameter: string) { return get(nodeParameters, parameter); },