{"version":3,"file":"voy.cjs","names":["VectorStore","client: VoyClient","embeddings: EmbeddingsInterface","documents: Document[]","vectors: number[][]","query: number[]","k: number","results: SearchResult","params: { deleteAll?: boolean }","texts: string[]","metadatas: object[] | object","docs: Document[]","Document"],"sources":["../../src/vectorstores/voy.ts"],"sourcesContent":["import type { Voy as VoyOriginClient, SearchResult } from \"voy-search\";\nimport type { EmbeddingsInterface } from \"@langchain/core/embeddings\";\nimport { VectorStore } from \"@langchain/core/vectorstores\";\nimport { Document } from \"@langchain/core/documents\";\n\nexport type VoyClient = Omit<\n  VoyOriginClient,\n  \"remove\" | \"size\" | \"serialize\" | \"free\"\n>;\n\n/**\n * Internal interface for storing documents mappings.\n */\ninterface InternalDoc {\n  embeddings: number[];\n  document: Document;\n}\n\n/**\n * Class that extends `VectorStore`. It allows to perform similarity search using\n * Voi similarity search engine. The class requires passing Voy Client as an input parameter.\n */\nexport class VoyVectorStore extends VectorStore {\n  client: VoyClient;\n\n  numDimensions: number | null = null;\n\n  docstore: InternalDoc[] = [];\n\n  _vectorstoreType(): string {\n    return \"voi\";\n  }\n\n  constructor(client: VoyClient, embeddings: EmbeddingsInterface) {\n    super(embeddings, {});\n    this.client = client;\n    this.embeddings = embeddings;\n  }\n\n  /**\n   * Adds documents to the Voy database. The documents are embedded using embeddings provided while instantiating the class.\n   * @param documents An array of `Document` instances associated with the vectors.\n   */\n  async addDocuments(documents: Document[]): Promise<void> {\n    const texts = documents.map(({ pageContent }) => pageContent);\n    if (documents.length === 0) {\n      return;\n    }\n\n    const firstVector = (\n      await this.embeddings.embedDocuments(texts.slice(0, 1))\n    )[0];\n    if (this.numDimensions === null) {\n      this.numDimensions = firstVector.length;\n    } else if (this.numDimensions !== firstVector.length) {\n      throw new Error(\n        `Vectors must have the same length as the number of dimensions (${this.numDimensions})`\n      );\n    }\n    const restResults = await this.embeddings.embedDocuments(texts.slice(1));\n    await this.addVectors([firstVector, ...restResults], documents);\n  }\n\n  /**\n   * Adds vectors to the Voy database. The vectors are associated with\n   * the provided documents.\n   * @param vectors An array of vectors to be added to the database.\n   * @param documents An array of `Document` instances associated with the vectors.\n   */\n  async addVectors(vectors: number[][], documents: Document[]): Promise<void> {\n    if (vectors.length === 0) {\n      return;\n    }\n    if (this.numDimensions === null) {\n      this.numDimensions = vectors[0].length;\n    }\n\n    if (vectors.length !== documents.length) {\n      throw new Error(`Vectors and metadata must have the same length`);\n    }\n    if (!vectors.every((v) => v.length === this.numDimensions)) {\n      throw new Error(\n        `Vectors must have the same length as the number of dimensions (${this.numDimensions})`\n      );\n    }\n\n    vectors.forEach((item, idx) => {\n      const doc = documents[idx];\n      this.docstore.push({ embeddings: item, document: doc });\n    });\n    const embeddings = this.docstore.map((item, idx) => ({\n      id: String(idx),\n      embeddings: item.embeddings,\n      title: \"\",\n      url: \"\",\n    }));\n    this.client.index({ embeddings });\n  }\n\n  /**\n   * Searches for vectors in the Voy database that are similar to the\n   * provided query vector.\n   * @param query The query vector.\n   * @param k The number of similar vectors to return.\n   * @returns A promise that resolves with an array of tuples, each containing a `Document` instance and a similarity score.\n   */\n  async similaritySearchVectorWithScore(query: number[], k: number) {\n    if (this.numDimensions === null) {\n      throw new Error(\"There aren't any elements in the index yet.\");\n    }\n    if (query.length !== this.numDimensions) {\n      throw new Error(\n        `Query vector must have the same length as the number of dimensions (${this.numDimensions})`\n      );\n    }\n    const itemsToQuery = Math.min(this.docstore.length, k);\n    if (itemsToQuery > this.docstore.length) {\n      console.warn(\n        `k (${k}) is greater than the number of elements in the index (${this.docstore.length}), setting k to ${itemsToQuery}`\n      );\n    }\n    const results: SearchResult = this.client.search(\n      new Float32Array(query),\n      itemsToQuery\n    );\n    return results.neighbors.map(\n      ({ id }, idx) =>\n        [this.docstore[parseInt(id, 10)].document, idx] as [Document, number]\n    );\n  }\n\n  /**\n   * Method to delete data from the Voy index. It can delete data based\n   * on specific IDs or a filter.\n   * @param params Object that includes either an array of IDs or a filter for the data to be deleted.\n   * @returns Promise that resolves when the deletion is complete.\n   */\n  async delete(params: { deleteAll?: boolean }): Promise<void> {\n    if (params.deleteAll === true) {\n      await this.client.clear();\n    } else {\n      throw new Error(`You must provide a \"deleteAll\" parameter.`);\n    }\n  }\n\n  /**\n   * Creates a new `VoyVectorStore` instance from an array of text strings. The text\n   * strings are converted to `Document` instances and added to the Voy\n   * database.\n   * @param texts An array of text strings.\n   * @param metadatas An array of metadata objects or a single metadata object. If an array is provided, it must have the same length as the `texts` array.\n   * @param embeddings An `Embeddings` instance used to generate embeddings for the documents.\n   * @param client An instance of Voy client to use in the underlying operations.\n   * @returns A promise that resolves with a new `VoyVectorStore` instance.\n   */\n  static async fromTexts(\n    texts: string[],\n    metadatas: object[] | object,\n    embeddings: EmbeddingsInterface,\n    client: VoyClient\n  ): Promise<VoyVectorStore> {\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 VoyVectorStore.fromDocuments(docs, embeddings, client);\n  }\n\n  /**\n   * Creates a new `VoyVectorStore` instance from an array of `Document` instances.\n   * The documents are added to the Voy database.\n   * @param docs An array of `Document` instances.\n   * @param embeddings An `Embeddings` instance used to generate embeddings for the documents.\n   * @param client An instance of Voy client to use in the underlying operations.\n   * @returns A promise that resolves with a new `VoyVectorStore` instance.\n   */\n  static async fromDocuments(\n    docs: Document[],\n    embeddings: EmbeddingsInterface,\n    client: VoyClient\n  ): Promise<VoyVectorStore> {\n    const instance = new VoyVectorStore(client, embeddings);\n    await instance.addDocuments(docs);\n    return instance;\n  }\n}\n"],"mappings":";;;;;;;;;;;AAsBA,IAAa,iBAAb,MAAa,uBAAuBA,0CAAY;CAC9C;CAEA,gBAA+B;CAE/B,WAA0B,CAAE;CAE5B,mBAA2B;AACzB,SAAO;CACR;CAED,YAAYC,QAAmBC,YAAiC;EAC9D,MAAM,YAAY,CAAE,EAAC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