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

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

Merged
merged 14 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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 @@ -38,7 +38,7 @@ export async function openAiFunctionsAgentExecute(
const memory = (await this.getInputConnectionData(NodeConnectionType.AiMemory, 0)) as
| BaseChatMemory
| undefined;
const tools = await getConnectedTools(this, nodeVersion >= 1.5);
const tools = await getConnectedTools(this, nodeVersion >= 1.5, false);
const outputParsers = await getOptionalOutputParsers(this);
const options = this.getNodeParameter('options', 0, {}) as {
systemMessage?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export async function toolsAgentExecute(this: IExecuteFunctions): Promise<INodeE
| BaseChatMemory
| undefined;

const tools = (await getConnectedTools(this, true)) as Array<DynamicStructuredTool | Tool>;
const tools = (await getConnectedTools(this, true, false)) as Array<DynamicStructuredTool | Tool>;
const outputParser = (await getOptionalOutputParsers(this))?.[0];
let structuredOutputParserTool: DynamicStructuredTool | undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export class OpenAiAssistant implements INodeType {

async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const nodeVersion = this.getNode().typeVersion;
const tools = await getConnectedTools(this, nodeVersion > 1);
const tools = await getConnectedTools(this, nodeVersion > 1, false);
const credentials = await this.getCredentials('openAiApi');

const items = this.getInputData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import type {
} from 'n8n-workflow';
import { NodeConnectionType, NodeOperationError, tryToParseAlphanumericString } from 'n8n-workflow';

import { DynamicTool } from '@langchain/core/tools';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';

import { N8nTool } from '../../../utils/N8nTool';
import {
configureHttpRequestFunction,
configureResponseOptimizer,
extractParametersFromText,
prepareToolDescription,
configureToolFunction,
updateParametersAndOptions,
makeToolInputSchema,
} from './utils';

import {
Expand Down Expand Up @@ -394,9 +394,14 @@ export class ToolHttpRequest implements INodeType {
optimizeResponse,
);

const description = prepareToolDescription(toolDescription, toolParameters);
const schema = makeToolInputSchema(toolParameters);

const tool = new DynamicTool({ name, description, func });
const tool = new N8nTool(this, {
name,
description: toolDescription,
func,
schema,
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure that after update behavior would be same? if not I would suggest to put those changes under new minor version

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the node version?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@burivuhster
yes, exactly

return {
response: tool,
Expand Down
67 changes: 55 additions & 12 deletions packages/@n8n/nodes-langchain/nodes/tools/ToolHttpRequest/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import type {
SendIn,
ToolParameter,
} from './interfaces';
import type { DynamicZodObject } from '../../../types/zod.types';
import { z } from 'zod';

const genericCredentialRequest = async (ctx: IExecuteFunctions, itemIndex: number) => {
const genericType = ctx.getNodeParameter('genericAuthType', itemIndex) as string;
Expand Down Expand Up @@ -566,7 +568,7 @@ export const configureToolFunction = (
httpRequest: (options: IHttpRequestOptions) => Promise<any>,
optimizeResponse: (response: string) => string,
) => {
return async (query: string): Promise<string> => {
return async (query: string | IDataObject): Promise<string> => {
const { index } = ctx.addInputData(NodeConnectionType.AiTool, [[{ json: { query } }]]);

let response: string = '';
Expand All @@ -581,18 +583,22 @@ export const configureToolFunction = (
if (query) {
let dataFromModel;

try {
dataFromModel = jsonParse<IDataObject>(query);
} catch (error) {
if (toolParameters.length === 1) {
dataFromModel = { [toolParameters[0].name]: query };
} else {
throw new NodeOperationError(
ctx.getNode(),
`Input is not a valid JSON: ${error.message}`,
{ itemIndex },
);
if (typeof query === 'string') {
try {
dataFromModel = jsonParse<IDataObject>(query);
} catch (error) {
if (toolParameters.length === 1) {
dataFromModel = { [toolParameters[0].name]: query };
} else {
throw new NodeOperationError(
ctx.getNode(),
`Input is not a valid JSON: ${error.message}`,
{ itemIndex },
);
}
}
} else {
dataFromModel = query;
}

for (const parameter of toolParameters) {
Expand Down Expand Up @@ -727,6 +733,8 @@ export const configureToolFunction = (
}
}
} catch (error) {
console.error(error);

const errorMessage = 'Input provided by model is not valid';

if (error instanceof NodeOperationError) {
Expand Down Expand Up @@ -765,3 +773,38 @@ export const configureToolFunction = (
return response;
};
};

function makeParameterZodSchema(parameter: ToolParameter) {
let schema: z.ZodTypeAny;

if (parameter.type === 'string') {
schema = z.string();
} else if (parameter.type === 'number') {
schema = z.number();
} else if (parameter.type === 'boolean') {
schema = z.boolean();
} else if (parameter.type === 'json') {
schema = z.record(z.any());
} else {
schema = z.string();
}

if (!parameter.required) {
schema = schema.optional();
}

if (parameter.description) {
schema = schema.describe(parameter.description);
}

return schema;
}

export function makeToolInputSchema(parameters: ToolParameter[]): DynamicZodObject {
const schemaEntries = parameters.map((parameter) => [
parameter.name,
makeParameterZodSchema(parameter),
]);

return z.object(Object.fromEntries(schemaEntries));
}
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ export class ToolWorkflow implements INodeType {
if (useSchema) {
try {
// We initialize these even though one of them will always be empty
// it makes it easer to navigate the ternary operator
// it makes it easier to navigate the ternary operator
const jsonExample = this.getNodeParameter('jsonSchemaExample', itemIndex, '') as string;
const inputSchema = this.getNodeParameter('inputSchema', itemIndex, '') as string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export async function execute(this: IExecuteFunctions, i: number): Promise<INode

const agent = new OpenAIAssistantRunnable({ assistantId, client, asAgent: true });

const tools = await getConnectedTools(this, nodeVersion > 1);
const tools = await getConnectedTools(this, nodeVersion > 1, false);
let assistantTools;

if (tools.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export async function execute(this: IExecuteFunctions, i: number): Promise<INode

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

if (externalTools.length) {
Expand Down
176 changes: 176 additions & 0 deletions packages/@n8n/nodes-langchain/utils/N8nTool.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import { N8nTool } from './N8nTool';
import { createMockExecuteFunction } from 'n8n-nodes-base/test/nodes/Helpers';
import { z } from 'zod';
import type { INode } from 'n8n-workflow';
import { DynamicStructuredTool, DynamicTool } from '@langchain/core/tools';

const mockNode: INode = {
id: '1',
name: 'Mock node',
typeVersion: 2,
type: 'n8n-nodes-base.mock',
position: [60, 760],
parameters: {
operation: 'test',
},
};

describe('Test N8nTool wrapper as DynamicStructuredTool', () => {
it('should wrap a tool', () => {
const func = jest.fn();

const ctx = createMockExecuteFunction({}, mockNode);

const tool = new N8nTool(ctx, {
name: 'Dummy Tool',
description: 'A dummy tool for testing',
fallbackDescription: 'A fallback description of the dummy tool',
func,
schema: z.object({
foo: z.string(),
}),
});

expect(tool).toBeInstanceOf(DynamicStructuredTool);
});
});

describe('Test N8nTool wrapper - DynamicTool fallback', () => {
it('should convert the tool to a dynamic tool', () => {
const func = jest.fn();

const ctx = createMockExecuteFunction({}, mockNode);

const tool = new N8nTool(ctx, {
name: 'Dummy Tool',
description: 'A dummy tool for testing',
fallbackDescription: 'A fallback description of the dummy tool',
func,
schema: z.object({
foo: z.string(),
}),
});

const dynamicTool = tool.asDynamicTool();

expect(dynamicTool).toBeInstanceOf(DynamicTool);
});

it('should format fallback description correctly', () => {
const func = jest.fn();

const ctx = createMockExecuteFunction({}, mockNode);

const tool = new N8nTool(ctx, {
name: 'Dummy Tool',
description: 'A dummy tool for testing',
fallbackDescription: 'A fallback description of the dummy tool',
func,
schema: z.object({
foo: z.string(),
bar: z.number().optional(),
qwe: z.boolean().describe('Boolean description'),
}),
});

const dynamicTool = tool.asDynamicTool();

expect(dynamicTool.description).toContain('foo: (description: , type: string, required: true)');
expect(dynamicTool.description).toContain(
'bar: (description: , type: number, required: false)',
);

expect(dynamicTool.description).toContain(
'qwe: (description: Boolean description, type: boolean, required: true)',
);
});

it('should handle empty parameter list correctly', () => {
const func = jest.fn();

const ctx = createMockExecuteFunction({}, mockNode);

const tool = new N8nTool(ctx, {
name: 'Dummy Tool',
description: 'A dummy tool for testing',
fallbackDescription: 'A fallback description of the dummy tool',
func,
schema: z.object({}),
});

const dynamicTool = tool.asDynamicTool();

expect(dynamicTool.description).toEqual('A dummy tool for testing');
});

it('should parse correct parameters', async () => {
const func = jest.fn();

const ctx = createMockExecuteFunction({}, mockNode);

const tool = new N8nTool(ctx, {
name: 'Dummy Tool',
description: 'A dummy tool for testing',
fallbackDescription: 'A fallback description of the dummy tool',
func,
schema: z.object({
foo: z.string().describe('Foo description'),
bar: z.number().optional(),
}),
});

const dynamicTool = tool.asDynamicTool();

const testParameters = { foo: 'some value' };

await dynamicTool.func(JSON.stringify(testParameters));

expect(func).toHaveBeenCalledWith(testParameters);
});

it('should recover when 1 parameter is passed directly', async () => {
const func = jest.fn();

const ctx = createMockExecuteFunction({}, mockNode);

const tool = new N8nTool(ctx, {
name: 'Dummy Tool',
description: 'A dummy tool for testing',
fallbackDescription: 'A fallback description of the dummy tool',
func,
schema: z.object({
foo: z.string().describe('Foo description'),
}),
});

const dynamicTool = tool.asDynamicTool();

const testParameter = 'some value';

await dynamicTool.func(testParameter);

expect(func).toHaveBeenCalledWith({ foo: testParameter });
});

it('should recover when JS object is passed instead of JSON', async () => {
const func = jest.fn();

const ctx = createMockExecuteFunction({}, mockNode);

const tool = new N8nTool(ctx, {
name: 'Dummy Tool',
description: 'A dummy tool for testing',
fallbackDescription: 'A fallback description of the dummy tool',
func,
schema: z.object({
foo: z.string().describe('Foo description'),
}),
});

const dynamicTool = tool.asDynamicTool();

await dynamicTool.func('{ foo: "some value" }');

expect(func).toHaveBeenCalledWith({ foo: 'some value' });
});
});
Loading
Loading