forked from n8n-io/n8n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Vector Store Tool Node): Add Vector Store Tool (n8n-io#9865)
Signed-off-by: Oleg Ivaniv <[email protected]>
- Loading branch information
1 parent
54c2b2f
commit df2bc84
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
packages/@n8n/nodes-langchain/nodes/tools/ToolVectorStore/ToolVectorStore.node.ts
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,113 @@ | ||
import type { IExecuteFunctions, INodeType, INodeTypeDescription, SupplyData } from 'n8n-workflow'; | ||
import { NodeConnectionType } from 'n8n-workflow'; | ||
|
||
import { VectorStoreQATool } from 'langchain/tools'; | ||
import type { VectorStore } from '@langchain/core/vectorstores'; | ||
import type { BaseLanguageModel } from '@langchain/core/language_models/base'; | ||
import { VectorDBQAChain } from 'langchain/chains'; | ||
import { getConnectionHintNoticeField } from '../../../utils/sharedFields'; | ||
import { logWrapper } from '../../../utils/logWrapper'; | ||
|
||
export class ToolVectorStore implements INodeType { | ||
description: INodeTypeDescription = { | ||
displayName: 'Vector Store Tool', | ||
name: 'toolVectorStore', | ||
icon: 'fa:database', | ||
group: ['transform'], | ||
version: [1], | ||
description: 'Retrieve context from vector store', | ||
defaults: { | ||
name: 'Vector Store Tool', | ||
}, | ||
codex: { | ||
categories: ['AI'], | ||
subcategories: { | ||
AI: ['Tools'], | ||
}, | ||
resources: { | ||
primaryDocumentation: [ | ||
{ | ||
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.toolvectorstore/', | ||
}, | ||
], | ||
}, | ||
}, | ||
// eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node | ||
inputs: [ | ||
{ | ||
displayName: 'Vector Store', | ||
maxConnections: 1, | ||
type: NodeConnectionType.AiVectorStore, | ||
required: true, | ||
}, | ||
{ | ||
displayName: 'Model', | ||
maxConnections: 1, | ||
type: NodeConnectionType.AiLanguageModel, | ||
required: true, | ||
}, | ||
], | ||
// eslint-disable-next-line n8n-nodes-base/node-class-description-outputs-wrong | ||
outputs: [NodeConnectionType.AiTool], | ||
outputNames: ['Tool'], | ||
properties: [ | ||
getConnectionHintNoticeField([NodeConnectionType.AiAgent]), | ||
{ | ||
displayName: 'Name', | ||
name: 'name', | ||
type: 'string', | ||
default: '', | ||
placeholder: 'e.g. state_of_union_address', | ||
validateType: 'string-alphanumeric', | ||
description: 'Name of the vector store', | ||
}, | ||
{ | ||
displayName: 'Description', | ||
name: 'description', | ||
type: 'string', | ||
default: '', | ||
placeholder: 'The most recent state of the Union address', | ||
typeOptions: { | ||
rows: 3, | ||
}, | ||
}, | ||
{ | ||
displayName: 'Limit', | ||
name: 'topK', | ||
type: 'number', | ||
default: 4, | ||
description: 'The maximum number of results to return', | ||
}, | ||
], | ||
}; | ||
|
||
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> { | ||
const name = this.getNodeParameter('name', itemIndex) as string; | ||
const toolDescription = this.getNodeParameter('description', itemIndex) as string; | ||
const topK = this.getNodeParameter('topK', itemIndex, 4) as number; | ||
|
||
const vectorStore = (await this.getInputConnectionData( | ||
NodeConnectionType.AiVectorStore, | ||
itemIndex, | ||
)) as VectorStore; | ||
|
||
const llm = (await this.getInputConnectionData( | ||
NodeConnectionType.AiLanguageModel, | ||
0, | ||
)) as BaseLanguageModel; | ||
|
||
const description = VectorStoreQATool.getDescription(name, toolDescription); | ||
const vectorStoreTool = new VectorStoreQATool(name, description, { | ||
llm, | ||
vectorStore, | ||
}); | ||
|
||
vectorStoreTool.chain = VectorDBQAChain.fromLLM(llm, vectorStore, { | ||
k: topK, | ||
}); | ||
|
||
return { | ||
response: logWrapper(vectorStoreTool, this), | ||
}; | ||
} | ||
} |
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