diff --git a/packages/@n8n/nodes-langchain/nodes/vector_store/VectorStoreLanceDB/VectorStoreLanceDB.node.ts b/packages/@n8n/nodes-langchain/nodes/vector_store/VectorStoreLanceDB/VectorStoreLanceDB.node.ts index 1c451ae217..ff55ef6f8a 100644 --- a/packages/@n8n/nodes-langchain/nodes/vector_store/VectorStoreLanceDB/VectorStoreLanceDB.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/vector_store/VectorStoreLanceDB/VectorStoreLanceDB.node.ts @@ -1,7 +1,7 @@ import { LanceDB } from '@langchain/community/vectorstores/lancedb'; import { connect, WriteMode } from 'vectordb'; -import type { INodeProperties } from 'n8n-workflow'; +import type { ILoadOptionsFunctions, INodeProperties } from 'n8n-workflow'; import { createVectorStoreNode } from '../shared/createVectorStoreNode'; export const lanceDBTableNameRLC: INodeProperties = { @@ -52,18 +52,7 @@ export const VectorStoreLanceDB = createVectorStoreNode({ docsUrl: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/root-nodes/n8n-nodes-langchain.vectorstorelancedb/', }, - sharedFields: [ - { - displayName: 'Directory Path', - name: 'directoryPath', - type: 'string', - default: '', - required: true, - placeholder: '/tmp/lancedb/', - description: 'Path of the LanceDB directory', - }, - lanceDBTableNameRLC, - ], + sharedFields: [lanceDBTableNameRLC], insertFields, loadFields: [], retrieveFields: [], @@ -71,18 +60,17 @@ export const VectorStoreLanceDB = createVectorStoreNode({ const tableName = context.getNodeParameter('tableName', itemIndex, '', { extractValue: true, }) as string; - const dbUri = context.getNodeParameter('directoryPath', itemIndex, '', { - extractValue: true, - }) as string; + + const dbUri = context.helpers.getVectorStorePath(); const db = await connect(dbUri); const table = await db.openTable(tableName); const client = new LanceDB(embeddings, { table }); - const test = await client.similaritySearch('horror movie', 5); - - console.log(test); + // const test = await client.similaritySearch('horror movie', 5); + // + // console.log(test); return client; }, @@ -90,15 +78,38 @@ export const VectorStoreLanceDB = createVectorStoreNode({ const tableName = context.getNodeParameter('tableName', itemIndex, '', { extractValue: true, }) as string; - const dbUri = context.getNodeParameter('directoryPath', itemIndex, '', { - extractValue: true, - }) as string; + + const dbUri = context.helpers.getVectorStorePath(); + const clearStore = context.getNodeParameter('clearStore', itemIndex) as boolean; + if (clearStore) { + const db = await connect(dbUri); + await db.dropTable(tableName); + console.log('cleared lancedb'); + } + await LanceDB.fromDocuments(documents, embeddings, { tableName, uri: dbUri, - mode: clearStore ? WriteMode.Overwrite : WriteMode.Append, + mode: WriteMode.Append, }); }, + methods: { + listSearch: { + async lanceDBTableNameSearch(this: ILoadOptionsFunctions) { + const dbUri = this.helpers.getVectorStorePath(); + const db = await connect(dbUri); + + const tables = await db.tableNames(); + + return { + results: tables.map((t) => ({ + name: t, + value: t, + })), + }; + }, + }, + }, }); diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index af428026b7..f797e7840e 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -3416,6 +3416,11 @@ const getFileSystemHelperFunctions = (node: INode): FileSystemHelperFunctions => return path.join(Container.get(InstanceSettings).n8nFolder, `storage/${node.type}`); }, + getVectorStorePath() { + // FIXME: make customizable via config + return path.join(Container.get(InstanceSettings).n8nFolder, 'vector-store'); + }, + async writeContentToFile(filePath, content, flag) { if (isFilePathBlocked(filePath as string)) { throw new NodeOperationError(node, `The file "${String(filePath)}" is not writable.`, { @@ -4150,6 +4155,7 @@ export function getLoadOptionsFunctions( helpers: { ...getSSHTunnelFunctions(), ...getRequestHelperFunctions(workflow, node, additionalData), + ...getFileSystemHelperFunctions(node), }, }; })(workflow, node, path); diff --git a/packages/workflow/src/Interfaces.ts b/packages/workflow/src/Interfaces.ts index 15802fe0b0..f0f903ccca 100644 --- a/packages/workflow/src/Interfaces.ts +++ b/packages/workflow/src/Interfaces.ts @@ -739,6 +739,7 @@ interface JsonHelperFunctions { export interface FileSystemHelperFunctions { createReadStream(path: PathLike): Promise; getStoragePath(): string; + getVectorStorePath(): string; writeContentToFile( path: PathLike, content: string | Buffer | Readable, @@ -989,7 +990,7 @@ export interface ILoadOptionsFunctions extends FunctionsBase { options?: IGetNodeParameterOptions, ): NodeParameterValueType | object | undefined; getCurrentNodeParameters(): INodeParameters | undefined; - helpers: RequestHelperFunctions & SSHTunnelFunctions; + helpers: RequestHelperFunctions & SSHTunnelFunctions & FileSystemHelperFunctions; } export interface IPollFunctions