2023-11-29 03:13:55 -08:00
|
|
|
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
|
|
|
|
import {
|
|
|
|
NodeConnectionType,
|
|
|
|
type INodeType,
|
|
|
|
type INodeTypeDescription,
|
2024-10-28 03:37:23 -07:00
|
|
|
type ISupplyDataFunctions,
|
2023-11-29 03:13:55 -08:00
|
|
|
type SupplyData,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import { ContextualCompressionRetriever } from 'langchain/retrievers/contextual_compression';
|
|
|
|
import { LLMChainExtractor } from 'langchain/retrievers/document_compressors/chain_extract';
|
2024-03-07 02:36:36 -08:00
|
|
|
import type { BaseLanguageModel } from '@langchain/core/language_models/base';
|
|
|
|
import type { BaseRetriever } from '@langchain/core/retrievers';
|
2023-11-29 03:13:55 -08:00
|
|
|
|
|
|
|
import { logWrapper } from '../../../utils/logWrapper';
|
|
|
|
|
|
|
|
export class RetrieverContextualCompression implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Contextual Compression Retriever',
|
|
|
|
name: 'retrieverContextualCompression',
|
|
|
|
icon: 'fa:box-open',
|
|
|
|
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/',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node
|
|
|
|
inputs: [
|
|
|
|
{
|
|
|
|
displayName: 'Model',
|
|
|
|
maxConnections: 1,
|
|
|
|
type: NodeConnectionType.AiLanguageModel,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Retriever',
|
|
|
|
maxConnections: 1,
|
|
|
|
type: NodeConnectionType.AiRetriever,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
outputs: [
|
|
|
|
{
|
|
|
|
displayName: 'Retriever',
|
|
|
|
maxConnections: 1,
|
|
|
|
type: NodeConnectionType.AiRetriever,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [],
|
|
|
|
};
|
|
|
|
|
2024-10-28 03:37:23 -07:00
|
|
|
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
|
2024-08-28 00:32:53 -07:00
|
|
|
this.logger.debug('Supplying data for Contextual Compression Retriever');
|
2023-11-29 03:13:55 -08:00
|
|
|
|
|
|
|
const model = (await this.getInputConnectionData(
|
|
|
|
NodeConnectionType.AiLanguageModel,
|
|
|
|
itemIndex,
|
|
|
|
)) as BaseLanguageModel;
|
|
|
|
|
|
|
|
const baseRetriever = (await this.getInputConnectionData(
|
|
|
|
NodeConnectionType.AiRetriever,
|
|
|
|
itemIndex,
|
|
|
|
)) as BaseRetriever;
|
|
|
|
|
|
|
|
const baseCompressor = LLMChainExtractor.fromLLM(model);
|
|
|
|
|
|
|
|
const retriever = new ContextualCompressionRetriever({
|
|
|
|
baseCompressor,
|
|
|
|
baseRetriever,
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
response: logWrapper(retriever, this),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|