{"version":3,"file":"togetherai.cjs","names":["Embeddings"],"sources":["../../src/embeddings/togetherai.ts"],"sourcesContent":["import { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport { Embeddings, type EmbeddingsParams } from \"@langchain/core/embeddings\";\nimport { chunkArray } from \"@langchain/core/utils/chunk_array\";\n\n/**\n * Interface for TogetherAIEmbeddingsParams parameters. Extends EmbeddingsParams and\n * defines additional parameters specific to the TogetherAIEmbeddings class.\n */\nexport interface TogetherAIEmbeddingsParams extends EmbeddingsParams {\n  /**\n   * The API key to use for the TogetherAI API.\n   * @default {process.env.TOGETHER_AI_API_KEY}\n   */\n  apiKey?: string;\n\n  /**\n   * Model name to use\n   * Alias for `model`\n   * @default {\"togethercomputer/m2-bert-80M-8k-retrieval\"}\n   */\n  modelName?: string;\n  /**\n   * Model name to use\n   * @default {\"togethercomputer/m2-bert-80M-8k-retrieval\"}\n   */\n  model?: string;\n\n  /**\n   * Timeout to use when making requests to TogetherAI.\n   * @default {undefined}\n   */\n  timeout?: number;\n\n  /**\n   * The maximum number of documents to embed in a single request.\n   * @default {512}\n   */\n  batchSize?: number;\n\n  /**\n   * Whether to strip new lines from the input text. May not be suitable\n   * for all use cases.\n   * @default {false}\n   */\n  stripNewLines?: boolean;\n}\n\n/** @ignore */\ninterface TogetherAIEmbeddingsResult {\n  object: string;\n  data: Array<{\n    object: \"embedding\";\n    embedding: number[];\n    index: number;\n  }>;\n  model: string;\n  request_id: string;\n}\n\n/**\n * Class for generating embeddings using the TogetherAI API. Extends the\n * Embeddings class and implements TogetherAIEmbeddingsParams.\n * @example\n * ```typescript\n * const embeddings = new TogetherAIEmbeddings({\n *   apiKey: process.env.TOGETHER_AI_API_KEY, // Default value\n *   model: \"togethercomputer/m2-bert-80M-8k-retrieval\", // Default value\n * });\n * const res = await embeddings.embedQuery(\n *   \"What would be a good company name a company that makes colorful socks?\"\n * );\n * ```\n */\nexport class TogetherAIEmbeddings\n  extends Embeddings\n  implements TogetherAIEmbeddingsParams\n{\n  modelName = \"togethercomputer/m2-bert-80M-8k-retrieval\";\n\n  model = \"togethercomputer/m2-bert-80M-8k-retrieval\";\n\n  apiKey: string;\n\n  batchSize = 512;\n\n  stripNewLines = false;\n\n  timeout?: number;\n\n  private embeddingsAPIUrl = \"https://api.together.xyz/v1/embeddings\";\n\n  constructor(fields?: Partial<TogetherAIEmbeddingsParams>) {\n    super(fields ?? {});\n\n    const apiKey =\n      fields?.apiKey ?? getEnvironmentVariable(\"TOGETHER_AI_API_KEY\");\n    if (!apiKey) {\n      throw new Error(\"TOGETHER_AI_API_KEY not found.\");\n    }\n\n    this.apiKey = apiKey;\n    this.modelName = fields?.model ?? fields?.modelName ?? this.model;\n    this.model = this.modelName;\n    this.timeout = fields?.timeout;\n    this.batchSize = fields?.batchSize ?? this.batchSize;\n    this.stripNewLines = fields?.stripNewLines ?? this.stripNewLines;\n  }\n\n  private constructHeaders() {\n    return {\n      accept: \"application/json\",\n      \"content-type\": \"application/json\",\n      Authorization: `Bearer ${this.apiKey}`,\n    };\n  }\n\n  private constructBody(input: string) {\n    const body = {\n      model: this?.model,\n      input,\n    };\n    return body;\n  }\n\n  /**\n   * Method to generate embeddings for an array of documents. Splits the\n   * documents into batches and makes requests to the TogetherAI API to generate\n   * embeddings.\n   * @param texts Array of documents to generate embeddings for.\n   * @returns Promise that resolves to a 2D array of embeddings for each document.\n   */\n  async embedDocuments(texts: string[]): Promise<number[][]> {\n    const batches = chunkArray(\n      this.stripNewLines ? texts.map((t) => t.replace(/\\n/g, \" \")) : texts,\n      this.batchSize\n    );\n\n    let batchResponses: TogetherAIEmbeddingsResult[] = [];\n    for await (const batch of batches) {\n      const batchRequests = batch.map((item) => this.embeddingWithRetry(item));\n      const response = await Promise.all(batchRequests);\n      batchResponses = batchResponses.concat(response);\n    }\n\n    const embeddings: number[][] = batchResponses.map(\n      (response) => response.data[0].embedding\n    );\n    return embeddings;\n  }\n\n  /**\n   * Method to generate an embedding for a single document. Calls the\n   * embeddingWithRetry method with the document as the input.\n   * @param {string} text Document to generate an embedding for.\n   * @returns {Promise<number[]>} Promise that resolves to an embedding for the document.\n   */\n  async embedQuery(text: string): Promise<number[]> {\n    const { data } = await this.embeddingWithRetry(\n      this.stripNewLines ? text.replace(/\\n/g, \" \") : text\n    );\n    return data[0].embedding;\n  }\n\n  /**\n   * Private method to make a request to the TogetherAI API to generate\n   * embeddings. Handles the retry logic and returns the response from the\n   * API.\n   * @param {string} input The input text to embed.\n   * @returns Promise that resolves to the response from the API.\n   * @TODO Figure out return type and statically type it.\n   */\n  private async embeddingWithRetry(\n    input: string\n  ): Promise<TogetherAIEmbeddingsResult> {\n    const body = JSON.stringify(this.constructBody(input));\n    const headers = this.constructHeaders();\n\n    return this.caller.call(async () => {\n      const fetchResponse = await fetch(this.embeddingsAPIUrl, {\n        method: \"POST\",\n        headers,\n        body,\n      });\n\n      if (fetchResponse.status === 200) {\n        return fetchResponse.json();\n      }\n      throw new Error(\n        `Error getting prompt completion from Together AI. ${JSON.stringify(\n          await fetchResponse.json(),\n          null,\n          2\n        )}`\n      );\n    });\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAyEA,IAAa,uBAAb,cACUA,2BAAAA,WAEV;CACE,YAAY;CAEZ,QAAQ;CAER;CAEA,YAAY;CAEZ,gBAAgB;CAEhB;CAEA,mBAA2B;CAE3B,YAAY,QAA8C;AACxD,QAAM,UAAU,EAAE,CAAC;EAEnB,MAAM,SACJ,QAAQ,WAAA,GAAA,0BAAA,wBAAiC,sBAAsB;AACjE,MAAI,CAAC,OACH,OAAM,IAAI,MAAM,iCAAiC;AAGnD,OAAK,SAAS;AACd,OAAK,YAAY,QAAQ,SAAS,QAAQ,aAAa,KAAK;AAC5D,OAAK,QAAQ,KAAK;AAClB,OAAK,UAAU,QAAQ;AACvB,OAAK,YAAY,QAAQ,aAAa,KAAK;AAC3C,OAAK,gBAAgB,QAAQ,iBAAiB,KAAK;;CAGrD,mBAA2B;AACzB,SAAO;GACL,QAA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