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 { 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,
})),
};
},
},
},
});

View file

@ -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);

View file

@ -739,6 +739,7 @@ interface JsonHelperFunctions {
export interface FileSystemHelperFunctions {
createReadStream(path: PathLike): Promise<Readable>;
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