@langchain/community
Version:
Third-party integrations for LangChain.js
101 lines (100 loc) • 3.71 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
let _langchain_core_utils_env = require("@langchain/core/utils/env");
let _langchain_core_utils_chunk_array = require("@langchain/core/utils/chunk_array");
let _langchain_core_embeddings = require("@langchain/core/embeddings");
//#region src/embeddings/fireworks.ts
var fireworks_exports = /* @__PURE__ */ require_runtime.__exportAll({ FireworksEmbeddings: () => FireworksEmbeddings });
/**
* A class for generating embeddings using the Fireworks AI API.
*/
var FireworksEmbeddings = class extends _langchain_core_embeddings.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 || (0, _langchain_core_utils_env.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 = (0, _langchain_core_utils_chunk_array.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
exports.FireworksEmbeddings = FireworksEmbeddings;
Object.defineProperty(exports, "fireworks_exports", {
enumerable: true,
get: function() {
return fireworks_exports;
}
});
//# sourceMappingURL=fireworks.cjs.map