Bugfix/Supabase Upserting (#2210)

* fix supabase upserting error

* fix supabase upserting error

* update pnpm to 9.0.3
This commit is contained in:
Henry Heng
2024-04-19 00:52:28 +01:00
committed by GitHub
parent e4ab2a9e33
commit d1c8f7eb96
4 changed files with 63 additions and 5 deletions
@@ -130,7 +130,7 @@ class Supabase_VectorStores implements INode {
try {
if (recordManager) {
const vectorStore = await SupabaseVectorStore.fromExistingIndex(embeddings, {
const vectorStore = await SupabaseUpsertVectorStore.fromExistingIndex(embeddings, {
client,
tableName: tableName,
queryName: queryName
@@ -148,7 +148,7 @@ class Supabase_VectorStores implements INode {
})
return res
} else {
await SupabaseVectorStore.fromDocuments(finalDocs, embeddings, {
await SupabaseUpsertVectorStore.fromDocuments(finalDocs, embeddings, {
client,
tableName: tableName,
queryName: queryName
@@ -190,4 +190,33 @@ class Supabase_VectorStores implements INode {
}
}
class SupabaseUpsertVectorStore extends SupabaseVectorStore {
async addVectors(vectors: number[][], documents: Document[]): Promise<string[]> {
if (vectors.length === 0) {
return []
}
const rows = vectors.map((embedding, idx) => ({
content: documents[idx].pageContent,
embedding,
metadata: documents[idx].metadata
}))
let returnedIds: string[] = []
for (let i = 0; i < rows.length; i += this.upsertBatchSize) {
const chunk = rows.slice(i, i + this.upsertBatchSize).map((row, index) => {
return { id: index, ...row }
})
const res = await this.client.from(this.tableName).upsert(chunk).select()
if (res.error) {
throw new Error(`Error inserting: ${res.error.message} ${res.status} ${res.statusText}`)
}
if (res.data) {
returnedIds = returnedIds.concat(res.data.map((row) => row.id))
}
}
return returnedIds
}
}
module.exports = { nodeClass: Supabase_VectorStores }