From 8cdf5b3ff937288eb45d264b52bb75f23fe6557a Mon Sep 17 00:00:00 2001 From: chungyau97 Date: Thu, 27 Apr 2023 20:03:42 +0700 Subject: [PATCH] add supabase upsert --- .../Supabase_Upsert/Supabase_Upsert.ts | 105 ++++++++++++++++++ .../vectorstores/Supabase_Upsert/supabase.svg | 15 +++ packages/components/package.json | 1 + 3 files changed, 121 insertions(+) create mode 100644 packages/components/nodes/vectorstores/Supabase_Upsert/Supabase_Upsert.ts create mode 100644 packages/components/nodes/vectorstores/Supabase_Upsert/supabase.svg diff --git a/packages/components/nodes/vectorstores/Supabase_Upsert/Supabase_Upsert.ts b/packages/components/nodes/vectorstores/Supabase_Upsert/Supabase_Upsert.ts new file mode 100644 index 00000000..3f1f9170 --- /dev/null +++ b/packages/components/nodes/vectorstores/Supabase_Upsert/Supabase_Upsert.ts @@ -0,0 +1,105 @@ +import { INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface' +import { Embeddings } from 'langchain/embeddings/base' +import { Document } from 'langchain/document' +import { getBaseClasses } from '../../../src/utils' +import { SupabaseVectorStore } from 'langchain/vectorstores/supabase' +import { createClient } from '@supabase/supabase-js' + +class SupabaseUpsert_VectorStores implements INode { + label: string + name: string + description: string + type: string + icon: string + category: string + baseClasses: string[] + inputs: INodeParams[] + outputs: INodeOutputsValue[] + + constructor() { + this.label = 'Supabase Upsert Document' + this.name = 'supabaseUpsert' + this.type = 'supabase' + this.icon = 'supabase.svg' + this.category = 'Vector Stores' + this.description = 'Upsert documents to Supabase' + this.baseClasses = [this.type, 'VectorStoreRetriever', 'BaseRetriever'] + this.inputs = [ + { + label: 'Document', + name: 'document', + type: 'Document' + }, + { + label: 'Embeddings', + name: 'embeddings', + type: 'Embeddings' + }, + { + label: 'Supabase API Key', + name: 'supabaseApiKey', + type: 'password' + }, + { + label: 'Supabase Project URL', + name: 'supabaseProjUrl', + type: 'string' + }, + { + label: 'Table Name', + name: 'tableName', + type: 'string' + }, + { + label: 'Query Name', + name: 'queryName', + type: 'string' + } + ] + this.outputs = [ + { + label: 'Supabase Retriever', + name: 'retriever', + baseClasses: this.baseClasses + }, + { + label: 'Supabase Vector Store', + name: 'vectorStore', + baseClasses: [this.type, ...getBaseClasses(SupabaseVectorStore)] + } + ] + } + + async init(nodeData: INodeData): Promise { + const supabaseApiKey = nodeData.inputs?.supabaseApiKey as string + const supabaseProjUrl = nodeData.inputs?.supabaseProjUrl as string + const tableName = nodeData.inputs?.tableName as string + const queryName = nodeData.inputs?.queryName as string + const docs = nodeData.inputs?.document as Document[] + const embeddings = nodeData.inputs?.embeddings as Embeddings + const output = nodeData.outputs?.output as string + + const client = createClient(supabaseProjUrl, supabaseApiKey) + + const finalDocs = [] + for (let i = 0; i < docs.length; i += 1) { + finalDocs.push(new Document(docs[i])) + } + + const vectorStore = await SupabaseVectorStore.fromDocuments(finalDocs, embeddings, { + client, + tableName: tableName, + queryName: queryName + }) + + if (output === 'retriever') { + const retriever = vectorStore.asRetriever() + return retriever + } else if (output === 'vectorStore') { + return vectorStore + } + return vectorStore + } +} + +module.exports = { nodeClass: SupabaseUpsert_VectorStores } diff --git a/packages/components/nodes/vectorstores/Supabase_Upsert/supabase.svg b/packages/components/nodes/vectorstores/Supabase_Upsert/supabase.svg new file mode 100644 index 00000000..884d6449 --- /dev/null +++ b/packages/components/nodes/vectorstores/Supabase_Upsert/supabase.svg @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/packages/components/package.json b/packages/components/package.json index 1d8af269..6446c3e9 100644 --- a/packages/components/package.json +++ b/packages/components/package.json @@ -19,6 +19,7 @@ "@dqbd/tiktoken": "^1.0.4", "@huggingface/inference": "^1.6.3", "@pinecone-database/pinecone": "^0.0.12", + "@supabase/supabase-js": "^2.21.0", "axios": "^0.27.2", "cheerio": "^1.0.0-rc.12", "chromadb": "^1.3.1",