UNPKG

@langchain/openai

Version:
1 lines 8.98 kB
{"version":3,"file":"azure.cjs","names":["config: OpenAIEndpointConfig","headers: unknown","headers: HeadersLike","iife","entries: [string, string][]"],"sources":["../../src/utils/azure.ts"],"sourcesContent":["import { getEnv } from \"@langchain/core/utils/env\";\nimport { iife } from \"./misc.js\";\n\nexport interface OpenAIEndpointConfig {\n azureOpenAIApiDeploymentName?: string;\n azureOpenAIApiInstanceName?: string;\n azureOpenAIApiKey?: string;\n azureADTokenProvider?: () => Promise<string>;\n azureOpenAIBasePath?: string;\n baseURL?: string | null;\n azureOpenAIEndpoint?: string;\n}\n\n/**\n * This function generates an endpoint URL for (Azure) OpenAI\n * based on the configuration parameters provided.\n *\n * @param {OpenAIEndpointConfig} config - The configuration object for the (Azure) endpoint.\n *\n * @property {string} config.azureOpenAIApiDeploymentName - The deployment name of Azure OpenAI.\n * @property {string} config.azureOpenAIApiInstanceName - The instance name of Azure OpenAI, e.g. `example-resource`.\n * @property {string} config.azureOpenAIApiKey - The API Key for Azure OpenAI.\n * @property {string} config.azureOpenAIBasePath - The base path for Azure OpenAI, e.g. `https://example-resource.azure.openai.com/openai/deployments/`.\n * @property {string} config.baseURL - Some other custom base path URL.\n * @property {string} config.azureOpenAIEndpoint - The endpoint for the Azure OpenAI instance, e.g. `https://example-resource.azure.openai.com/`.\n *\n * The function operates as follows:\n * - If both `azureOpenAIBasePath` and `azureOpenAIApiDeploymentName` (plus `azureOpenAIApiKey`) are provided, it returns an URL combining these two parameters (`${azureOpenAIBasePath}/${azureOpenAIApiDeploymentName}`).\n * - If both `azureOpenAIEndpoint` and `azureOpenAIApiDeploymentName` (plus `azureOpenAIApiKey`) are provided, it returns an URL combining these two parameters (`${azureOpenAIEndpoint}/openai/deployments/${azureOpenAIApiDeploymentName}`).\n * - If `azureOpenAIApiKey` is provided, it checks for `azureOpenAIApiInstanceName` and `azureOpenAIApiDeploymentName` and throws an error if any of these is missing. If both are provided, it generates an URL incorporating these parameters.\n * - If none of the above conditions are met, return any custom `baseURL`.\n * - The function returns the generated URL as a string, or undefined if no custom paths are specified.\n *\n * @throws Will throw an error if the necessary parameters for generating the URL are missing.\n *\n * @returns {string | undefined} The generated (Azure) OpenAI endpoint URL.\n */\nexport function getEndpoint(config: OpenAIEndpointConfig) {\n const {\n azureOpenAIApiDeploymentName,\n azureOpenAIApiInstanceName,\n azureOpenAIApiKey,\n azureOpenAIBasePath,\n baseURL,\n azureADTokenProvider,\n azureOpenAIEndpoint,\n } = config;\n\n if (\n (azureOpenAIApiKey || azureADTokenProvider) &&\n azureOpenAIBasePath &&\n azureOpenAIApiDeploymentName\n ) {\n return `${azureOpenAIBasePath}/${azureOpenAIApiDeploymentName}`;\n }\n if (\n (azureOpenAIApiKey || azureADTokenProvider) &&\n azureOpenAIEndpoint &&\n azureOpenAIApiDeploymentName\n ) {\n return `${azureOpenAIEndpoint}/openai/deployments/${azureOpenAIApiDeploymentName}`;\n }\n\n if (azureOpenAIApiKey || azureADTokenProvider) {\n if (!azureOpenAIApiInstanceName) {\n throw new Error(\n \"azureOpenAIApiInstanceName is required when using azureOpenAIApiKey\"\n );\n }\n if (!azureOpenAIApiDeploymentName) {\n throw new Error(\n \"azureOpenAIApiDeploymentName is a required parameter when using azureOpenAIApiKey\"\n );\n }\n return `https://${azureOpenAIApiInstanceName}.openai.azure.com/openai/deployments/${azureOpenAIApiDeploymentName}`;\n }\n\n return baseURL;\n}\n\ntype HeaderValue = string | undefined | null;\nexport type HeadersLike =\n | Headers\n | readonly HeaderValue[][]\n | Record<string, HeaderValue | readonly HeaderValue[]>\n | undefined\n | null\n // NullableHeaders\n | { values: Headers; [key: string]: unknown };\n\nexport function isHeaders(headers: unknown): headers is Headers {\n return (\n typeof Headers !== \"undefined\" &&\n headers !== null &&\n typeof headers === \"object\" &&\n Object.prototype.toString.call(headers) === \"[object Headers]\"\n );\n}\n\n/**\n * Normalizes various header formats into a consistent Record format.\n *\n * This function accepts headers in multiple formats and converts them to a\n * Record<string, HeaderValue | readonly HeaderValue[]> for consistent handling.\n *\n * @param headers - The headers to normalize. Can be:\n * - A Headers instance\n * - An array of [key, value] pairs\n * - A plain object with string keys\n * - A NullableHeaders-like object with a 'values' property containing Headers\n * - null or undefined\n * @returns A normalized Record containing the header key-value pairs\n *\n * @example\n * ```ts\n * // With Headers instance\n * const headers1 = new Headers([['content-type', 'application/json']]);\n * const normalized1 = normalizeHeaders(headers1);\n *\n * // With plain object\n * const headers2 = { 'content-type': 'application/json' };\n * const normalized2 = normalizeHeaders(headers2);\n *\n * // With array of pairs\n * const headers3 = [['content-type', 'application/json']];\n * const normalized3 = normalizeHeaders(headers3);\n * ```\n */\nexport function normalizeHeaders(\n headers: HeadersLike\n): Record<string, HeaderValue | readonly HeaderValue[]> {\n const output = iife(() => {\n // If headers is a Headers instance\n if (isHeaders(headers)) {\n return headers;\n }\n // If headers is an array of [key, value] pairs\n else if (Array.isArray(headers)) {\n return new Headers(headers);\n }\n // If headers is a NullableHeaders-like object (has 'values' property that is a Headers)\n else if (\n typeof headers === \"object\" &&\n headers !== null &&\n \"values\" in headers &&\n isHeaders(headers.values)\n ) {\n return headers.values;\n }\n // If headers is a plain object\n else if (typeof headers === \"object\" && headers !== null) {\n const entries: [string, string][] = Object.entries(headers)\n .filter(([, v]) => typeof v === \"string\")\n .map(([k, v]) => [k, v as string]);\n return new Headers(entries);\n }\n return new Headers();\n });\n\n return Object.fromEntries(output.entries());\n}\n\nexport function getFormattedEnv() {\n let env = getEnv();\n if (env === \"node\" || env === \"deno\") {\n env = `(${env}/${process.version}; ${process.platform}; ${process.arch})`;\n }\n return env;\n}\n\n// Note: ideally version would be imported from package.json, but there's\n// currently no good way to do that for all supported environments (Node, Deno, Browser).\nexport function getHeadersWithUserAgent(\n headers: HeadersLike,\n isAzure = false,\n version = \"1.0.0\"\n): Record<string, string> {\n const normalizedHeaders = normalizeHeaders(headers);\n const env = getFormattedEnv();\n const library = `langchainjs${isAzure ? \"-azure\" : \"\"}-openai`;\n return {\n ...normalizedHeaders,\n \"User-Agent\": normalizedHeaders[\"User-Agent\"]\n ? `${library}/${version} (${env})${normalizedHeaders[\"User-Agent\"]}`\n : `${library}/${version} (${env})`,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,YAAYA,QAA8B;CACxD,MAAM,EACJ,8BACA,4BACA,mBACA,qBACA,SACA,sBACA,qBACD,GAAG;AAEJ,MACG,qBAAqB,yBACtB,uBACA,6BAEA,QAAO,GAAG,oBAAoB,CAAC,EAAE,8BAA8B;AAEjE,MACG,qBAAqB,yBACtB,uBACA,6BAEA,QAAO,GAAG,oBAAoB,oBAAoB,EAAE,8BAA8B;AAGpF,KAAI,qBAAqB,sBAAsB;AAC7C,MAAI,CAAC,2BACH,OAAM,IAAI,MACR;AAGJ,MAAI,CAAC,6BACH,OAAM,IAAI,MACR;AAGJ,SAAO,CAAC,QAAQ,EAAE,2BAA2B,qCAAqC,EAAE,8BAA8B;CACnH;AAED,QAAO;AACR;AAYD,SAAgB,UAAUC,SAAsC;AAC9D,QACE,OAAO,YAAY,eACnB,YAAY,QACZ,OAAO,YAAY,YACnB,OAAO,UAAU,SAAS,KAAK,QAAQ,KAAK;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BD,SAAgB,iBACdC,SACsD;CACtD,MAAM,SAASC,kBAAK,MAAM;AAExB,MAAI,UAAU,QAAQ,CACpB,QAAO;WAGA,MAAM,QAAQ,QAAQ,CAC7B,QAAO,IAAI,QAAQ;WAInB,OAAO,YAAY,YACnB,YAAY,QACZ,YAAY,WACZ,UAAU,QAAQ,OAAO,CAEzB,QAAO,QAAQ;WAGR,OAAO,YAAY,YAAY,YAAY,MAAM;GACxD,MAAMC,UAA8B,OAAO,QAAQ,QAAQ,CACxD,OAAO,CAAC,GAAG,EAAE,KAAK,OAAO,MAAM,SAAS,CACxC,IAAI,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,CAAY,EAAC;AACpC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