UNPKG

@langchain/azure-openai

Version:

Azure SDK for OpenAI integrations for LangChain.js

160 lines (159 loc) 6.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AzureOpenAIEmbeddings = void 0; const embeddings_1 = require("@langchain/core/embeddings"); const openai_1 = require("@azure/openai"); const core_auth_1 = require("@azure/core-auth"); const env_1 = require("@langchain/core/utils/env"); const chunk_array_1 = require("@langchain/core/utils/chunk_array"); const constants_js_1 = require("./constants.cjs"); /** @deprecated Import from "@langchain/openai" instead. */ class AzureOpenAIEmbeddings extends embeddings_1.Embeddings { constructor(fields) { const fieldsWithDefaults = { maxConcurrency: 2, ...fields }; super(fieldsWithDefaults); Object.defineProperty(this, "modelName", { enumerable: true, configurable: true, writable: true, value: "text-embedding-ada-002" }); Object.defineProperty(this, "model", { enumerable: true, configurable: true, writable: true, value: "text-embedding-ada-002" }); Object.defineProperty(this, "batchSize", { enumerable: true, configurable: true, writable: true, value: 512 }); Object.defineProperty(this, "stripNewLines", { enumerable: true, configurable: true, writable: true, value: false }); Object.defineProperty(this, "timeout", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "user", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "azureOpenAIApiKey", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "apiKey", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "azureOpenAIEndpoint", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "azureOpenAIApiDeploymentName", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "client", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.azureOpenAIApiDeploymentName = (fieldsWithDefaults?.azureOpenAIEmbeddingsApiDeploymentName || fieldsWithDefaults?.azureOpenAIApiDeploymentName) ?? ((0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME") || (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_DEPLOYMENT_NAME")); this.azureOpenAIEndpoint = fields?.azureOpenAIEndpoint ?? (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_ENDPOINT"); const openAiApiKey = fields?.apiKey ?? fields?.openAIApiKey ?? (0, env_1.getEnvironmentVariable)("OPENAI_API_KEY"); this.azureOpenAIApiKey = fields?.apiKey ?? fields?.azureOpenAIApiKey ?? (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY") ?? openAiApiKey; this.apiKey = this.azureOpenAIApiKey; const azureCredential = fields?.credentials ?? (this.apiKey === openAiApiKey ? new openai_1.OpenAIKeyCredential(this.apiKey ?? "") : new openai_1.AzureKeyCredential(this.apiKey ?? "")); // eslint-disable-next-line no-instanceof/no-instanceof const isOpenAIApiKey = azureCredential instanceof openai_1.OpenAIKeyCredential; if (!this.apiKey && !fields?.credentials) { throw new Error("Azure OpenAI API key not found"); } if (!this.azureOpenAIEndpoint && !isOpenAIApiKey) { throw new Error("Azure OpenAI Endpoint not found"); } if (!this.azureOpenAIApiDeploymentName && !isOpenAIApiKey) { throw new Error("Azure OpenAI Deployment name not found"); } this.modelName = fieldsWithDefaults?.model ?? fieldsWithDefaults?.modelName ?? this.model; this.model = this.modelName; this.batchSize = fieldsWithDefaults?.batchSize ?? (this.apiKey ? 1 : this.batchSize); this.stripNewLines = fieldsWithDefaults?.stripNewLines ?? this.stripNewLines; this.timeout = fieldsWithDefaults?.timeout; const options = { userAgentOptions: { userAgentPrefix: constants_js_1.USER_AGENT_PREFIX }, }; if (isOpenAIApiKey) { this.client = new openai_1.OpenAIClient(azureCredential); } else if ((0, core_auth_1.isTokenCredential)(azureCredential)) { this.client = new openai_1.OpenAIClient(this.azureOpenAIEndpoint ?? "", azureCredential, options); } else { this.client = new openai_1.OpenAIClient(this.azureOpenAIEndpoint ?? "", azureCredential, options); } } async embedDocuments(texts) { const batches = (0, chunk_array_1.chunkArray)(this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, this.batchSize); const batchRequests = batches.map((batch) => this.getEmbeddings(batch)); const embeddings = await Promise.all(batchRequests); return embeddings.flat(); } async embedQuery(document) { const input = [ this.stripNewLines ? document.replace(/\n/g, " ") : document, ]; const embeddings = await this.getEmbeddings(input); return embeddings.flat(); } async getEmbeddings(input) { const deploymentName = this.azureOpenAIApiDeploymentName || this.model; const res = await this.caller.call(() => this.client.getEmbeddings(deploymentName, input, { user: this.user, model: this.model, requestOptions: { timeout: this.timeout, }, })); return res.data.map((data) => data.embedding); } } exports.AzureOpenAIEmbeddings = AzureOpenAIEmbeddings;