add vector upsert ability

This commit is contained in:
Henry
2023-11-16 18:34:17 +00:00
parent 9205a29787
commit 44cadc1cc3
108 changed files with 10019 additions and 4726 deletions
@@ -1,9 +1,9 @@
import { INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
import { flatten } from 'lodash'
import { MemoryVectorStore } from 'langchain/vectorstores/memory'
import { Embeddings } from 'langchain/embeddings/base'
import { Document } from 'langchain/document'
import { INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { flatten } from 'lodash'
class InMemoryVectorStore_VectorStores implements INode {
label: string
@@ -61,6 +61,28 @@ class InMemoryVectorStore_VectorStores implements INode {
]
}
//@ts-ignore
vectorStoreMethods = {
async upsert(nodeData: INodeData): Promise<void> {
const docs = nodeData.inputs?.document as Document[]
const embeddings = nodeData.inputs?.embeddings as Embeddings
const flattenDocs = docs && docs.length ? flatten(docs) : []
const finalDocs = []
for (let i = 0; i < flattenDocs.length; i += 1) {
if (flattenDocs[i] && flattenDocs[i].pageContent) {
finalDocs.push(new Document(flattenDocs[i]))
}
}
try {
await MemoryVectorStore.fromDocuments(finalDocs, embeddings)
} catch (e) {
throw new Error(e)
}
}
}
async init(nodeData: INodeData): Promise<any> {
const docs = nodeData.inputs?.document as Document[]
const embeddings = nodeData.inputs?.embeddings as Embeddings