UNPKG

@langchain/openai

Version:
168 lines (167 loc) 6.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AzureOpenAI = void 0; const openai_1 = require("openai"); const env_1 = require("@langchain/core/utils/env"); const llms_js_1 = require("../llms.cjs"); const azure_js_1 = require("../utils/azure.cjs"); class AzureOpenAI extends llms_js_1.OpenAI { get lc_aliases() { return { ...super.lc_aliases, openAIApiKey: "openai_api_key", openAIApiVersion: "openai_api_version", openAIBasePath: "openai_api_base", deploymentName: "deployment_name", azureOpenAIEndpoint: "azure_endpoint", azureOpenAIApiVersion: "openai_api_version", azureOpenAIBasePath: "openai_api_base", azureOpenAIApiDeploymentName: "deployment_name", }; } get lc_secrets() { return { ...super.lc_secrets, azureOpenAIApiKey: "AZURE_OPENAI_API_KEY", }; } 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 }); Object.defineProperty(this, "azureOpenAIEndpoint", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.azureOpenAIApiDeploymentName = (fields?.azureOpenAIApiCompletionsDeploymentName || fields?.azureOpenAIApiDeploymentName) ?? ((0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME") || (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_DEPLOYMENT_NAME")); this.azureOpenAIApiKey = fields?.azureOpenAIApiKey ?? fields?.openAIApiKey ?? fields?.apiKey ?? (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY"); this.azureOpenAIApiInstanceName = fields?.azureOpenAIApiInstanceName ?? (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_INSTANCE_NAME"); 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.azureOpenAIEndpoint = fields?.azureOpenAIEndpoint ?? (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_ENDPOINT"); this.azureADTokenProvider = fields?.azureADTokenProvider; if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) { throw new Error("Azure OpenAI API key or Token Provider not found"); } } _getClientOptions(options) { 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, ...params, }); } const requestOptions = { ...this.clientConfig, ...options, }; if (this.azureOpenAIApiKey) { requestOptions.headers = { "api-key": this.azureOpenAIApiKey, ...requestOptions.headers, }; requestOptions.query = { "api-version": this.azureOpenAIApiVersion, ...requestOptions.query, }; } return requestOptions; } // eslint-disable-next-line @typescript-eslint/no-explicit-any toJSON() { const json = super.toJSON(); function isRecord(obj) { return typeof obj === "object" && obj != null; } if (isRecord(json) && isRecord(json.kwargs)) { delete json.kwargs.azure_openai_base_path; delete json.kwargs.azure_openai_api_deployment_name; delete json.kwargs.azure_openai_api_key; delete json.kwargs.azure_openai_api_version; delete json.kwargs.azure_open_ai_base_path; } return json; } } exports.AzureOpenAI = AzureOpenAI;