first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
+228
@@ -0,0 +1,228 @@
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { VectorStoreQATool } from '@langchain/classic/tools';
|
||||
import {
|
||||
NodeConnectionTypes,
|
||||
type IExecuteFunctions,
|
||||
type INode,
|
||||
type INodeExecutionData,
|
||||
type ISupplyDataFunctions,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { ToolVectorStore } from './ToolVectorStore.node';
|
||||
|
||||
describe('ToolVectorStore', () => {
|
||||
describe('supplyData', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('should read name from node name on version >=1.1', async () => {
|
||||
const node = new ToolVectorStore();
|
||||
|
||||
const supplyDataResult = await node.supplyData.call(
|
||||
mock<ISupplyDataFunctions>({
|
||||
getNode: jest.fn(() => mock<INode>({ typeVersion: 1.2, name: 'test tool' })),
|
||||
getNodeParameter: jest.fn().mockImplementation((paramName, _itemIndex) => {
|
||||
switch (paramName) {
|
||||
case 'name':
|
||||
return 'wrong_field';
|
||||
case 'topK':
|
||||
return 4;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}),
|
||||
getInputConnectionData: jest.fn().mockImplementation(async (inputName, _itemIndex) => {
|
||||
switch (inputName) {
|
||||
case NodeConnectionTypes.AiVectorStore:
|
||||
return jest.fn();
|
||||
case NodeConnectionTypes.AiLanguageModel:
|
||||
return {
|
||||
_modelType: jest.fn(),
|
||||
};
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}),
|
||||
}),
|
||||
0,
|
||||
);
|
||||
|
||||
expect(supplyDataResult.response).toBeInstanceOf(VectorStoreQATool);
|
||||
|
||||
const tool = supplyDataResult.response as VectorStoreQATool;
|
||||
expect(tool.name).toBe('test_tool');
|
||||
expect(tool.description).toContain('test_tool');
|
||||
});
|
||||
|
||||
it('should read name from name parameter on version <1.2', async () => {
|
||||
const node = new ToolVectorStore();
|
||||
|
||||
const supplyDataResult = await node.supplyData.call(
|
||||
mock<ISupplyDataFunctions>({
|
||||
getNode: jest.fn(() => mock<INode>({ typeVersion: 1, name: 'wrong name' })),
|
||||
getNodeParameter: jest.fn().mockImplementation((paramName, _itemIndex) => {
|
||||
switch (paramName) {
|
||||
case 'name':
|
||||
return 'test_tool';
|
||||
case 'topK':
|
||||
return 4;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}),
|
||||
getInputConnectionData: jest.fn().mockImplementation(async (inputName, _itemIndex) => {
|
||||
switch (inputName) {
|
||||
case NodeConnectionTypes.AiVectorStore:
|
||||
return jest.fn();
|
||||
case NodeConnectionTypes.AiLanguageModel:
|
||||
return {
|
||||
_modelType: jest.fn(),
|
||||
};
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}),
|
||||
}),
|
||||
0,
|
||||
);
|
||||
|
||||
expect(supplyDataResult.response).toBeInstanceOf(VectorStoreQATool);
|
||||
|
||||
const tool = supplyDataResult.response as VectorStoreQATool;
|
||||
expect(tool.name).toBe('test_tool');
|
||||
expect(tool.description).toContain('test_tool');
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('should execute vector store tool and return result', async () => {
|
||||
const node = new ToolVectorStore();
|
||||
const inputData: INodeExecutionData[] = [
|
||||
{
|
||||
json: { query: 'test question' },
|
||||
},
|
||||
];
|
||||
|
||||
const mockExecute = mock<IExecuteFunctions>({
|
||||
getInputData: jest.fn(() => inputData),
|
||||
getNode: jest.fn(() => mock<INode>({ typeVersion: 1.2, name: 'test tool' })),
|
||||
getNodeParameter: jest.fn().mockImplementation((paramName, _itemIndex) => {
|
||||
switch (paramName) {
|
||||
case 'description':
|
||||
return 'test description';
|
||||
case 'topK':
|
||||
return 4;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}),
|
||||
getInputConnectionData: jest.fn().mockImplementation(async (inputName, _itemIndex) => {
|
||||
switch (inputName) {
|
||||
case NodeConnectionTypes.AiVectorStore:
|
||||
return jest.fn();
|
||||
case NodeConnectionTypes.AiLanguageModel:
|
||||
return {
|
||||
_modelType: jest.fn(),
|
||||
};
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}),
|
||||
});
|
||||
|
||||
// Mock the VectorStoreQATool.invoke method
|
||||
const mockResult = 'This is the answer from vector store';
|
||||
VectorStoreQATool.prototype.invoke = jest.fn().mockResolvedValue(mockResult);
|
||||
|
||||
const result = await node.execute.call(mockExecute);
|
||||
|
||||
expect(result).toEqual([
|
||||
[
|
||||
{
|
||||
json: {
|
||||
response: mockResult,
|
||||
},
|
||||
pairedItem: {
|
||||
item: 0,
|
||||
},
|
||||
},
|
||||
],
|
||||
]);
|
||||
expect(VectorStoreQATool.prototype.invoke).toHaveBeenCalledWith(inputData[0].json);
|
||||
});
|
||||
|
||||
it('should handle multiple input items', async () => {
|
||||
const node = new ToolVectorStore();
|
||||
const inputData: INodeExecutionData[] = [
|
||||
{
|
||||
json: { query: 'first question' },
|
||||
},
|
||||
{
|
||||
json: { query: 'second question' },
|
||||
},
|
||||
];
|
||||
|
||||
const mockExecute = mock<IExecuteFunctions>({
|
||||
getInputData: jest.fn(() => inputData),
|
||||
getNode: jest.fn(() => mock<INode>({ typeVersion: 1.2, name: 'test tool' })),
|
||||
getNodeParameter: jest.fn().mockImplementation((paramName, _itemIndex) => {
|
||||
switch (paramName) {
|
||||
case 'description':
|
||||
return 'test description';
|
||||
case 'topK':
|
||||
return 4;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}),
|
||||
getInputConnectionData: jest.fn().mockImplementation(async (inputName, _itemIndex) => {
|
||||
switch (inputName) {
|
||||
case NodeConnectionTypes.AiVectorStore:
|
||||
return jest.fn();
|
||||
case NodeConnectionTypes.AiLanguageModel:
|
||||
return {
|
||||
_modelType: jest.fn(),
|
||||
};
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}),
|
||||
});
|
||||
|
||||
// Mock the VectorStoreQATool.invoke method
|
||||
VectorStoreQATool.prototype.invoke = jest
|
||||
.fn()
|
||||
.mockResolvedValueOnce('Answer to first question')
|
||||
.mockResolvedValueOnce('Answer to second question');
|
||||
|
||||
const result = await node.execute.call(mockExecute);
|
||||
|
||||
expect(result).toEqual([
|
||||
[
|
||||
{
|
||||
json: {
|
||||
response: 'Answer to first question',
|
||||
},
|
||||
pairedItem: {
|
||||
item: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
json: {
|
||||
response: 'Answer to second question',
|
||||
},
|
||||
pairedItem: {
|
||||
item: 1,
|
||||
},
|
||||
},
|
||||
],
|
||||
]);
|
||||
expect(VectorStoreQATool.prototype.invoke).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,166 @@
|
||||
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
|
||||
import type { VectorStore } from '@langchain/core/vectorstores';
|
||||
import { VectorDBQAChain } from '@langchain/classic/chains';
|
||||
import { VectorStoreQATool } from '@langchain/classic/tools';
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
ISupplyDataFunctions,
|
||||
SupplyData,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeConnectionTypes, nodeNameToToolName } from 'n8n-workflow';
|
||||
|
||||
import { logWrapper, getConnectionHintNoticeField } from '@n8n/ai-utilities';
|
||||
|
||||
async function getTool(
|
||||
ctx: ISupplyDataFunctions | IExecuteFunctions,
|
||||
itemIndex: number,
|
||||
): Promise<VectorStoreQATool> {
|
||||
const node = ctx.getNode();
|
||||
const { typeVersion } = node;
|
||||
const name =
|
||||
typeVersion <= 1
|
||||
? (ctx.getNodeParameter('name', itemIndex) as string)
|
||||
: nodeNameToToolName(node);
|
||||
const toolDescription = ctx.getNodeParameter('description', itemIndex) as string;
|
||||
const topK = ctx.getNodeParameter('topK', itemIndex, 4) as number;
|
||||
const description = VectorStoreQATool.getDescription(name, toolDescription);
|
||||
const vectorStore = (await ctx.getInputConnectionData(
|
||||
NodeConnectionTypes.AiVectorStore,
|
||||
itemIndex,
|
||||
)) as VectorStore;
|
||||
const llm = (await ctx.getInputConnectionData(
|
||||
NodeConnectionTypes.AiLanguageModel,
|
||||
itemIndex,
|
||||
)) as BaseLanguageModel;
|
||||
|
||||
const vectorStoreTool = new VectorStoreQATool(name, description, {
|
||||
llm,
|
||||
vectorStore,
|
||||
});
|
||||
|
||||
vectorStoreTool.chain = VectorDBQAChain.fromLLM(llm, vectorStore, {
|
||||
k: topK,
|
||||
});
|
||||
|
||||
return vectorStoreTool;
|
||||
}
|
||||
|
||||
export class ToolVectorStore implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Vector Store Question Answer Tool',
|
||||
name: 'toolVectorStore',
|
||||
icon: 'fa:database',
|
||||
iconColor: 'black',
|
||||
group: ['transform'],
|
||||
version: [1, 1.1],
|
||||
description: 'Answer questions with a vector store',
|
||||
defaults: {
|
||||
name: 'Answer questions with a vector store',
|
||||
},
|
||||
codex: {
|
||||
categories: ['AI'],
|
||||
subcategories: {
|
||||
AI: ['Tools'],
|
||||
Tools: ['Other Tools'],
|
||||
},
|
||||
resources: {
|
||||
primaryDocumentation: [
|
||||
{
|
||||
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.toolvectorstore/',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
inputs: [
|
||||
{
|
||||
displayName: 'Vector Store',
|
||||
maxConnections: 1,
|
||||
type: NodeConnectionTypes.AiVectorStore,
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Model',
|
||||
maxConnections: 1,
|
||||
type: NodeConnectionTypes.AiLanguageModel,
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
|
||||
outputs: [NodeConnectionTypes.AiTool],
|
||||
outputNames: ['Tool'],
|
||||
builderHint: {
|
||||
inputs: {
|
||||
ai_vectorStore: { required: true },
|
||||
ai_languageModel: { required: true },
|
||||
},
|
||||
},
|
||||
properties: [
|
||||
getConnectionHintNoticeField([NodeConnectionTypes.AiAgent]),
|
||||
{
|
||||
displayName: 'Data Name',
|
||||
name: 'name',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'e.g. users_info',
|
||||
validateType: 'string-alphanumeric',
|
||||
description:
|
||||
'Name of the data in vector store. This will be used to fill this tool description: Useful for when you need to answer questions about [name]. Whenever you need information about [data description], you should ALWAYS use this. Input should be a fully formed question.',
|
||||
displayOptions: {
|
||||
show: {
|
||||
'@version': [1],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Description of Data',
|
||||
name: 'description',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: "[Describe your data here, e.g. a user's name, email, etc.]",
|
||||
description:
|
||||
'Describe the data in vector store. This will be used to fill this tool description: Useful for when you need to answer questions about [name]. Whenever you need information about [data description], you should ALWAYS use this. Input should be a fully formed question.',
|
||||
typeOptions: {
|
||||
rows: 3,
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Limit',
|
||||
name: 'topK',
|
||||
type: 'number',
|
||||
default: 4,
|
||||
description: 'The maximum number of results to return',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
|
||||
const vectorStoreTool = await getTool(this, itemIndex);
|
||||
|
||||
return {
|
||||
response: logWrapper(vectorStoreTool, this),
|
||||
};
|
||||
}
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const inputData = this.getInputData();
|
||||
const result: INodeExecutionData[] = [];
|
||||
for (let itemIndex = 0; itemIndex < inputData.length; itemIndex++) {
|
||||
const tool = await getTool(this, itemIndex);
|
||||
const outputData = await tool.invoke(inputData[itemIndex].json);
|
||||
result.push({
|
||||
json: {
|
||||
response: outputData,
|
||||
},
|
||||
pairedItem: {
|
||||
item: itemIndex,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return [result];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user