From 044023ee593dcb13c4e6f876e0b0e619f5ea9ecf Mon Sep 17 00:00:00 2001 From: Michael Kret Date: Tue, 12 Mar 2024 15:41:57 +0200 Subject: [PATCH 1/3] :zap: fix --- .../OpenAi/actions/text/message.operation.ts | 28 ++++++++++++++++++- .../OpenAi/actions/versionDescription.ts | 17 ++++++++--- .../nodes/vendors/OpenAi/helpers/constants.ts | 22 +++++++++++++++ 3 files changed, 62 insertions(+), 5 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 d80ac6bdeb8a0..46bec7bea7a19 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 @@ -10,6 +10,8 @@ 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'; +import type { Tool } from '@langchain/core/tools'; 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; diff --git a/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/versionDescription.ts b/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/versionDescription.ts index 4e25b64f2491c..778b42c26d727 100644 --- a/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/versionDescription.ts +++ b/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/versionDescription.ts @@ -42,8 +42,17 @@ const prettifyOperation = (resource: string, operation: string) => { 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 0000000000000..8eacc0fb1592e --- /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', +]; From a0dc9580fdd3123cee5ba8295133ff72de8ecdf8 Mon Sep 17 00:00:00 2001 From: Oleg Ivaniv Date: Tue, 12 Mar 2024 15:31:03 +0100 Subject: [PATCH 2/3] Prase out input from function call Signed-off-by: Oleg Ivaniv --- .../nodes/vendors/OpenAi/actions/text/message.operation.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 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 46bec7bea7a19..8590825d20450 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,7 +4,7 @@ import type { INodeExecutionData, IDataObject, } from 'n8n-workflow'; -import { updateDisplayOptions } from 'n8n-workflow'; +import { jsonParse, updateDisplayOptions } from 'n8n-workflow'; import { apiRequest } from '../../transport'; import type { ChatCompletion } from '../../helpers/interfaces'; import { formatToOpenAIAssistantTool } from '../../helpers/utils'; @@ -252,7 +252,8 @@ export async function execute(this: IExecuteFunctions, i: number): Promise Date: Tue, 12 Mar 2024 15:54:00 +0100 Subject: [PATCH 3/3] Fix linting issues Signed-off-by: Oleg Ivaniv --- .../nodes/vendors/OpenAi/actions/text/message.operation.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 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 8590825d20450..ddcb250d26e93 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 @@ -5,13 +5,13 @@ import type { IDataObject, } 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'; -import type { Tool } from '@langchain/core/tools'; const properties: INodeProperties[] = [ modelRLC, @@ -252,7 +252,8 @@ export async function execute(this: IExecuteFunctions, i: number): Promise