UNPKG

@n8n/n8n-nodes-langchain

Version:

![Banner image](https://user-images.githubusercontent.com/10284570/173569848-c624317f-42b1-45a6-ab09-f0ea3c247648.png)

139 lines 5.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToolVectorStore = void 0; const chains_1 = require("@langchain/classic/chains"); const tools_1 = require("@langchain/classic/tools"); const n8n_workflow_1 = require("n8n-workflow"); const ai_utilities_1 = require("@n8n/ai-utilities"); async function getTool(ctx, itemIndex) { const node = ctx.getNode(); const { typeVersion } = node; const name = typeVersion <= 1 ? ctx.getNodeParameter('name', itemIndex) : (0, n8n_workflow_1.nodeNameToToolName)(node); const toolDescription = ctx.getNodeParameter('description', itemIndex); const topK = ctx.getNodeParameter('topK', itemIndex, 4); const description = tools_1.VectorStoreQATool.getDescription(name, toolDescription); const vectorStore = (await ctx.getInputConnectionData(n8n_workflow_1.NodeConnectionTypes.AiVectorStore, itemIndex)); const llm = (await ctx.getInputConnectionData(n8n_workflow_1.NodeConnectionTypes.AiLanguageModel, itemIndex)); const vectorStoreTool = new tools_1.VectorStoreQATool(name, description, { llm, vectorStore, }); vectorStoreTool.chain = chains_1.VectorDBQAChain.fromLLM(llm, vectorStore, { k: topK, }); return vectorStoreTool; } class ToolVectorStore { constructor() { this.description = { displayName: 'Vector Store Question Answer Tool', name: 'toolVectorStore', icon: 'node:vector-store-question-answer-tool', 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: n8n_workflow_1.NodeConnectionTypes.AiVectorStore, required: true, }, { displayName: 'Model', maxConnections: 1, type: n8n_workflow_1.NodeConnectionTypes.AiLanguageModel, required: true, }, ], outputs: [n8n_workflow_1.NodeConnectionTypes.AiTool], outputNames: ['Tool'], builderHint: { inputs: { ai_vectorStore: { required: true }, ai_languageModel: { required: true }, }, }, properties: [ (0, ai_utilities_1.getConnectionHintNoticeField)([n8n_workflow_1.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(itemIndex) { const vectorStoreTool = await getTool(this, itemIndex); return { response: (0, ai_utilities_1.logWrapper)(vectorStoreTool, this), }; } async execute() { const inputData = this.getInputData(); const result = []; 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]; } } exports.ToolVectorStore = ToolVectorStore; //# sourceMappingURL=ToolVectorStore.node.js.map