wip: LanceDB node POC

This commit is contained in:
Eugene Molodkin 2024-10-04 11:03:00 +02:00
parent 609381de3d
commit 8a03ec267a
No known key found for this signature in database
3 changed files with 42 additions and 24 deletions

View file

@ -1,7 +1,7 @@
import { LanceDB } from '@langchain/community/vectorstores/lancedb'; import { LanceDB } from '@langchain/community/vectorstores/lancedb';
import { connect, WriteMode } from 'vectordb'; import { connect, WriteMode } from 'vectordb';
import type { INodeProperties } from 'n8n-workflow'; import type { ILoadOptionsFunctions, INodeProperties } from 'n8n-workflow';
import { createVectorStoreNode } from '../shared/createVectorStoreNode'; import { createVectorStoreNode } from '../shared/createVectorStoreNode';
export const lanceDBTableNameRLC: INodeProperties = { export const lanceDBTableNameRLC: INodeProperties = {
@ -52,18 +52,7 @@ export const VectorStoreLanceDB = createVectorStoreNode({
docsUrl: docsUrl:
'https://docs.n8n.io/integrations/builtin/cluster-nodes/root-nodes/n8n-nodes-langchain.vectorstorelancedb/', 'https://docs.n8n.io/integrations/builtin/cluster-nodes/root-nodes/n8n-nodes-langchain.vectorstorelancedb/',
}, },
sharedFields: [ sharedFields: [lanceDBTableNameRLC],
{
displayName: 'Directory Path',
name: 'directoryPath',
type: 'string',
default: '',
required: true,
placeholder: '/tmp/lancedb/',
description: 'Path of the LanceDB directory',
},
lanceDBTableNameRLC,
],
insertFields, insertFields,
loadFields: [], loadFields: [],
retrieveFields: [], retrieveFields: [],
@ -71,18 +60,17 @@ export const VectorStoreLanceDB = createVectorStoreNode({
const tableName = context.getNodeParameter('tableName', itemIndex, '', { const tableName = context.getNodeParameter('tableName', itemIndex, '', {
extractValue: true, extractValue: true,
}) as string; }) as string;
const dbUri = context.getNodeParameter('directoryPath', itemIndex, '', {
extractValue: true, const dbUri = context.helpers.getVectorStorePath();
}) as string;
const db = await connect(dbUri); const db = await connect(dbUri);
const table = await db.openTable(tableName); const table = await db.openTable(tableName);
const client = new LanceDB(embeddings, { table }); const client = new LanceDB(embeddings, { table });
const test = await client.similaritySearch('horror movie', 5); // const test = await client.similaritySearch('horror movie', 5);
//
console.log(test); // console.log(test);
return client; return client;
}, },
@ -90,15 +78,38 @@ export const VectorStoreLanceDB = createVectorStoreNode({
const tableName = context.getNodeParameter('tableName', itemIndex, '', { const tableName = context.getNodeParameter('tableName', itemIndex, '', {
extractValue: true, extractValue: true,
}) as string; }) as string;
const dbUri = context.getNodeParameter('directoryPath', itemIndex, '', {
extractValue: true, const dbUri = context.helpers.getVectorStorePath();
}) as string;
const clearStore = context.getNodeParameter('clearStore', itemIndex) as boolean; 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, { await LanceDB.fromDocuments(documents, embeddings, {
tableName, tableName,
uri: dbUri, 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,
})),
};
},
},
},
}); });

View file

@ -3416,6 +3416,11 @@ const getFileSystemHelperFunctions = (node: INode): FileSystemHelperFunctions =>
return path.join(Container.get(InstanceSettings).n8nFolder, `storage/${node.type}`); 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) { async writeContentToFile(filePath, content, flag) {
if (isFilePathBlocked(filePath as string)) { if (isFilePathBlocked(filePath as string)) {
throw new NodeOperationError(node, `The file "${String(filePath)}" is not writable.`, { throw new NodeOperationError(node, `The file "${String(filePath)}" is not writable.`, {
@ -4150,6 +4155,7 @@ export function getLoadOptionsFunctions(
helpers: { helpers: {
...getSSHTunnelFunctions(), ...getSSHTunnelFunctions(),
...getRequestHelperFunctions(workflow, node, additionalData), ...getRequestHelperFunctions(workflow, node, additionalData),
...getFileSystemHelperFunctions(node),
}, },
}; };
})(workflow, node, path); })(workflow, node, path);

View file

@ -739,6 +739,7 @@ interface JsonHelperFunctions {
export interface FileSystemHelperFunctions { export interface FileSystemHelperFunctions {
createReadStream(path: PathLike): Promise<Readable>; createReadStream(path: PathLike): Promise<Readable>;
getStoragePath(): string; getStoragePath(): string;
getVectorStorePath(): string;
writeContentToFile( writeContentToFile(
path: PathLike, path: PathLike,
content: string | Buffer | Readable, content: string | Buffer | Readable,
@ -989,7 +990,7 @@ export interface ILoadOptionsFunctions extends FunctionsBase {
options?: IGetNodeParameterOptions, options?: IGetNodeParameterOptions,
): NodeParameterValueType | object | undefined; ): NodeParameterValueType | object | undefined;
getCurrentNodeParameters(): INodeParameters | undefined; getCurrentNodeParameters(): INodeParameters | undefined;
helpers: RequestHelperFunctions & SSHTunnelFunctions; helpers: RequestHelperFunctions & SSHTunnelFunctions & FileSystemHelperFunctions;
} }
export interface IPollFunctions export interface IPollFunctions