@langchain/community
Version:
Third-party integrations for LangChain.js
1 lines • 8.1 kB
Source Map (JSON)
{"version":3,"file":"voyage.cjs","names":["Embeddings","fields?: Partial<VoyageEmbeddingsParams> & {\n verbose?: boolean;\n apiKey?: string;\n inputType?: string;\n }","texts: string[]","embeddings: number[][]","text: string","request: CreateVoyageEmbeddingRequest"],"sources":["../../src/embeddings/voyage.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 VoyageEmbeddings class.\n */\nexport interface VoyageEmbeddingsParams extends EmbeddingsParams {\n modelName: string;\n\n /**\n * The maximum number of documents to embed in a single request. This is\n * limited by the Voyage AI API to a maximum of 8.\n */\n batchSize?: number;\n\n /**\n * Input type for the embeddings request.\n */\n inputType?: string;\n\n /**\n * Whether to truncate the input texts to the maximum length allowed by the model.\n */\n truncation?: boolean;\n\n /**\n * The desired dimension of the output embeddings.\n */\n outputDimension?: number;\n\n /**\n * The data type of the output embeddings. Can be \"float\" or \"int8\".\n */\n outputDtype?: string;\n\n /**\n * The format of the output embeddings. Can be \"float\", \"base64\", or \"ubinary\".\n */\n encodingFormat?: string;\n}\n\n/**\n * Interface for the request body to generate embeddings.\n */\nexport interface CreateVoyageEmbeddingRequest {\n /**\n * @type {string}\n * @memberof CreateVoyageEmbeddingRequest\n */\n model: string;\n\n /**\n * Text to generate vector expectation\n * @type {CreateEmbeddingRequestInput}\n * @memberof CreateVoyageEmbeddingRequest\n */\n input: string | string[];\n\n /**\n * Input type for the embeddings request.\n */\n input_type?: string;\n\n /**\n * Whether to truncate the input texts.\n */\n truncation?: boolean;\n\n /**\n * The desired dimension of the output embeddings.\n */\n output_dimension?: number;\n\n /**\n * The data type of the output embeddings.\n */\n output_dtype?: string;\n\n /**\n * The format of the output embeddings.\n */\n encoding_format?: string;\n}\n\n/**\n * A class for generating embeddings using the Voyage AI API.\n */\nexport class VoyageEmbeddings\n extends Embeddings\n implements VoyageEmbeddingsParams\n{\n modelName = \"voyage-01\";\n\n batchSize = 8;\n\n private apiKey: string;\n\n basePath?: string = \"https://api.voyageai.com/v1\";\n\n apiUrl: string;\n\n headers?: Record<string, string>;\n\n inputType?: string;\n\n truncation?: boolean;\n\n outputDimension?: number;\n\n outputDtype?: string;\n\n encodingFormat?: string;\n\n /**\n * Constructor for the VoyageEmbeddings class.\n * @param fields - An optional object with properties to configure the instance.\n */\n constructor(\n fields?: Partial<VoyageEmbeddingsParams> & {\n verbose?: boolean;\n apiKey?: string;\n inputType?: string;\n }\n ) {\n const fieldsWithDefaults = { ...fields };\n\n super(fieldsWithDefaults);\n\n const apiKey =\n fieldsWithDefaults?.apiKey || getEnvironmentVariable(\"VOYAGEAI_API_KEY\");\n\n if (!apiKey) {\n throw new Error(\"Voyage AI API key not found\");\n }\n\n this.modelName = fieldsWithDefaults?.modelName ?? this.modelName;\n this.batchSize = fieldsWithDefaults?.batchSize ?? this.batchSize;\n this.apiKey = apiKey;\n this.apiUrl = `${this.basePath}/embeddings`;\n this.inputType = fieldsWithDefaults?.inputType;\n this.truncation = fieldsWithDefaults?.truncation;\n this.outputDimension = fieldsWithDefaults?.outputDimension;\n this.outputDtype = fieldsWithDefaults?.outputDtype;\n this.encodingFormat = fieldsWithDefaults?.encodingFormat;\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.modelName,\n input: batch,\n input_type: this.inputType,\n truncation: this.truncation,\n output_dimension: this.outputDimension,\n output_dtype: this.outputDtype,\n encoding_format: this.encodingFormat,\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.modelName,\n input: text,\n input_type: this.inputType,\n truncation: this.truncation,\n output_dimension: this.outputDimension,\n output_dtype: this.outputDtype,\n encoding_format: this.encodingFormat,\n });\n\n return data[0].embedding;\n }\n\n /**\n * Makes a request to the Voyage 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 Voyage AI API.\n */\n private async embeddingWithRetry(request: CreateVoyageEmbeddingRequest) {\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 const json = await response.json();\n return json;\n };\n\n return this.caller.call(makeCompletionRequest);\n }\n}\n"],"mappings":";;;;;;;;;;;AAyFA,IAAa,mBAAb,cACUA,uCAEV;CACE,YAAY;CAEZ,YAAY;CAEZ,AAAQ;CAER,WAAoB;CAEpB;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA;;;;;CAMA,YACEC,QAKA;EACA,MAAM,qBAAqB,EAAE,GAAG,OAAQ;EAExC,MAAM,mBAAmB;EAEzB,MAAM,SACJ,oBAAoB,iEAAiC,mBAAmB;AAE1E,MAAI,CAAC,OACH,OAAM,IAAI,MAAM;EAGlB,KAAK,YAAY,oBAAoB,aAAa,KAAK;EACvD,KAAK,YAAY,oBAAoB,aAAa,KAAK;EACvD,KAAK,SAAS;EACd,KAAK,SAAS,GAAG,KAAK,SAAS,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