fix: OpenAI node text > message hide tools connector for unsupported models (#8866)

Signed-off-by: Oleg Ivaniv <me@olegivaniv.com>
Co-authored-by: Oleg Ivaniv <me@olegivaniv.com>
This commit is contained in:
Michael Kret
2024-03-12 17:23:00 +02:00
committed by GitHub
parent dd40570056
commit cef7c24b77
3 changed files with 66 additions and 7 deletions

View File

@@ -4,12 +4,14 @@ import type {
INodeExecutionData, INodeExecutionData,
IDataObject, IDataObject,
} from 'n8n-workflow'; } 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 { apiRequest } from '../../transport';
import type { ChatCompletion } from '../../helpers/interfaces'; import type { ChatCompletion } from '../../helpers/interfaces';
import { formatToOpenAIAssistantTool } from '../../helpers/utils'; import { formatToOpenAIAssistantTool } from '../../helpers/utils';
import { modelRLC } from '../descriptions'; import { modelRLC } from '../descriptions';
import { getConnectedTools } from '../../../../../utils/helpers'; import { getConnectedTools } from '../../../../../utils/helpers';
import { MODELS_NOT_SUPPORT_FUNCTION_CALLS } from '../../helpers/constants';
const properties: INodeProperties[] = [ const properties: INodeProperties[] = [
modelRLC, 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.', '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, 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', displayName: 'Connect your own custom n8n tools to this node on the canvas',
name: 'noticeTools', name: 'noticeTools',
type: 'notice', type: 'notice',
default: '', default: '',
displayOptions: {
hide: {
hideTools: ['hide'],
},
},
}, },
{ {
displayName: 'Options', displayName: 'Options',
@@ -193,8 +212,15 @@ export async function execute(this: IExecuteFunctions, i: number): Promise<INode
]; ];
} }
const externalTools = await getConnectedTools(this, nodeVersion > 1); const hideTools = this.getNodeParameter('hideTools', i, '') as string;
let tools; let tools;
let externalTools: Tool[] = [];
if (hideTools !== 'hide') {
const enforceUniqueNames = nodeVersion > 1;
externalTools = await getConnectedTools(this, enforceUniqueNames);
}
if (externalTools.length) { if (externalTools.length) {
tools = externalTools.length ? externalTools?.map(formatToOpenAIAssistantTool) : undefined; tools = externalTools.length ? externalTools?.map(formatToOpenAIAssistantTool) : undefined;
@@ -226,7 +252,9 @@ export async function execute(this: IExecuteFunctions, i: number): Promise<INode
let functionResponse; let functionResponse;
for (const tool of externalTools ?? []) { for (const tool of externalTools ?? []) {
if (tool.name === functionName) { if (tool.name === functionName) {
functionResponse = await tool.invoke(functionArgs); const parsedArgs: { input: string } = jsonParse(functionArgs);
const functionInput = parsedArgs.input ?? functionArgs;
functionResponse = await tool.invoke(functionInput);
} }
} }

View File

@@ -42,8 +42,17 @@ const prettifyOperation = (resource: string, operation: string) => {
return `${capitalize(operation)} ${capitalize(resource)}`; return `${capitalize(operation)} ${capitalize(resource)}`;
}; };
const configureNodeInputs = (resource: string, operation: string) => { const configureNodeInputs = (resource: string, operation: string, hideTools: string) => {
if (['assistant', 'text'].includes(resource) && operation === 'message') { 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 [ return [
{ type: NodeConnectionType.Main }, { type: NodeConnectionType.Main },
{ type: NodeConnectionType.AiTool, displayName: 'Tools' }, { type: NodeConnectionType.AiTool, displayName: 'Tools' },
@@ -59,7 +68,7 @@ export const versionDescription: INodeTypeDescription = {
name: 'openAi', name: 'openAi',
icon: 'file:openAi.svg', icon: 'file:openAi.svg',
group: ['transform'], group: ['transform'],
version: [1, 1.1], version: [1, 1.1, 1.2],
subtitle: `={{(${prettifyOperation})($parameter.resource, $parameter.operation)}}`, subtitle: `={{(${prettifyOperation})($parameter.resource, $parameter.operation)}}`,
description: 'Message an assistant or GPT, analyze images, generate audio, etc.', description: 'Message an assistant or GPT, analyze images, generate audio, etc.',
defaults: { 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'], outputs: ['main'],
credentials: [ credentials: [
{ {

View File

@@ -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',
];