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:
+95
@@ -0,0 +1,95 @@
|
||||
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
|
||||
import type { BaseRetriever } from '@langchain/core/retrievers';
|
||||
import { ContextualCompressionRetriever } from '@langchain/classic/retrievers/contextual_compression';
|
||||
import { LLMChainExtractor } from '@langchain/classic/retrievers/document_compressors/chain_extract';
|
||||
import {
|
||||
NodeConnectionTypes,
|
||||
type INodeType,
|
||||
type INodeTypeDescription,
|
||||
type ISupplyDataFunctions,
|
||||
type SupplyData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { logWrapper } from '@n8n/ai-utilities';
|
||||
|
||||
export class RetrieverContextualCompression implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Contextual Compression Retriever',
|
||||
name: 'retrieverContextualCompression',
|
||||
icon: 'fa:box-open',
|
||||
iconColor: 'black',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
description: 'Enhances document similarity search by contextual compression.',
|
||||
defaults: {
|
||||
name: 'Contextual Compression Retriever',
|
||||
},
|
||||
codex: {
|
||||
categories: ['AI'],
|
||||
subcategories: {
|
||||
AI: ['Retrievers'],
|
||||
},
|
||||
resources: {
|
||||
primaryDocumentation: [
|
||||
{
|
||||
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.retrievercontextualcompression/',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
inputs: [
|
||||
{
|
||||
displayName: 'Model',
|
||||
maxConnections: 1,
|
||||
type: NodeConnectionTypes.AiLanguageModel,
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Retriever',
|
||||
maxConnections: 1,
|
||||
type: NodeConnectionTypes.AiRetriever,
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
outputs: [
|
||||
{
|
||||
displayName: 'Retriever',
|
||||
maxConnections: 1,
|
||||
type: NodeConnectionTypes.AiRetriever,
|
||||
},
|
||||
],
|
||||
builderHint: {
|
||||
inputs: {
|
||||
ai_languageModel: { required: true },
|
||||
ai_retriever: { required: true },
|
||||
},
|
||||
},
|
||||
properties: [],
|
||||
};
|
||||
|
||||
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
|
||||
this.logger.debug('Supplying data for Contextual Compression Retriever');
|
||||
|
||||
const model = (await this.getInputConnectionData(
|
||||
NodeConnectionTypes.AiLanguageModel,
|
||||
itemIndex,
|
||||
)) as BaseLanguageModel;
|
||||
|
||||
const baseRetriever = (await this.getInputConnectionData(
|
||||
NodeConnectionTypes.AiRetriever,
|
||||
itemIndex,
|
||||
)) as BaseRetriever;
|
||||
|
||||
const baseCompressor = LLMChainExtractor.fromLLM(model);
|
||||
|
||||
const retriever = new ContextualCompressionRetriever({
|
||||
baseCompressor,
|
||||
baseRetriever,
|
||||
});
|
||||
|
||||
return {
|
||||
response: logWrapper(retriever, this),
|
||||
};
|
||||
}
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
|
||||
import type { BaseRetriever } from '@langchain/core/retrievers';
|
||||
import { MultiQueryRetriever } from '@langchain/classic/retrievers/multi_query';
|
||||
import {
|
||||
NodeConnectionTypes,
|
||||
type INodeType,
|
||||
type INodeTypeDescription,
|
||||
type ISupplyDataFunctions,
|
||||
type SupplyData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { logWrapper } from '@n8n/ai-utilities';
|
||||
|
||||
export class RetrieverMultiQuery implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'MultiQuery Retriever',
|
||||
name: 'retrieverMultiQuery',
|
||||
icon: 'fa:box-open',
|
||||
iconColor: 'black',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
description:
|
||||
'Automates prompt tuning, generates diverse queries and expands document pool for enhanced retrieval.',
|
||||
defaults: {
|
||||
name: 'MultiQuery Retriever',
|
||||
},
|
||||
codex: {
|
||||
categories: ['AI'],
|
||||
subcategories: {
|
||||
AI: ['Retrievers'],
|
||||
},
|
||||
resources: {
|
||||
primaryDocumentation: [
|
||||
{
|
||||
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.retrievermultiquery/',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
inputs: [
|
||||
{
|
||||
displayName: 'Model',
|
||||
maxConnections: 1,
|
||||
type: NodeConnectionTypes.AiLanguageModel,
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Retriever',
|
||||
maxConnections: 1,
|
||||
type: NodeConnectionTypes.AiRetriever,
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
outputs: [
|
||||
{
|
||||
displayName: 'Retriever',
|
||||
maxConnections: 1,
|
||||
type: NodeConnectionTypes.AiRetriever,
|
||||
},
|
||||
],
|
||||
builderHint: {
|
||||
inputs: {
|
||||
ai_languageModel: { required: true },
|
||||
ai_retriever: { required: true },
|
||||
},
|
||||
},
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
placeholder: 'Add Option',
|
||||
description: 'Additional options to add',
|
||||
type: 'collection',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Query Count',
|
||||
name: 'queryCount',
|
||||
default: 3,
|
||||
typeOptions: { minValue: 1 },
|
||||
description: 'Number of different versions of the given question to generate',
|
||||
type: 'number',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
|
||||
this.logger.debug('Supplying data for MultiQuery Retriever');
|
||||
|
||||
const options = this.getNodeParameter('options', itemIndex, {}) as { queryCount?: number };
|
||||
|
||||
const model = (await this.getInputConnectionData(
|
||||
NodeConnectionTypes.AiLanguageModel,
|
||||
itemIndex,
|
||||
)) as BaseLanguageModel;
|
||||
|
||||
const baseRetriever = (await this.getInputConnectionData(
|
||||
NodeConnectionTypes.AiRetriever,
|
||||
itemIndex,
|
||||
)) as BaseRetriever;
|
||||
|
||||
// TODO: Add support for parserKey
|
||||
|
||||
const retriever = MultiQueryRetriever.fromLLM({
|
||||
llm: model,
|
||||
retriever: baseRetriever,
|
||||
...options,
|
||||
});
|
||||
|
||||
return {
|
||||
response: logWrapper(retriever, this),
|
||||
};
|
||||
}
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
import type { BaseDocumentCompressor } from '@langchain/core/retrievers/document_compressors';
|
||||
import { VectorStore } from '@langchain/core/vectorstores';
|
||||
import { ContextualCompressionRetriever } from '@langchain/classic/retrievers/contextual_compression';
|
||||
import {
|
||||
NodeConnectionTypes,
|
||||
type INodeType,
|
||||
type INodeTypeDescription,
|
||||
type ISupplyDataFunctions,
|
||||
type SupplyData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { logWrapper } from '@n8n/ai-utilities';
|
||||
|
||||
export class RetrieverVectorStore implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Vector Store Retriever',
|
||||
name: 'retrieverVectorStore',
|
||||
icon: 'fa:box-open',
|
||||
iconColor: 'black',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
description: 'Use a Vector Store as Retriever',
|
||||
defaults: {
|
||||
name: 'Vector Store Retriever',
|
||||
},
|
||||
codex: {
|
||||
categories: ['AI'],
|
||||
subcategories: {
|
||||
AI: ['Retrievers'],
|
||||
},
|
||||
resources: {
|
||||
primaryDocumentation: [
|
||||
{
|
||||
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.retrievervectorstore/',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
inputs: [
|
||||
{
|
||||
displayName: 'Vector Store',
|
||||
maxConnections: 1,
|
||||
type: NodeConnectionTypes.AiVectorStore,
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
|
||||
outputs: [NodeConnectionTypes.AiRetriever],
|
||||
outputNames: ['Retriever'],
|
||||
builderHint: {
|
||||
relatedNodes: [
|
||||
{
|
||||
nodeType: '@n8n/n8n-nodes-langchain.vectorStoreInMemory',
|
||||
relationHint: 'Connect to provide vectors for retrieval in RAG workflows',
|
||||
},
|
||||
],
|
||||
inputs: {
|
||||
ai_vectorStore: { required: true },
|
||||
},
|
||||
},
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Limit',
|
||||
name: 'topK',
|
||||
type: 'number',
|
||||
default: 4,
|
||||
description: 'The maximum number of results to return',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
|
||||
this.logger.debug('Supplying data for Vector Store Retriever');
|
||||
|
||||
const topK = this.getNodeParameter('topK', itemIndex, 4) as number;
|
||||
const vectorStore = (await this.getInputConnectionData(
|
||||
NodeConnectionTypes.AiVectorStore,
|
||||
itemIndex,
|
||||
)) as
|
||||
| VectorStore
|
||||
| {
|
||||
reranker: BaseDocumentCompressor;
|
||||
vectorStore: VectorStore;
|
||||
};
|
||||
|
||||
let retriever = null;
|
||||
|
||||
if (vectorStore instanceof VectorStore) {
|
||||
retriever = vectorStore.asRetriever(topK);
|
||||
} else {
|
||||
retriever = new ContextualCompressionRetriever({
|
||||
baseCompressor: vectorStore.reranker,
|
||||
baseRetriever: vectorStore.vectorStore.asRetriever(topK),
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
response: logWrapper(retriever, this),
|
||||
};
|
||||
}
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
import type { BaseDocumentCompressor } from '@langchain/core/retrievers/document_compressors';
|
||||
import { VectorStore } from '@langchain/core/vectorstores';
|
||||
import { ContextualCompressionRetriever } from '@langchain/classic/retrievers/contextual_compression';
|
||||
import type { ISupplyDataFunctions } from 'n8n-workflow';
|
||||
import { NodeConnectionTypes } from 'n8n-workflow';
|
||||
|
||||
import { RetrieverVectorStore } from '../RetrieverVectorStore.node';
|
||||
|
||||
const mockLogger = {
|
||||
debug: jest.fn(),
|
||||
info: jest.fn(),
|
||||
warn: jest.fn(),
|
||||
error: jest.fn(),
|
||||
};
|
||||
|
||||
describe('RetrieverVectorStore', () => {
|
||||
let retrieverNode: RetrieverVectorStore;
|
||||
let mockContext: jest.Mocked<ISupplyDataFunctions>;
|
||||
|
||||
beforeEach(() => {
|
||||
retrieverNode = new RetrieverVectorStore();
|
||||
mockContext = {
|
||||
logger: mockLogger,
|
||||
getNodeParameter: jest.fn(),
|
||||
getInputConnectionData: jest.fn(),
|
||||
} as unknown as jest.Mocked<ISupplyDataFunctions>;
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('supplyData', () => {
|
||||
it('should create a retriever from a basic VectorStore', async () => {
|
||||
const mockVectorStore = Object.create(VectorStore.prototype) as VectorStore;
|
||||
mockVectorStore.asRetriever = jest.fn().mockReturnValue({ test: 'retriever' });
|
||||
|
||||
mockContext.getNodeParameter.mockImplementation((param, _itemIndex, defaultValue) => {
|
||||
if (param === 'topK') return 4;
|
||||
return defaultValue;
|
||||
});
|
||||
|
||||
mockContext.getInputConnectionData.mockResolvedValue(mockVectorStore);
|
||||
|
||||
const result = await retrieverNode.supplyData.call(mockContext, 0);
|
||||
|
||||
expect(mockContext.getInputConnectionData).toHaveBeenCalledWith(
|
||||
NodeConnectionTypes.AiVectorStore,
|
||||
0,
|
||||
);
|
||||
expect(mockVectorStore.asRetriever).toHaveBeenCalledWith(4);
|
||||
expect(result).toHaveProperty('response', { test: 'retriever' });
|
||||
});
|
||||
|
||||
it('should create a retriever with custom topK parameter', async () => {
|
||||
const mockVectorStore = Object.create(VectorStore.prototype) as VectorStore;
|
||||
mockVectorStore.asRetriever = jest.fn().mockReturnValue({ test: 'retriever' });
|
||||
|
||||
mockContext.getNodeParameter.mockImplementation((param, _itemIndex, defaultValue) => {
|
||||
if (param === 'topK') return 10;
|
||||
return defaultValue;
|
||||
});
|
||||
mockContext.getInputConnectionData.mockResolvedValue(mockVectorStore);
|
||||
|
||||
const result = await retrieverNode.supplyData.call(mockContext, 0);
|
||||
|
||||
expect(mockVectorStore.asRetriever).toHaveBeenCalledWith(10);
|
||||
expect(result).toHaveProperty('response', { test: 'retriever' });
|
||||
});
|
||||
|
||||
it('should create a ContextualCompressionRetriever when input contains reranker and vectorStore', async () => {
|
||||
const mockVectorStore = Object.create(VectorStore.prototype) as VectorStore;
|
||||
mockVectorStore.asRetriever = jest.fn().mockReturnValue({ test: 'base-retriever' });
|
||||
|
||||
const mockReranker = {} as BaseDocumentCompressor;
|
||||
|
||||
const inputWithReranker = {
|
||||
reranker: mockReranker,
|
||||
vectorStore: mockVectorStore,
|
||||
};
|
||||
|
||||
mockContext.getNodeParameter.mockImplementation((param, _itemIndex, defaultValue) => {
|
||||
if (param === 'topK') return 4;
|
||||
return defaultValue;
|
||||
});
|
||||
mockContext.getInputConnectionData.mockResolvedValue(inputWithReranker);
|
||||
|
||||
const result = await retrieverNode.supplyData.call(mockContext, 0);
|
||||
|
||||
expect(mockContext.getInputConnectionData).toHaveBeenCalledWith(
|
||||
NodeConnectionTypes.AiVectorStore,
|
||||
0,
|
||||
);
|
||||
expect(mockVectorStore.asRetriever).toHaveBeenCalledWith(4);
|
||||
expect(result.response).toBeInstanceOf(ContextualCompressionRetriever);
|
||||
});
|
||||
|
||||
it('should create a ContextualCompressionRetriever with custom topK when using reranker', async () => {
|
||||
const mockVectorStore = Object.create(VectorStore.prototype) as VectorStore;
|
||||
mockVectorStore.asRetriever = jest.fn().mockReturnValue({ test: 'base-retriever' });
|
||||
|
||||
const mockReranker = {} as BaseDocumentCompressor;
|
||||
|
||||
const inputWithReranker = {
|
||||
reranker: mockReranker,
|
||||
vectorStore: mockVectorStore,
|
||||
};
|
||||
|
||||
mockContext.getNodeParameter.mockImplementation((param, _itemIndex, defaultValue) => {
|
||||
if (param === 'topK') return 8;
|
||||
return defaultValue;
|
||||
});
|
||||
mockContext.getInputConnectionData.mockResolvedValue(inputWithReranker);
|
||||
|
||||
const result = await retrieverNode.supplyData.call(mockContext, 0);
|
||||
|
||||
expect(mockVectorStore.asRetriever).toHaveBeenCalledWith(8);
|
||||
expect(result.response).toBeInstanceOf(ContextualCompressionRetriever);
|
||||
});
|
||||
|
||||
it('should use default topK value when parameter is not provided', async () => {
|
||||
const mockVectorStore = Object.create(VectorStore.prototype) as VectorStore;
|
||||
mockVectorStore.asRetriever = jest.fn().mockReturnValue({ test: 'retriever' });
|
||||
|
||||
mockContext.getNodeParameter.mockImplementation((_param, _itemIndex, defaultValue) => {
|
||||
return defaultValue;
|
||||
});
|
||||
mockContext.getInputConnectionData.mockResolvedValue(mockVectorStore);
|
||||
|
||||
await retrieverNode.supplyData.call(mockContext, 0);
|
||||
|
||||
expect(mockContext.getNodeParameter).toHaveBeenCalledWith('topK', 0, 4);
|
||||
expect(mockVectorStore.asRetriever).toHaveBeenCalledWith(4);
|
||||
});
|
||||
});
|
||||
});
|
||||
+438
@@ -0,0 +1,438 @@
|
||||
import type { CallbackManagerForRetrieverRun } from '@langchain/core/callbacks/manager';
|
||||
import { Document } from '@langchain/core/documents';
|
||||
import { BaseRetriever, type BaseRetrieverInput } from '@langchain/core/retrievers';
|
||||
import type { SetField, SetNodeOptions } from 'n8n-nodes-base/dist/nodes/Set/v2/helpers/interfaces';
|
||||
import * as manual from 'n8n-nodes-base/dist/nodes/Set/v2/manual.mode';
|
||||
import { NodeConnectionTypes, NodeOperationError } from 'n8n-workflow';
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteWorkflowInfo,
|
||||
INodeExecutionData,
|
||||
IWorkflowBase,
|
||||
ISupplyDataFunctions,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
SupplyData,
|
||||
INodeParameterResourceLocator,
|
||||
ExecuteWorkflowData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { logWrapper } from '@n8n/ai-utilities';
|
||||
|
||||
function objectToString(obj: Record<string, string> | IDataObject, level = 0) {
|
||||
let result = '';
|
||||
for (const key in obj) {
|
||||
const value = obj[key];
|
||||
if (typeof value === 'object' && value !== null) {
|
||||
result += `${' '.repeat(level)}- "${key}":\n${objectToString(
|
||||
value as IDataObject,
|
||||
level + 1,
|
||||
)}`;
|
||||
} else {
|
||||
result += `${' '.repeat(level)}- "${key}": "${value}"\n`;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export class RetrieverWorkflow implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Workflow Retriever',
|
||||
name: 'retrieverWorkflow',
|
||||
icon: 'fa:box-open',
|
||||
iconColor: 'black',
|
||||
group: ['transform'],
|
||||
version: [1, 1.1],
|
||||
description: 'Use an n8n Workflow as Retriever',
|
||||
defaults: {
|
||||
name: 'Workflow Retriever',
|
||||
},
|
||||
codex: {
|
||||
categories: ['AI'],
|
||||
subcategories: {
|
||||
AI: ['Retrievers'],
|
||||
},
|
||||
resources: {
|
||||
primaryDocumentation: [
|
||||
{
|
||||
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.retrieverworkflow/',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
inputs: [],
|
||||
outputs: [
|
||||
{
|
||||
displayName: 'Retriever',
|
||||
maxConnections: 1,
|
||||
type: NodeConnectionTypes.AiRetriever,
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName:
|
||||
'The workflow will receive "query" as input and the output of the last node will be returned and converted to Documents',
|
||||
name: 'executeNotice',
|
||||
type: 'notice',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Source',
|
||||
name: 'source',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Database',
|
||||
value: 'database',
|
||||
description: 'Load the workflow from the database by ID',
|
||||
},
|
||||
{
|
||||
name: 'Parameter',
|
||||
value: 'parameter',
|
||||
description: 'Load the workflow from a parameter',
|
||||
},
|
||||
],
|
||||
default: 'database',
|
||||
description: 'Where to get the workflow to execute from',
|
||||
},
|
||||
|
||||
// ----------------------------------
|
||||
// source:database
|
||||
// ----------------------------------
|
||||
{
|
||||
displayName: 'Workflow ID',
|
||||
name: 'workflowId',
|
||||
type: 'string',
|
||||
displayOptions: {
|
||||
show: {
|
||||
source: ['database'],
|
||||
'@version': [{ _cnd: { eq: 1 } }],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
required: true,
|
||||
description: 'The workflow to execute',
|
||||
},
|
||||
{
|
||||
displayName: 'Workflow',
|
||||
name: 'workflowId',
|
||||
type: 'workflowSelector',
|
||||
displayOptions: {
|
||||
show: {
|
||||
source: ['database'],
|
||||
'@version': [{ _cnd: { gte: 1.1 } }],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
required: true,
|
||||
},
|
||||
|
||||
// ----------------------------------
|
||||
// source:parameter
|
||||
// ----------------------------------
|
||||
{
|
||||
displayName: 'Workflow JSON',
|
||||
name: 'workflowJson',
|
||||
type: 'json',
|
||||
typeOptions: {
|
||||
rows: 10,
|
||||
},
|
||||
displayOptions: {
|
||||
show: {
|
||||
source: ['parameter'],
|
||||
},
|
||||
},
|
||||
default: '\n\n\n',
|
||||
required: true,
|
||||
description: 'The workflow JSON code to execute',
|
||||
},
|
||||
|
||||
// ----------------------------------
|
||||
// For all
|
||||
// ----------------------------------
|
||||
{
|
||||
displayName: 'Workflow Values',
|
||||
name: 'fields',
|
||||
placeholder: 'Add Value',
|
||||
type: 'fixedCollection',
|
||||
description: 'Set the values which should be made available in the workflow',
|
||||
typeOptions: {
|
||||
multipleValues: true,
|
||||
sortable: true,
|
||||
},
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
name: 'values',
|
||||
displayName: 'Values',
|
||||
values: [
|
||||
{
|
||||
displayName: 'Name',
|
||||
name: 'name',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'e.g. fieldName',
|
||||
description:
|
||||
'Name of the field to set the value of. Supports dot-notation. Example: data.person[0].name.',
|
||||
requiresDataPath: 'single',
|
||||
},
|
||||
{
|
||||
displayName: 'Type',
|
||||
name: 'type',
|
||||
type: 'options',
|
||||
description: 'The field value type',
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-options-type-unsorted-items
|
||||
options: [
|
||||
{
|
||||
name: 'String',
|
||||
value: 'stringValue',
|
||||
},
|
||||
{
|
||||
name: 'Number',
|
||||
value: 'numberValue',
|
||||
},
|
||||
{
|
||||
name: 'Boolean',
|
||||
value: 'booleanValue',
|
||||
},
|
||||
{
|
||||
name: 'Array',
|
||||
value: 'arrayValue',
|
||||
},
|
||||
{
|
||||
name: 'Object',
|
||||
value: 'objectValue',
|
||||
},
|
||||
],
|
||||
default: 'stringValue',
|
||||
},
|
||||
{
|
||||
displayName: 'Value',
|
||||
name: 'stringValue',
|
||||
type: 'string',
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
type: ['stringValue'],
|
||||
},
|
||||
},
|
||||
validateType: 'string',
|
||||
ignoreValidationDuringExecution: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Value',
|
||||
name: 'numberValue',
|
||||
type: 'string',
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
type: ['numberValue'],
|
||||
},
|
||||
},
|
||||
validateType: 'number',
|
||||
ignoreValidationDuringExecution: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Value',
|
||||
name: 'booleanValue',
|
||||
type: 'options',
|
||||
default: 'true',
|
||||
options: [
|
||||
{
|
||||
name: 'True',
|
||||
value: 'true',
|
||||
},
|
||||
{
|
||||
name: 'False',
|
||||
value: 'false',
|
||||
},
|
||||
],
|
||||
displayOptions: {
|
||||
show: {
|
||||
type: ['booleanValue'],
|
||||
},
|
||||
},
|
||||
validateType: 'boolean',
|
||||
ignoreValidationDuringExecution: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Value',
|
||||
name: 'arrayValue',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'e.g. [ arrayItem1, arrayItem2, arrayItem3 ]',
|
||||
displayOptions: {
|
||||
show: {
|
||||
type: ['arrayValue'],
|
||||
},
|
||||
},
|
||||
validateType: 'array',
|
||||
ignoreValidationDuringExecution: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Value',
|
||||
name: 'objectValue',
|
||||
type: 'json',
|
||||
default: '={}',
|
||||
typeOptions: {
|
||||
rows: 2,
|
||||
},
|
||||
displayOptions: {
|
||||
show: {
|
||||
type: ['objectValue'],
|
||||
},
|
||||
},
|
||||
validateType: 'object',
|
||||
ignoreValidationDuringExecution: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
|
||||
const workflowProxy = this.getWorkflowDataProxy(0);
|
||||
|
||||
class WorkflowRetriever extends BaseRetriever {
|
||||
lc_namespace = ['n8n-nodes-langchain', 'retrievers', 'workflow'];
|
||||
|
||||
constructor(
|
||||
private executeFunctions: ISupplyDataFunctions,
|
||||
fields: BaseRetrieverInput,
|
||||
) {
|
||||
super(fields);
|
||||
}
|
||||
|
||||
async _getRelevantDocuments(
|
||||
query: string,
|
||||
config?: CallbackManagerForRetrieverRun,
|
||||
): Promise<Document[]> {
|
||||
const source = this.executeFunctions.getNodeParameter('source', itemIndex) as string;
|
||||
|
||||
const baseMetadata: IDataObject = {
|
||||
source: 'workflow',
|
||||
workflowSource: source,
|
||||
};
|
||||
|
||||
const workflowInfo: IExecuteWorkflowInfo = {};
|
||||
if (source === 'database') {
|
||||
const nodeVersion = this.executeFunctions.getNode().typeVersion;
|
||||
if (nodeVersion === 1) {
|
||||
workflowInfo.id = this.executeFunctions.getNodeParameter(
|
||||
'workflowId',
|
||||
itemIndex,
|
||||
) as string;
|
||||
} else {
|
||||
const { value } = this.executeFunctions.getNodeParameter(
|
||||
'workflowId',
|
||||
itemIndex,
|
||||
{},
|
||||
) as INodeParameterResourceLocator;
|
||||
workflowInfo.id = value as string;
|
||||
}
|
||||
|
||||
baseMetadata.workflowId = workflowInfo.id;
|
||||
} else if (source === 'parameter') {
|
||||
// Read workflow from parameter
|
||||
const workflowJson = this.executeFunctions.getNodeParameter(
|
||||
'workflowJson',
|
||||
itemIndex,
|
||||
) as string;
|
||||
try {
|
||||
workflowInfo.code = JSON.parse(workflowJson) as IWorkflowBase;
|
||||
} catch (error) {
|
||||
throw new NodeOperationError(
|
||||
this.executeFunctions.getNode(),
|
||||
`The provided workflow is not valid JSON: "${(error as Error).message}"`,
|
||||
{
|
||||
itemIndex,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// same as current workflow
|
||||
baseMetadata.workflowId = workflowProxy.$workflow.id;
|
||||
}
|
||||
|
||||
const rawData: IDataObject = { query };
|
||||
|
||||
const workflowFieldsJson = this.executeFunctions.getNodeParameter(
|
||||
'fields.values',
|
||||
itemIndex,
|
||||
[],
|
||||
{
|
||||
rawExpressions: true,
|
||||
},
|
||||
) as SetField[];
|
||||
|
||||
// Copied from Set Node v2
|
||||
for (const entry of workflowFieldsJson) {
|
||||
if (entry.type === 'objectValue' && (entry.objectValue as string).startsWith('=')) {
|
||||
rawData[entry.name] = (entry.objectValue as string).replace(/^=+/, '');
|
||||
}
|
||||
}
|
||||
|
||||
const options: SetNodeOptions = {
|
||||
include: 'all',
|
||||
};
|
||||
|
||||
const newItem = await manual.execute.call(
|
||||
this.executeFunctions,
|
||||
{ json: { query } },
|
||||
itemIndex,
|
||||
options,
|
||||
rawData,
|
||||
this.executeFunctions.getNode(),
|
||||
);
|
||||
|
||||
const items = [newItem] as INodeExecutionData[];
|
||||
|
||||
let receivedData: ExecuteWorkflowData;
|
||||
try {
|
||||
receivedData = await this.executeFunctions.executeWorkflow(
|
||||
workflowInfo,
|
||||
items,
|
||||
config?.getChild(),
|
||||
{
|
||||
parentExecution: {
|
||||
executionId: workflowProxy.$execution.id,
|
||||
workflowId: workflowProxy.$workflow.id,
|
||||
},
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
// Make sure a valid error gets returned that can by json-serialized else it will
|
||||
// not show up in the frontend
|
||||
throw new NodeOperationError(this.executeFunctions.getNode(), error as Error);
|
||||
}
|
||||
|
||||
const receivedItems = receivedData.data?.[0] ?? [];
|
||||
|
||||
const returnData: Document[] = [];
|
||||
for (const [index, itemData] of receivedItems.entries()) {
|
||||
const pageContent = objectToString(itemData.json);
|
||||
returnData.push(
|
||||
new Document({
|
||||
pageContent: `### ${index + 1}. Context data:\n${pageContent}`,
|
||||
metadata: {
|
||||
...baseMetadata,
|
||||
itemIndex: index,
|
||||
executionId: receivedData.executionId,
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return returnData;
|
||||
}
|
||||
}
|
||||
|
||||
const retriever = new WorkflowRetriever(this, {});
|
||||
|
||||
return {
|
||||
response: logWrapper(retriever, this),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user