{"version":3,"file":"zep_cloud.cjs","names":["Document","VectorStore","ZepClient","NotFoundError","FakeEmbeddings"],"sources":["../../src/vectorstores/zep_cloud.ts"],"sourcesContent":["import { ZepClient } from \"@getzep/zep-cloud\";\nimport {\n  CreateDocumentRequest,\n  DocumentSearchResult,\n  NotFoundError,\n} from \"@getzep/zep-cloud/api\";\nimport {\n  MaxMarginalRelevanceSearchOptions,\n  VectorStore,\n} from \"@langchain/core/vectorstores\";\nimport type { EmbeddingsInterface } from \"@langchain/core/embeddings\";\nimport { Document } from \"@langchain/core/documents\";\nimport { Callbacks } from \"@langchain/core/callbacks/manager\";\nimport { maximalMarginalRelevance } from \"@langchain/core/utils/math\";\nimport { FakeEmbeddings } from \"@langchain/core/utils/testing\";\n\nfunction zepDocsToDocumentsAndScore(\n  results: DocumentSearchResult[]\n): [Document, number][] {\n  return results.map((d) => [\n    new Document({\n      pageContent: d.content ?? \"\",\n      metadata: d.metadata,\n    }),\n    d.score ? d.score : 0,\n  ]);\n}\n\nfunction assignMetadata(\n  value: string | Record<string, unknown> | object | undefined\n): Record<string, unknown> | undefined {\n  if (typeof value === \"object\" && value !== null) {\n    return value as Record<string, unknown>;\n  }\n  if (value !== undefined) {\n    console.warn(\"Metadata filters must be an object, Record, or undefined.\");\n  }\n  return undefined;\n}\n\n/**\n * Interface for the configuration options for a ZepCloudVectorStore instance.\n */\nexport interface IZepCloudConfig {\n  apiKey?: string;\n  client?: ZepClient;\n  collectionName: string;\n  description?: string;\n  metadata?: Record<string, never>;\n}\n\n/**\n * Interface for the parameters required to delete documents from a\n * ZepCloudVectorStore instance.\n */\nexport interface IZepCloudDeleteParams {\n  uuids: string[];\n}\n\n/**\n * ZepCloudVectorStore is a VectorStore implementation\n * that uses the Zep long-term memory store as a backend.\n *\n * If the collection does not exist, it will be created automatically.\n *\n * Requires `@getzep/zep-cloud` to be installed:\n *\n *\n * @property {ZepClient} client - The ZepClient instance used to interact with Zep's API.\n * @property {Promise<void>} initPromise - A promise that resolves\n * when the collection is initialized.\n */\nexport class ZepCloudVectorStore extends VectorStore {\n  public client: ZepClient;\n\n  public collectionName: string;\n\n  private readonly initPromise: Promise<void>;\n\n  constructor(embeddings: EmbeddingsInterface, args: IZepCloudConfig) {\n    super(embeddings, args);\n\n    this.initPromise = this.initCollection(args).catch((err) => {\n      console.error(\"Error initializing collection:\", err);\n      throw err;\n    });\n  }\n\n  /**\n   * Initializes the document collection. If the collection does not exist, it creates a new one.\n   *\n   * @param {IZepConfig} args - The configuration object for the Zep API.\n   */\n  private async initCollection(args: IZepCloudConfig) {\n    if (args.client) {\n      this.client = args.client;\n    } else {\n      this.client = new ZepClient({\n        apiKey: args.apiKey,\n      });\n    }\n    try {\n      this.collectionName = args.collectionName;\n      await this.client.document.getCollection(this.collectionName);\n    } catch (err) {\n      // eslint-disable-next-line no-instanceof/no-instanceof\n      if (err instanceof Error) {\n        // eslint-disable-next-line no-instanceof/no-instanceof\n        if (err instanceof NotFoundError || err.name === \"NotFoundError\") {\n          await this.createCollection(args);\n        } else {\n          throw err;\n        }\n      }\n    }\n  }\n\n  /**\n   * Creates a new document collection.\n   *\n   * @param {IZepConfig} args - The configuration object for the Zep API.\n   */\n  private async createCollection(args: IZepCloudConfig) {\n    await this.client.document.addCollection(args.collectionName, {\n      description: args.description,\n      metadata: args.metadata,\n    });\n  }\n\n  async addVectors(): Promise<string[]> {\n    throw new Error(\"Adding vectors is not supported in Zep Cloud.\");\n  }\n\n  /**\n   * Adds documents to the collection. The documents are first embedded into vectors\n   * using the provided embedding model.\n   *\n   * @param {Document[]} documents - The documents to add.\n   * @returns {Promise<string[]>} - A promise that resolves with the UUIDs of the added documents.\n   */\n  async addDocuments(documents: Document[]): Promise<string[]> {\n    const docs: Array<CreateDocumentRequest> = [];\n    for (let i = 0; i < documents.length; i += 1) {\n      const doc: CreateDocumentRequest = {\n        content: documents[i].pageContent,\n        metadata: documents[i].metadata,\n      };\n      docs.push(doc);\n    }\n    // Wait for collection to be initialized\n    await this.initPromise;\n    return this.client.document.addDocuments(this.collectionName, docs);\n  }\n\n  _vectorstoreType(): string {\n    return \"zep\";\n  }\n\n  /**\n   * Deletes documents from the collection.\n   *\n   * @param {IZepDeleteParams} params - The list of Zep document UUIDs to delete.\n   * @returns {Promise<void>}\n   */\n  async delete(params: IZepCloudDeleteParams): Promise<void> {\n    // Wait for collection to be initialized\n    await this.initPromise;\n    for await (const uuid of params.uuids) {\n      await this.client.document.deleteDocument(this.collectionName, uuid);\n    }\n  }\n\n  async similaritySearchVectorWithScore(): Promise<[Document, number][]> {\n    throw new Error(\"Unsupported in Zep Cloud.\");\n  }\n\n  async _similaritySearchWithScore(\n    query: string,\n    k: number,\n    filter?: Record<string, unknown> | undefined\n  ): Promise<[Document, number][]> {\n    await this.initPromise;\n    const { results } = await this.client.document.search(this.collectionName, {\n      text: query,\n      metadata: assignMetadata(filter),\n      limit: k,\n    });\n    return zepDocsToDocumentsAndScore(results!);\n  }\n\n  async similaritySearchWithScore(\n    query: string,\n    k = 4,\n    filter: Record<string, unknown> | undefined = undefined,\n    _callbacks = undefined // implement passing to embedQuery later\n  ): Promise<[Document, number][]> {\n    return this._similaritySearchWithScore(query, k, filter);\n  }\n\n  /**\n   * Performs a similarity search on the Zep collection.\n   *\n   * @param {string} query - The query string to search for.\n   * @param {number} [k=4] - The number of results to return. Defaults to 4.\n   * @param {this[\"FilterType\"] | undefined} [filter=undefined] - An optional set of JSONPath filters to apply to the search.\n   * @param {Callbacks | undefined} [_callbacks=undefined] - Optional callbacks. Currently not implemented.\n   * @returns {Promise<Document[]>} - A promise that resolves to an array of Documents that are similar to the query.\n   *\n   * @async\n   */\n  async similaritySearch(\n    query: string,\n    k = 4,\n    filter: this[\"FilterType\"] | undefined = undefined,\n    _callbacks: Callbacks | undefined = undefined // implement passing to embedQuery later\n  ): Promise<Document[]> {\n    await this.initPromise;\n\n    const { results: zepResults } = await this.client.document.search(\n      this.collectionName,\n      {\n        text: query,\n        metadata: assignMetadata(filter),\n        limit: k,\n      }\n    );\n    const results = zepDocsToDocumentsAndScore(zepResults!);\n\n    return results.map((result) => result[0]);\n  }\n\n  /**\n   * Return documents selected using the maximal marginal relevance.\n   * Maximal marginal relevance optimizes for similarity to the query AND diversity\n   * among selected documents.\n   *\n   * @param {string} query - Text to look up documents similar to.\n   * @param options\n   * @param {number} options.k - Number of documents to return.\n   * @param {number} options.fetchK=20- Number of documents to fetch before passing to the MMR algorithm.\n   * @param {number} options.lambda=0.5 - Number between 0 and 1 that determines the degree of diversity among the results,\n   *                 where 0 corresponds to maximum diversity and 1 to minimum diversity.\n   * @param {Record<string, any>} options.filter - Optional Zep JSONPath query to pre-filter on document metadata field\n   *\n   * @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance.\n   */\n  async maxMarginalRelevanceSearch(\n    query: string,\n    options: MaxMarginalRelevanceSearchOptions<this[\"FilterType\"]>\n  ): Promise<Document[]> {\n    const { k, fetchK = 20, lambda = 0.5, filter } = options;\n\n    const r = await this.client.document.search(this.collectionName, {\n      text: query,\n      metadata: assignMetadata(filter),\n      limit: fetchK,\n    });\n\n    const queryEmbedding = Array.from(r.queryVector!);\n\n    const results = zepDocsToDocumentsAndScore(r.results!);\n\n    const embeddingList = r.results!.map((doc) =>\n      Array.from(doc.embedding ? doc.embedding : [])\n    );\n\n    const mmrIndexes = maximalMarginalRelevance(\n      queryEmbedding,\n      embeddingList,\n      lambda,\n      k\n    );\n\n    return mmrIndexes.filter((idx) => idx !== -1).map((idx) => results[idx][0]);\n  }\n\n  static async init(zepConfig: IZepCloudConfig) {\n    const instance = new this(new FakeEmbeddings(), zepConfig);\n    // Wait for collection to be initialized\n    await instance.initPromise;\n    return instance;\n  }\n\n  /**\n   * Creates a new ZepVectorStore instance from an array of texts. Each text is converted into a Document and added to the collection.\n   *\n   * @param {string[]} texts - The texts to convert into Documents.\n   * @param {object[] | object} metadatas - The metadata to associate with each Document.\n   * If an array is provided, each element is associated with the corresponding Document.\n   * If an object is provided, it is associated with all Documents.\n   * @param {Embeddings} embeddings - Pass FakeEmbeddings, Zep Cloud will handle text embedding for you.\n   * @param {IZepConfig} zepConfig - The configuration object for the Zep API.\n   * @returns {Promise<ZepVectorStore>} - A promise that resolves with the new ZepVectorStore instance.\n   */\n  static async fromTexts(\n    texts: string[],\n    metadatas: object[] | object,\n    embeddings: EmbeddingsInterface,\n    zepConfig: IZepCloudConfig\n  ): Promise<ZepCloudVectorStore> {\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 ZepCloudVectorStore.fromDocuments(docs, embeddings, zepConfig);\n  }\n\n  /**\n   * Creates a new ZepVectorStore instance from an array of Documents. Each Document is added to a Zep collection.\n   *\n   * @param {Document[]} docs - The Documents to add.\n   * @param {Embeddings} embeddings - Pass FakeEmbeddings, Zep Cloud will handle text embedding for you.\n   * @param {IZepConfig} zepConfig - The configuration object for the Zep API.\n   * @returns {Promise<ZepVectorStore>} - A promise that resolves with the new ZepVectorStore instance.\n   */\n  static async fromDocuments(\n    docs: Document[],\n    embeddings: EmbeddingsInterface,\n    zepConfig: IZepCloudConfig\n  ): Promise<ZepCloudVectorStore> {\n    const instance = new this(embeddings, zepConfig);\n    // Wait for collection to be initialized\n    await instance.initPromise;\n    await instance.addDocuments(docs);\n    return instance;\n  }\n}\n"],"mappings":";;;;;;;;;;AAgBA,SAAS,2BACP,SACsB;AACtB,QAAO,QAAQ,KAAK,MAAM,CACxB,IAAIA,0BAAAA,SAAS;EACX,aAAa,EAAE,WAAW;EAC1B,UAAU,EAAE;EACb,CAAC,EACF,EAAE,QAAQ,EAAE,QAAQ,EACrB,CAAC;;AAGJ,SAAS,eACP,OACqC;AACrC,KAAI,OAAO,UAAU,YAAY,UAAU,KACzC,QAAO;AAET,KAAI,UAAU,KAAA,EACZ,SAAQ,KAAK,4DAA4D;;;;;;;;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