mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-09 22:24:05 -08:00
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:
parent
dd40570056
commit
cef7c24b77
|
@ -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<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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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: [
|
||||
{
|
||||
|
|
22
packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/helpers/constants.ts
vendored
Normal file
22
packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/helpers/constants.ts
vendored
Normal 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',
|
||||
];
|
Loading…
Reference in a new issue