UNPKG

@langchain/openai

Version:
130 lines (129 loc) 5.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AzureOpenAIEmbeddings = void 0; const openai_1 = require("openai"); const env_1 = require("@langchain/core/utils/env"); const embeddings_js_1 = require("../embeddings.cjs"); const azure_js_1 = require("../utils/azure.cjs"); const openai_js_1 = require("../utils/openai.cjs"); class AzureOpenAIEmbeddings extends embeddings_js_1.OpenAIEmbeddings { constructor(fields) { super(fields); Object.defineProperty(this, "azureOpenAIApiVersion", { 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, "azureADTokenProvider", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "azureOpenAIApiInstanceName", { 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, "azureOpenAIBasePath", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.batchSize = fields?.batchSize ?? 1; this.azureOpenAIApiKey = fields?.azureOpenAIApiKey ?? fields?.apiKey ?? (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY"); this.azureOpenAIApiVersion = fields?.azureOpenAIApiVersion ?? fields?.openAIApiVersion ?? (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_VERSION"); this.azureOpenAIBasePath = fields?.azureOpenAIBasePath ?? (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_BASE_PATH"); this.azureOpenAIApiInstanceName = fields?.azureOpenAIApiInstanceName ?? (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_INSTANCE_NAME"); this.azureOpenAIApiDeploymentName = (fields?.azureOpenAIApiEmbeddingsDeploymentName || fields?.azureOpenAIApiDeploymentName) ?? ((0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME") || (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_DEPLOYMENT_NAME")); this.azureADTokenProvider = fields?.azureADTokenProvider; } async embeddingWithRetry(request) { if (!this.client) { const openAIEndpointConfig = { azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName, azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName, azureOpenAIApiKey: this.azureOpenAIApiKey, azureOpenAIBasePath: this.azureOpenAIBasePath, azureADTokenProvider: this.azureADTokenProvider, baseURL: this.clientConfig.baseURL, }; const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig); const params = { ...this.clientConfig, baseURL: endpoint, timeout: this.timeout, maxRetries: 0, }; if (!this.azureADTokenProvider) { params.apiKey = openAIEndpointConfig.azureOpenAIApiKey; } if (!params.baseURL) { delete params.baseURL; } params.defaultHeaders = { ...params.defaultHeaders, "User-Agent": params.defaultHeaders?.["User-Agent"] ? `${params.defaultHeaders["User-Agent"]}: langchainjs-azure-openai-v2` : `langchainjs-azure-openai-v2`, }; this.client = new openai_1.AzureOpenAI({ apiVersion: this.azureOpenAIApiVersion, azureADTokenProvider: this.azureADTokenProvider, deployment: this.azureOpenAIApiDeploymentName, ...params, }); } const requestOptions = {}; if (this.azureOpenAIApiKey) { requestOptions.headers = { "api-key": this.azureOpenAIApiKey, ...requestOptions.headers, }; requestOptions.query = { "api-version": this.azureOpenAIApiVersion, ...requestOptions.query, }; } return this.caller.call(async () => { try { const res = await this.client.embeddings.create(request, requestOptions); return res; } catch (e) { const error = (0, openai_js_1.wrapOpenAIClientError)(e); throw error; } }); } } exports.AzureOpenAIEmbeddings = AzureOpenAIEmbeddings;