@langchain/community
Version:
Third-party integrations for LangChain.js
1 lines • 6.64 kB
Source Map (JSON)
{"version":3,"file":"bytedance_doubao.cjs","names":["Embeddings"],"sources":["../../src/embeddings/bytedance_doubao.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\nexport interface ByteDanceDoubaoEmbeddingsParams extends EmbeddingsParams {\n /** Model name to use */\n model: string;\n\n /**\n * Timeout to use when making requests to ByteDanceDoubao.\n */\n timeout?: number;\n\n /**\n * The maximum number of documents to embed in a single request. This is\n * limited by the ByteDanceDoubao API to a maximum of 2048.\n */\n batchSize?: number;\n\n /**\n * Whether to strip new lines from the input text.\n */\n stripNewLines?: boolean;\n}\n\ninterface EmbeddingCreateParams {\n model: ByteDanceDoubaoEmbeddingsParams[\"model\"];\n input: string[];\n encoding_format?: \"float\";\n}\n\ninterface EmbeddingResponse {\n data: {\n index: number;\n embedding: number[];\n }[];\n\n usage: {\n prompt_tokens: number;\n total_tokens: number;\n };\n\n id: string;\n}\n\ninterface EmbeddingErrorResponse {\n type: string;\n code: string;\n param: string;\n message: string;\n}\n\nexport class ByteDanceDoubaoEmbeddings\n extends Embeddings\n implements ByteDanceDoubaoEmbeddingsParams\n{\n model: string;\n\n batchSize = 24;\n\n stripNewLines = true;\n\n apiKey: string;\n\n constructor(\n fields?: Partial<ByteDanceDoubaoEmbeddingsParams> & {\n verbose?: boolean;\n apiKey?: string;\n }\n ) {\n const fieldsWithDefaults = { maxConcurrency: 2, ...fields };\n super(fieldsWithDefaults);\n\n const apiKey =\n fieldsWithDefaults?.apiKey ?? getEnvironmentVariable(\"ARK_API_KEY\");\n\n if (!apiKey) throw new Error(\"ByteDanceDoubao API key not found\");\n\n this.apiKey = apiKey;\n\n this.model = fieldsWithDefaults?.model ?? this.model;\n this.batchSize = fieldsWithDefaults?.batchSize ?? this.batchSize;\n this.stripNewLines =\n fieldsWithDefaults?.stripNewLines ?? this.stripNewLines;\n }\n\n /**\n * Method to generate embeddings for an array of documents. Splits the\n * documents into batches and makes requests to the ByteDanceDoubao API to generate\n * embeddings.\n * @param texts Array of documents to generate embeddings for.\n * @returns Promise that resolves to a 2D array of embeddings for each document.\n */\n async embedDocuments(texts: string[]): Promise<number[][]> {\n const batches = chunkArray(\n this.stripNewLines ? texts.map((t) => t.replace(/\\n/g, \" \")) : texts,\n this.batchSize\n );\n const batchRequests = batches.map((batch) => {\n const params = this.getParams(batch);\n\n return this.embeddingWithRetry(params);\n });\n\n const batchResponses = await Promise.all(batchRequests);\n const embeddings: number[][] = [];\n\n for (let i = 0; i < batchResponses.length; i += 1) {\n const batch = batches[i];\n const batchResponse = batchResponses[i] || [];\n for (let j = 0; j < batch.length; j += 1) {\n embeddings.push(batchResponse[j]);\n }\n }\n\n return embeddings;\n }\n\n /**\n * Method to generate an embedding for a single document. Calls the\n * embeddingWithRetry method with the document as the input.\n * @param text Document to generate an embedding for.\n * @returns Promise that resolves to an embedding for the document.\n */\n async embedQuery(text: string): Promise<number[]> {\n const params = this.getParams([\n this.stripNewLines ? text.replace(/\\n/g, \" \") : text,\n ]);\n\n const embeddings = (await this.embeddingWithRetry(params)) || [[]];\n return embeddings[0];\n }\n\n /**\n * Method to generate an embedding params.\n * @param texts Array of documents to generate embeddings for.\n * @returns an embedding params.\n */\n private getParams(\n texts: EmbeddingCreateParams[\"input\"]\n ): EmbeddingCreateParams {\n return {\n model: this.model,\n input: texts,\n };\n }\n\n /**\n * Private method to make a request to the OpenAI API to generate\n * embeddings. Handles the retry logic and returns the response from the\n * API.\n * @param request Request to send to the OpenAI API.\n * @returns Promise that resolves to the response from the API.\n */\n private async embeddingWithRetry(body: EmbeddingCreateParams) {\n return fetch(\"https://ark.cn-beijing.volces.com/api/v3/embeddings\", {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.apiKey}`,\n },\n body: JSON.stringify(body),\n }).then(async (response) => {\n const embeddingData: EmbeddingResponse | EmbeddingErrorResponse =\n await response.json();\n\n if (\"code\" in embeddingData && embeddingData.code) {\n throw new Error(`${embeddingData.code}: ${embeddingData.message}`);\n }\n\n return (embeddingData as EmbeddingResponse).data.map(\n ({ embedding }) => embedding\n );\n });\n }\n}\n"],"mappings":";;;;;;;AAoDA,IAAa,4BAAb,cACUA,2BAAAA,WAEV;CACE;CAEA,YAAY;CAEZ,gBAAgB;CAEhB;CAEA,YACE,QAIA;EACA,MAAM,qBAAqB;GAAE,gBAAgB;GAAG,GAAG;GAAQ;AAC3D,QAAM,mBAAmB;EAEzB,MAAM,SACJ,oBAAoB,WAAA,GAAA,0BAAA,wBAAiC,cAAc;AAErE,MAAI,CAAC,OAAQ,OAAM,IAAI,MAAM,oCAAoC;AAEjE,OAAK,SAAS;AAEd,OAAK,QAAQ,oBAAoB,SAAS,KAAK;AAC/C,OAAK,YAAY,oBAAoB,aAAa,KAAK;AACvD,OAAK,gBACH,oBAAoB,iBAAiB,KAAK;;;;;;;;;CAU9C,MAAM,eAAe,OAAsC;EACzD,MAAM,WAAA,GAAA,kCAAA,YACJ,KAAK,gBAAgB,MAAM,KAAK,MAAM,EAAE,QAAQ,OAAO,IAAI,CAAC,GAAG,OAC/D,KAAK,UACN;EACD,MAAM,gBAAgB,QAAQ,KAAK,UAAU;GAC3C,MAAM,SAAS,KAAK,UAAU,MAAM;AAEpC,UAAO,KAAK,mBAAmB,OAAO;IACtC;EAEF,MAAM,iBAAiB,MAAM,QAAQ,IAAI,cAAc;EACvD,MAAM,aAAyB,EAAE;AAEjC,OAAK,IAAI,IAAI,GAAG,IAAI,eAAe,QAAQ,KAAK,GAAG;GACjD,MAAM,QAAQ,QAAQ;GACtB,MAAM,gBAAgB,eAAe,MAAM,EAAE;AAC7C,QAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,EACrC,YAAW,KAAK,cAAc,GAAG;;AAIrC,SAAO;;;;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