Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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 externalTools: Tool[] = [];

if (hideTools !== 'hide') {
const enforceUniqueNames = nodeVersion > 1;
externalTools = await getConnectedTools(this, enforceUniqueNames);
}

if (externalTools.length) {
tools = externalTools.length ? externalTools?.map(formatToOpenAIAssistantTool) : undefined;
Expand Down Expand Up @@ -226,7 +252,9 @@ export async function execute(this: IExecuteFunctions, i: number): Promise<INode
let functionResponse;
for (const tool of externalTools ?? []) {
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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: {
Expand All @@ -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: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
];
Loading