From cef7c24b7779000386e45dae4c3ee25dd2d6251b Mon Sep 17 00:00:00 2001 From: Michael Kret <88898367+michael-radency@users.noreply.github.com> Date: Tue, 12 Mar 2024 17:23:00 +0200 Subject: [PATCH] fix: OpenAI node text > message hide tools connector for unsupported models (#8866) Signed-off-by: Oleg Ivaniv Co-authored-by: Oleg Ivaniv --- .../OpenAi/actions/text/message.operation.ts | 34 +++++++++++++++++-- .../OpenAi/actions/versionDescription.ts | 17 +++++++--- .../nodes/vendors/OpenAi/helpers/constants.ts | 22 ++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/helpers/constants.ts 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 d80ac6bde..ddcb250d2 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 @@ -4,12 +4,14 @@ import type { INodeExecutionData, IDataObject, } from 'n8n-workflow'; -import { updateDisplayOptions } from 'n8n-workflow'; +import { jsonParse, updateDisplayOptions } from 'n8n-workflow'; +import type { Tool } from '@langchain/core/tools'; 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, @@ -83,11 +85,28 @@ const properties: INodeProperties[] = [ 'Whether to attempt to return the response in JSON format. Compatible with GPT-4 Turbo and all GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106.', default: false, }, + { + displayName: 'Hide Tools', + name: 'hideTools', + type: 'hidden', + default: 'hide', + displayOptions: { + show: { + modelId: MODELS_NOT_SUPPORT_FUNCTION_CALLS, + '@version': [{ _cnd: { gte: 1.2 } }], + }, + }, + }, { displayName: 'Connect your own custom n8n tools to this node on the canvas', name: 'noticeTools', type: 'notice', default: '', + displayOptions: { + hide: { + hideTools: ['hide'], + }, + }, }, { displayName: 'Options', @@ -193,8 +212,15 @@ export async function execute(this: IExecuteFunctions, i: number): Promise 1); + const hideTools = this.getNodeParameter('hideTools', i, '') as string; + let tools; + let externalTools: Tool[] = []; + + if (hideTools !== 'hide') { + const enforceUniqueNames = nodeVersion > 1; + externalTools = await getConnectedTools(this, enforceUniqueNames); + } if (externalTools.length) { tools = externalTools.length ? externalTools?.map(formatToOpenAIAssistantTool) : undefined; @@ -226,7 +252,9 @@ export async function execute(this: IExecuteFunctions, i: number): Promise { return `${capitalize(operation)} ${capitalize(resource)}`; }; -const configureNodeInputs = (resource: string, operation: string) => { - if (['assistant', 'text'].includes(resource) && operation === 'message') { +const configureNodeInputs = (resource: string, operation: string, hideTools: string) => { + if (resource === 'assistant' && operation === 'message') { + return [ + { type: NodeConnectionType.Main }, + { type: NodeConnectionType.AiTool, displayName: 'Tools' }, + ]; + } + if (resource === 'text' && operation === 'message') { + if (hideTools === 'hide') { + return [NodeConnectionType.Main]; + } return [ { type: NodeConnectionType.Main }, { type: NodeConnectionType.AiTool, displayName: 'Tools' }, @@ -59,7 +68,7 @@ export const versionDescription: INodeTypeDescription = { name: 'openAi', icon: 'file:openAi.svg', group: ['transform'], - version: [1, 1.1], + version: [1, 1.1, 1.2], subtitle: `={{(${prettifyOperation})($parameter.resource, $parameter.operation)}}`, description: 'Message an assistant or GPT, analyze images, generate audio, etc.', defaults: { @@ -79,7 +88,7 @@ export const versionDescription: INodeTypeDescription = { ], }, }, - inputs: `={{(${configureNodeInputs})($parameter.resource, $parameter.operation)}}`, + inputs: `={{(${configureNodeInputs})($parameter.resource, $parameter.operation, $parameter.hideTools)}}`, outputs: ['main'], credentials: [ { diff --git a/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/helpers/constants.ts b/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/helpers/constants.ts new file mode 100644 index 000000000..8eacc0fb1 --- /dev/null +++ b/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/helpers/constants.ts @@ -0,0 +1,22 @@ +export const MODELS_NOT_SUPPORT_FUNCTION_CALLS = [ + 'gpt-3.5-turbo-16k-0613', + 'dall-e-3', + 'text-embedding-3-large', + 'dall-e-2', + 'whisper-1', + 'tts-1-hd-1106', + 'tts-1-hd', + 'gpt-4-0314', + 'text-embedding-3-small', + 'gpt-4-32k-0314', + 'gpt-3.5-turbo-0301', + 'gpt-4-vision-preview', + 'gpt-3.5-turbo-16k', + 'gpt-3.5-turbo-instruct-0914', + 'tts-1', + 'davinci-002', + 'gpt-3.5-turbo-instruct', + 'babbage-002', + 'tts-1-1106', + 'text-embedding-ada-002', +];