@langchain/community
Version:
Third-party integrations for LangChain.js
1 lines • 6.22 kB
Source Map (JSON)
{"version":3,"file":"fireworks.cjs","names":["Embeddings"],"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 // oxlint-disable-next-line typescript/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,2BAAAA,WAEV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