UNPKG

@langchain/community

Version:
94 lines (93 loc) 3.35 kB
import { __exportAll } from "../_virtual/_rolldown/runtime.js"; import { getEnvironmentVariable } from "@langchain/core/utils/env"; import { chunkArray } from "@langchain/core/utils/chunk_array"; import { Embeddings } from "@langchain/core/embeddings"; //#region src/embeddings/fireworks.ts var fireworks_exports = /* @__PURE__ */ __exportAll({ FireworksEmbeddings: () => FireworksEmbeddings }); /** * A class for generating embeddings using the Fireworks AI API. */ var FireworksEmbeddings = class extends Embeddings { model = "nomic-ai/nomic-embed-text-v1.5"; batchSize = 8; apiKey; basePath = "https://api.fireworks.ai/inference/v1"; apiUrl; headers; /** * Constructor for the FireworksEmbeddings class. * @param fields - An optional object with properties to configure the instance. */ constructor(fields) { const fieldsWithDefaults = { ...fields }; super(fieldsWithDefaults); const apiKey = fieldsWithDefaults?.apiKey || getEnvironmentVariable("FIREWORKS_API_KEY"); if (!apiKey) throw new Error("Fireworks AI API key not found"); this.model = fieldsWithDefaults?.model ?? this.model; this.batchSize = fieldsWithDefaults?.batchSize ?? this.batchSize; this.apiKey = apiKey; this.apiUrl = `${this.basePath}/embeddings`; } /** * Generates embeddings for an array of texts. * @param texts - An array of strings to generate embeddings for. * @returns A Promise that resolves to an array of embeddings. */ async embedDocuments(texts) { const batches = chunkArray(texts, this.batchSize); const batchRequests = batches.map((batch) => this.embeddingWithRetry({ model: this.model, input: batch })); const batchResponses = await Promise.all(batchRequests); const embeddings = []; for (let i = 0; i < batchResponses.length; i += 1) { const batch = batches[i]; const { data: batchResponse } = batchResponses[i]; for (let j = 0; j < batch.length; j += 1) embeddings.push(batchResponse[j].embedding); } return embeddings; } /** * Generates an embedding for a single text. * @param text - A string to generate an embedding for. * @returns A Promise that resolves to an array of numbers representing the embedding. */ async embedQuery(text) { const { data } = await this.embeddingWithRetry({ model: this.model, input: text }); return data[0].embedding; } /** * Makes a request to the Fireworks AI API to generate embeddings for an array of texts. * @param request - An object with properties to configure the request. * @returns A Promise that resolves to the response from the Fireworks AI API. */ async embeddingWithRetry(request) { const makeCompletionRequest = async () => { const url = `${this.apiUrl}`; const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${this.apiKey}`, ...this.headers }, body: JSON.stringify(request) }); if (!response.ok) { const { error: message } = await response.json(); const error = /* @__PURE__ */ new Error(`Error ${response.status}: ${message ?? "Unspecified error"}`); error.response = response; throw error; } return await response.json(); }; return this.caller.call(makeCompletionRequest); } }; //#endregion export { FireworksEmbeddings, fireworks_exports }; //# sourceMappingURL=fireworks.js.map