{"version":3,"file":"vectorstores.cjs","names":["VectorStore","cosine","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  // oxlint-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,qBAAAA,YAAY;CAG/C,gBAAgC,CAAC;CAEjC;CAEA,mBAA2B;EACzB,OAAO;CACT;CAEA,YACE,YACA,EAAE,YAAY,GAAG,SAA8B,CAAC,GAChD;EACA,MAA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