feat(HTTP Request Tool Node): Use DynamicStructuredTool with models supporting it (no-changelog) (#10246)

This commit is contained in:
Eugene
2024-08-07 11:20:17 +02:00
committed by GitHub
parent fa17391dbd
commit a936680768
11 changed files with 382 additions and 26 deletions

View File

@@ -1,17 +1,19 @@
import { NodeConnectionType, NodeOperationError, jsonStringify } from 'n8n-workflow';
import type {
EventNamesAiNodesType,
IDataObject,
IExecuteFunctions,
IWebhookFunctions,
} from 'n8n-workflow';
import { NodeConnectionType, NodeOperationError, jsonStringify } from 'n8n-workflow';
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
import type { BaseOutputParser } from '@langchain/core/output_parsers';
import type { BaseMessage } from '@langchain/core/messages';
import { DynamicTool, type Tool } from '@langchain/core/tools';
import type { Tool } from '@langchain/core/tools';
import type { BaseLLM } from '@langchain/core/language_models/llms';
import type { BaseChatMemory } from 'langchain/memory';
import type { BaseChatMessageHistory } from '@langchain/core/chat_history';
import { N8nTool } from './N8nTool';
import { DynamicTool } from '@langchain/core/tools';
function hasMethods<T>(obj: unknown, ...methodNames: Array<string | symbol>): obj is T {
return methodNames.every(
@@ -178,7 +180,11 @@ export function serializeChatHistory(chatHistory: BaseMessage[]): string {
.join('\n');
}
export const getConnectedTools = async (ctx: IExecuteFunctions, enforceUniqueNames: boolean) => {
export const getConnectedTools = async (
ctx: IExecuteFunctions,
enforceUniqueNames: boolean,
convertStructuredTool: boolean = true,
) => {
const connectedTools =
((await ctx.getInputConnectionData(NodeConnectionType.AiTool, 0)) as Tool[]) || [];
@@ -186,8 +192,10 @@ export const getConnectedTools = async (ctx: IExecuteFunctions, enforceUniqueNam
const seenNames = new Set<string>();
const finalTools = [];
for (const tool of connectedTools) {
if (!(tool instanceof DynamicTool)) continue;
if (!(tool instanceof DynamicTool) && !(tool instanceof N8nTool)) continue;
const { name } = tool;
if (seenNames.has(name)) {
@@ -197,7 +205,13 @@ export const getConnectedTools = async (ctx: IExecuteFunctions, enforceUniqueNam
);
}
seenNames.add(name);
if (convertStructuredTool && tool instanceof N8nTool) {
finalTools.push(tool.asDynamicTool());
} else {
finalTools.push(tool);
}
}
return connectedTools;
return finalTools;
};