UNPKG

@langchain/core

Version:
1 lines 9.16 kB
{"version":3,"file":"vectorstores.cjs","names":["VectorStore","embeddings: EmbeddingsInterface","cosine","documents: Document[]","vectors: number[][]","query: number[]","k: number","filter?: this[\"FilterType\"]","memoryVector: MemoryVector","Document","result: [Document, number][]","texts: string[]","metadatas: object[] | object","dbConfig?: FakeVectorStoreArgs","docs: Document[]"],"sources":["../../../src/utils/testing/vectorstores.ts"],"sourcesContent":["import { Document } from \"../../documents/document.js\";\nimport { EmbeddingsInterface } from \"../../embeddings.js\";\nimport { VectorStore } from \"../../vectorstores.js\";\nimport { cosine } from \"../ml-distance/similarities.js\";\n\n/**\n * Interface representing a vector in memory. It includes the content\n * (text), the corresponding embedding (vector), and any associated\n * metadata.\n */\ninterface MemoryVector {\n content: string;\n embedding: number[];\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n metadata: Record<string, any>;\n}\n\n/**\n * Interface for the arguments that can be passed to the\n * `FakeVectorStore` constructor. It includes an optional `similarity`\n * function.\n */\nexport interface FakeVectorStoreArgs {\n similarity?: typeof cosine;\n}\n\n/**\n * Class that extends `VectorStore` to store vectors in memory. Provides\n * methods for adding documents, performing similarity searches, and\n * creating instances from texts, documents, or an existing index.\n */\nexport class FakeVectorStore extends VectorStore {\n declare FilterType: (doc: Document) => boolean;\n\n memoryVectors: MemoryVector[] = [];\n\n similarity: typeof cosine;\n\n _vectorstoreType(): string {\n return \"memory\";\n }\n\n constructor(\n embeddings: EmbeddingsInterface,\n { similarity, ...rest }: FakeVectorStoreArgs = {}\n ) {\n super(embeddings, rest);\n\n this.similarity = similarity ?? cosine;\n }\n\n /**\n * Method to add documents to the memory vector store. It extracts the\n * text from each document, generates embeddings for them, and adds the\n * resulting vectors to the store.\n * @param documents Array of `Document` instances to be added to the store.\n * @returns Promise that resolves when all documents have been added.\n */\n async addDocuments(documents: Document[]): Promise<void> {\n const texts = documents.map(({ pageContent }) => pageContent);\n return this.addVectors(\n await this.embeddings.embedDocuments(texts),\n documents\n );\n }\n\n /**\n * Method to add vectors to the memory vector store. It creates\n * `MemoryVector` instances for each vector and document pair and adds\n * them to the store.\n * @param vectors Array of vectors to be added to the store.\n * @param documents Array of `Document` instances corresponding to the vectors.\n * @returns Promise that resolves when all vectors have been added.\n */\n async addVectors(vectors: number[][], documents: Document[]): Promise<void> {\n const memoryVectors = vectors.map((embedding, idx) => ({\n content: documents[idx].pageContent,\n embedding,\n metadata: documents[idx].metadata,\n }));\n\n this.memoryVectors = this.memoryVectors.concat(memoryVectors);\n }\n\n /**\n * Method to perform a similarity search in the memory vector store. It\n * calculates the similarity between the query vector and each vector in\n * the store, sorts the results by similarity, and returns the top `k`\n * results along with their scores.\n * @param query Query vector to compare against the vectors in the store.\n * @param k Number of top results to return.\n * @param filter Optional filter function to apply to the vectors before performing the search.\n * @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.\n */\n async similaritySearchVectorWithScore(\n query: number[],\n k: number,\n filter?: this[\"FilterType\"]\n ): Promise<[Document, number][]> {\n const filterFunction = (memoryVector: MemoryVector) => {\n if (!filter) {\n return true;\n }\n\n const doc = new Document({\n metadata: memoryVector.metadata,\n pageContent: memoryVector.content,\n });\n return filter(doc);\n };\n const filteredMemoryVectors = this.memoryVectors.filter(filterFunction);\n const searches = filteredMemoryVectors\n .map((vector, index) => ({\n similarity: this.similarity(query, vector.embedding),\n index,\n }))\n .sort((a, b) => (a.similarity > b.similarity ? -1 : 0))\n .slice(0, k);\n\n const result: [Document, number][] = searches.map((search) => [\n new Document({\n metadata: filteredMemoryVectors[search.index].metadata,\n pageContent: filteredMemoryVectors[search.index].content,\n }),\n search.similarity,\n ]);\n\n return result;\n }\n\n /**\n * Static method to create a `FakeVectorStore` instance from an array of\n * texts. It creates a `Document` for each text and metadata pair, and\n * adds them to the store.\n * @param texts Array of texts to be added to the store.\n * @param metadatas Array or single object of metadata corresponding to the texts.\n * @param embeddings `Embeddings` instance used to generate embeddings for the texts.\n * @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.\n * @returns Promise that resolves with a new `FakeVectorStore` instance.\n */\n static async fromTexts(\n texts: string[],\n metadatas: object[] | object,\n embeddings: EmbeddingsInterface,\n dbConfig?: FakeVectorStoreArgs\n ): Promise<FakeVectorStore> {\n const docs: Document[] = [];\n for (let i = 0; i < texts.length; i += 1) {\n const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;\n const newDoc = new Document({\n pageContent: texts[i],\n metadata,\n });\n docs.push(newDoc);\n }\n return FakeVectorStore.fromDocuments(docs, embeddings, dbConfig);\n }\n\n /**\n * Static method to create a `FakeVectorStore` instance from an array of\n * `Document` instances. It adds the documents to the store.\n * @param docs Array of `Document` instances to be added to the store.\n * @param embeddings `Embeddings` instance used to generate embeddings for the documents.\n * @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.\n * @returns Promise that resolves with a new `FakeVectorStore` instance.\n */\n static async fromDocuments(\n docs: Document[],\n embeddings: EmbeddingsInterface,\n dbConfig?: FakeVectorStoreArgs\n ): Promise<FakeVectorStore> {\n const instance = new this(embeddings, dbConfig);\n await instance.addDocuments(docs);\n return instance;\n }\n\n /**\n * Static method to create a `FakeVectorStore` instance from an existing\n * index. It creates a new `FakeVectorStore` instance without adding any\n * documents or vectors.\n * @param embeddings `Embeddings` instance used to generate embeddings for the documents.\n * @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.\n * @returns Promise that resolves with a new `FakeVectorStore` instance.\n */\n static async fromExistingIndex(\n embeddings: EmbeddingsInterface,\n dbConfig?: FakeVectorStoreArgs\n ): Promise<FakeVectorStore> {\n const instance = new this(embeddings, dbConfig);\n return instance;\n }\n}\n"],"mappings":";;;;;;;;;;AA+BA,IAAa,kBAAb,MAAa,wBAAwBA,iCAAY;CAG/C,gBAAgC,CAAE;CAElC;CAEA,mBAA2B;AACzB,SAAO;CACR;CAED,YACEC,YACA,EAAE,WAAY,GAAG,MAA2B,GAAG,CAAE,GACjD;E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