{"version":3,"file":"fireworks.cjs","names":["Embeddings","fields?: Partial<FireworksEmbeddingsParams> & {\n      verbose?: boolean;\n      apiKey?: string;\n    }","texts: string[]","embeddings: number[][]","text: string","request: CreateFireworksEmbeddingRequest"],"sources":["../../src/embeddings/fireworks.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 that extends EmbeddingsParams and defines additional\n * parameters specific to the FireworksEmbeddings class.\n */\nexport interface FireworksEmbeddingsParams extends EmbeddingsParams {\n  /**\n   * Model name to use.\n   */\n  model: string;\n\n  /**\n   * The maximum number of documents to embed in a single request. This is\n   * limited by the Fireworks AI API to a maximum of 8.\n   */\n  batchSize?: number;\n}\n\n/**\n * Interface for the request body to generate embeddings.\n */\nexport interface CreateFireworksEmbeddingRequest {\n  /**\n   * @type {string}\n   * @memberof CreateFireworksEmbeddingRequest\n   */\n  model: string;\n\n  /**\n   *  Text to generate vector expectation\n   * @type {CreateEmbeddingRequestInput}\n   * @memberof CreateFireworksEmbeddingRequest\n   */\n  input: string | string[];\n}\n\n/**\n * A class for generating embeddings using the Fireworks AI API.\n */\nexport class FireworksEmbeddings\n  extends Embeddings\n  implements FireworksEmbeddingsParams\n{\n  model = \"nomic-ai/nomic-embed-text-v1.5\";\n\n  batchSize = 8;\n\n  private apiKey: string;\n\n  basePath?: string = \"https://api.fireworks.ai/inference/v1\";\n\n  apiUrl: string;\n\n  headers?: Record<string, string>;\n\n  /**\n   * Constructor for the FireworksEmbeddings class.\n   * @param fields - An optional object with properties to configure the instance.\n   */\n  constructor(\n    fields?: Partial<FireworksEmbeddingsParams> & {\n      verbose?: boolean;\n      apiKey?: string;\n    }\n  ) {\n    const fieldsWithDefaults = { ...fields };\n\n    super(fieldsWithDefaults);\n\n    const apiKey =\n      fieldsWithDefaults?.apiKey || getEnvironmentVariable(\"FIREWORKS_API_KEY\");\n\n    if (!apiKey) {\n      throw new Error(\"Fireworks AI API key not found\");\n    }\n\n    this.model = fieldsWithDefaults?.model ?? this.model;\n    this.batchSize = fieldsWithDefaults?.batchSize ?? this.batchSize;\n    this.apiKey = apiKey;\n    this.apiUrl = `${this.basePath}/embeddings`;\n  }\n\n  /**\n   * Generates embeddings for an array of texts.\n   * @param texts - An array of strings to generate embeddings for.\n   * @returns A Promise that resolves to an array of embeddings.\n   */\n  async embedDocuments(texts: string[]): Promise<number[][]> {\n    const batches = chunkArray(texts, this.batchSize);\n\n    const batchRequests = batches.map((batch) =>\n      this.embeddingWithRetry({\n        model: this.model,\n        input: batch,\n      })\n    );\n\n    const batchResponses = await Promise.all(batchRequests);\n\n    const embeddings: number[][] = [];\n\n    for (let i = 0; i < batchResponses.length; i += 1) {\n      const batch = batches[i];\n      const { data: batchResponse } = batchResponses[i];\n      for (let j = 0; j < batch.length; j += 1) {\n        embeddings.push(batchResponse[j].embedding);\n      }\n    }\n\n    return embeddings;\n  }\n\n  /**\n   * Generates an embedding for a single text.\n   * @param text - A string to generate an embedding for.\n   * @returns A Promise that resolves to an array of numbers representing the embedding.\n   */\n  async embedQuery(text: string): Promise<number[]> {\n    const { data } = await this.embeddingWithRetry({\n      model: this.model,\n      input: text,\n    });\n\n    return data[0].embedding;\n  }\n\n  /**\n   * Makes a request to the Fireworks AI API to generate embeddings for an array of texts.\n   * @param request - An object with properties to configure the request.\n   * @returns A Promise that resolves to the response from the Fireworks AI API.\n   */\n\n  private async embeddingWithRetry(request: CreateFireworksEmbeddingRequest) {\n    const makeCompletionRequest = async () => {\n      const url = `${this.apiUrl}`;\n      const response = await fetch(url, {\n        method: \"POST\",\n        headers: {\n          \"Content-Type\": \"application/json\",\n          Authorization: `Bearer ${this.apiKey}`,\n          ...this.headers,\n        },\n        body: JSON.stringify(request),\n      });\n\n      if (!response.ok) {\n        const { error: message } = await response.json();\n        const error = new Error(\n          `Error ${response.status}: ${message ?? \"Unspecified error\"}`\n        );\n        // eslint-disable-next-line @typescript-eslint/no-explicit-any\n        (error as any).response = response;\n        throw error;\n      }\n\n      const json = await response.json();\n      return json;\n    };\n\n    return this.caller.call(makeCompletionRequest);\n  }\n}\n"],"mappings":";;;;;;;;;;;AA0CA,IAAa,sBAAb,cACUA,uCAEV;CACE,QAAQ;CAER,YAAY;CAEZ,AAAQ;CAER,WAAoB;CAEpB;CAEA;;;;;CAMA,YACEC,QAIA;EACA,MAAM,qBAAqB,EAAE,GAAG,OAAQ;EAExC,MAAM,mBAAmB;EAEzB,MAAM,SACJ,oBAAoB,iEAAiC,oBAAoB;AAE3E,MAAI,CAAC,OACH,OAAM,IAAI,MAAM;EAGlB,KAAK,QAAQ,oBAAoB,SAAS,KAAK;EAC/C,KAAK,YAAY,oBAAoB,aAAa,KAAK;EACvD,KAAK,SAAS;EACd,KAAK,SAAS,GAAG,KAAK,SAAS,WAAW,CAAC;CAC5C;;;;;;CAOD,MAAM,eAAeC,OAAsC;EACzD,MAAM,6DAAqB,OAAO,KAAK,UAAU;EAEjD,MAAM,gBAAgB,QAAQ,IAAI,CAAC,UACjC,KAAK,mBAAmB;GACtB,OAAO,KAAK;GACZ,OAAO;EACR,EAAC,CACH;EAED,MAAM,iBAAiB,MAAM,QAAQ,IAAI,cAAc;EAEvD,MAAMC,aAAyB,CAAE;AAEjC,OAAK,IAAI,IAAI,GAAG,IAAI,eAAe,QAAQ,KAAK,GAAG;GACjD,MAAM,QAAQ,QAAQ;GACtB,MAAM,EAAE,MAAM,eAAe,GAAG,eAAe;AAC/C,QAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,GACrC,WAAW,KAAK,cAAc,GAAG,UAAU;EAE9C;AAED,SAAO;CACR;;;;;;CAOD,MAAM,WAAWC,MAAiC;EAChD,MAAM,EAAE,MAAM,GAAG,MAAM,KAAK,mBAAmB;GAC7C,OAAO,KAAK;GACZ,OAAO;EACR,EAAC;AAEF,SAAO,KAAK,GAAG;CAChB;;;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