-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
burivuhster
merged 14 commits into
master
from
ai-249-tools-agent-with-openai-chat-model
Aug 7, 2024
Merged
feat(HTTP Request Tool Node): Use DynamicStructureTool with models supporting it (no-changelog) #10246
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1fa6a72
wip: use HTTP tool as DynamicStructuredTool wherever possible
burivuhster 26c3728
wip: use HTTP tool as DynamicStructuredTool wherever possible
burivuhster 337d3c8
Merge branch 'ai-249-tools-agent-with-openai-chat-model' of https://g…
burivuhster 1886764
wip: Fix zod schema for a JSON parameter
burivuhster fb7826b
Merge branch 'master' into ai-249-tools-agent-with-openai-chat-model
burivuhster ca9a78c
Merge branch 'master' into ai-249-tools-agent-with-openai-chat-model
burivuhster 7305386
wip: using fallback tool description for older models
burivuhster 377897e
wip: wrap HTTP Tool into N8nTool, add tests
burivuhster ca4dd3f
wip: rename test file for consistency
burivuhster f67b7e9
wip: fix type errors
burivuhster bc0ce8a
Merge branch 'master' into ai-249-tools-agent-with-openai-chat-model
burivuhster aac2726
wip: fix type errors
burivuhster 06d5536
wip: fix type issues
burivuhster 6e3c52c
wip: add minor node version
burivuhster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@burivuhster
yes, exactly