|
2 | 2 |
|
3 | 3 | import * as Core from './core';
|
4 | 4 | import * as Errors from './error';
|
5 |
| -import { type Agent } from './_shims/index'; |
| 5 | +import { type Agent, type RequestInit } from './_shims/index'; |
6 | 6 | import * as Uploads from './uploads';
|
7 | 7 | import * as Pagination from 'openai/pagination';
|
8 | 8 | import * as API from 'openai/resources/index';
|
@@ -310,4 +310,187 @@ export namespace OpenAI {
|
310 | 310 | export import FunctionParameters = API.FunctionParameters;
|
311 | 311 | }
|
312 | 312 |
|
| 313 | +// ---------------------- Azure ---------------------- |
| 314 | + |
| 315 | +/** API Client for interfacing with the Azure OpenAI API. */ |
| 316 | +export interface AzureClientOptions extends ClientOptions { |
| 317 | + /** |
| 318 | + * Defaults to process.env['OPENAI_API_VERSION']. |
| 319 | + */ |
| 320 | + apiVersion?: string | undefined; |
| 321 | + |
| 322 | + /** |
| 323 | + * Your Azure endpoint, including the resource, e.g. `https://example-resource.azure.openai.com/` |
| 324 | + */ |
| 325 | + endpoint?: string | undefined; |
| 326 | + |
| 327 | + /** |
| 328 | + * A model deployment, if given, sets the base client URL to include `/deployments/{deployment}`. |
| 329 | + * Note: this means you won't be able to use non-deployment endpoints. Not supported with Assistants APIs. |
| 330 | + */ |
| 331 | + deployment?: string | undefined; |
| 332 | + |
| 333 | + /** |
| 334 | + * Defaults to process.env['AZURE_OPENAI_API_KEY']. |
| 335 | + */ |
| 336 | + apiKey?: string | undefined; |
| 337 | + |
| 338 | + /** |
| 339 | + * A function that returns an access token for Microsoft Entra (formerly known as Azure Active Directory), |
| 340 | + * which will be invoked on every request. |
| 341 | + */ |
| 342 | + azureADTokenProvider?: (() => string) | undefined; |
| 343 | +} |
| 344 | + |
| 345 | +/** API Client for interfacing with the Azure OpenAI API. */ |
| 346 | +export class AzureOpenAI extends OpenAI { |
| 347 | + private _azureADTokenProvider: (() => string) | undefined; |
| 348 | + apiVersion: string = ''; |
| 349 | + /** |
| 350 | + * API Client for interfacing with the Azure OpenAI API. |
| 351 | + * |
| 352 | + * @param {string | undefined} [opts.apiVersion=process.env['OPENAI_API_VERSION'] ?? undefined] |
| 353 | + * @param {string | undefined} [opts.endpoint=process.env['AZURE_OPENAI_ENDPOINT'] ?? undefined] - Your Azure endpoint, including the resource, e.g. `https://example-resource.azure.openai.com/` |
| 354 | + * @param {string | undefined} [opts.apiKey=process.env['AZURE_OPENAI_API_KEY'] ?? undefined] |
| 355 | + * @param {string | undefined} opts.deployment - A model deployment, if given, sets the base client URL to include `/deployments/{deployment}`. |
| 356 | + * @param {string | null | undefined} [opts.organization=process.env['OPENAI_ORG_ID'] ?? null] |
| 357 | + * @param {string} [opts.baseURL=process.env['OPENAI_BASE_URL']] - Sets the base URL for the API. |
| 358 | + * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out. |
| 359 | + * @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections. |
| 360 | + * @param {Core.Fetch} [opts.fetch] - Specify a custom `fetch` function implementation. |
| 361 | + * @param {number} [opts.maxRetries=2] - The maximum number of times the client will retry a request. |
| 362 | + * @param {Core.Headers} opts.defaultHeaders - Default headers to include with every request to the API. |
| 363 | + * @param {Core.DefaultQuery} opts.defaultQuery - Default query parameters to include with every request to the API. |
| 364 | + * @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers. |
| 365 | + */ |
| 366 | + constructor({ |
| 367 | + baseURL = Core.readEnv('OPENAI_BASE_URL'), |
| 368 | + apiKey = Core.readEnv('AZURE_OPENAI_API_KEY'), |
| 369 | + apiVersion = Core.readEnv('OPENAI_API_VERSION'), |
| 370 | + endpoint, |
| 371 | + deployment, |
| 372 | + azureADTokenProvider, |
| 373 | + dangerouslyAllowBrowser, |
| 374 | + ...opts |
| 375 | + }: AzureClientOptions = {}) { |
| 376 | + if (!apiVersion) { |
| 377 | + throw new Errors.OpenAIError( |
| 378 | + "The OPENAI_API_VERSION environment variable is missing or empty; either provide it, or instantiate the AzureOpenAI client with an apiVersion option, like new AzureOpenAI({ apiVersion: 'My API Version' }).", |
| 379 | + ); |
| 380 | + } |
| 381 | + |
| 382 | + if (typeof azureADTokenProvider === 'function') { |
| 383 | + dangerouslyAllowBrowser = true; |
| 384 | + } |
| 385 | + |
| 386 | + if (!azureADTokenProvider && !apiKey) { |
| 387 | + throw new Errors.OpenAIError( |
| 388 | + 'Missing credentials. Please pass one of `apiKey` and `azureADTokenProvider`, or set the `AZURE_OPENAI_API_KEY` environment variable.', |
| 389 | + ); |
| 390 | + } |
| 391 | + |
| 392 | + if (azureADTokenProvider && apiKey) { |
| 393 | + throw new Errors.OpenAIError( |
| 394 | + 'The `apiKey` and `azureADTokenProvider` arguments are mutually exclusive; only one can be passed at a time.', |
| 395 | + ); |
| 396 | + } |
| 397 | + |
| 398 | + // define a sentinel value to avoid any typing issues |
| 399 | + apiKey ??= API_KEY_SENTINEL; |
| 400 | + |
| 401 | + opts.defaultQuery = { ...opts.defaultQuery, 'api-version': apiVersion }; |
| 402 | + |
| 403 | + if (!baseURL) { |
| 404 | + if (!endpoint) { |
| 405 | + endpoint = process.env['AZURE_OPENAI_ENDPOINT']; |
| 406 | + } |
| 407 | + |
| 408 | + if (!endpoint) { |
| 409 | + throw new Errors.OpenAIError( |
| 410 | + 'Must provide one of the `baseURL` or `endpoint` arguments, or the `AZURE_OPENAI_ENDPOINT` environment variable', |
| 411 | + ); |
| 412 | + } |
| 413 | + |
| 414 | + if (deployment) { |
| 415 | + baseURL = `${endpoint}/openai/deployments/${deployment}`; |
| 416 | + } else { |
| 417 | + baseURL = `${endpoint}/openai`; |
| 418 | + } |
| 419 | + } else { |
| 420 | + if (endpoint) { |
| 421 | + throw new Errors.OpenAIError('baseURL and endpoint are mutually exclusive'); |
| 422 | + } |
| 423 | + } |
| 424 | + |
| 425 | + super({ |
| 426 | + apiKey, |
| 427 | + baseURL, |
| 428 | + ...opts, |
| 429 | + ...(dangerouslyAllowBrowser !== undefined ? { dangerouslyAllowBrowser } : {}), |
| 430 | + }); |
| 431 | + |
| 432 | + this._azureADTokenProvider = azureADTokenProvider; |
| 433 | + this.apiVersion = apiVersion; |
| 434 | + } |
| 435 | + |
| 436 | + override buildRequest(options: Core.FinalRequestOptions<unknown>): { |
| 437 | + req: RequestInit; |
| 438 | + url: string; |
| 439 | + timeout: number; |
| 440 | + } { |
| 441 | + if (_deployments_endpoints.has(options.path) && options.method === 'post' && options.body !== undefined) { |
| 442 | + if (!Core.isObj(options.body)) { |
| 443 | + throw new Error('Expected request body to be an object'); |
| 444 | + } |
| 445 | + const model = options.body['model']; |
| 446 | + delete options.body['model']; |
| 447 | + if (model !== undefined && !this.baseURL.includes('/deployments')) { |
| 448 | + options.path = `/deployments/${model}${options.path}`; |
| 449 | + } |
| 450 | + } |
| 451 | + return super.buildRequest(options); |
| 452 | + } |
| 453 | + |
| 454 | + private _getAzureADToken(): string | undefined { |
| 455 | + if (typeof this._azureADTokenProvider === 'function') { |
| 456 | + const token = this._azureADTokenProvider(); |
| 457 | + if (!token || typeof token !== 'string') { |
| 458 | + throw new Errors.OpenAIError( |
| 459 | + `Expected 'azureADTokenProvider' argument to return a string but it returned ${token}`, |
| 460 | + ); |
| 461 | + } |
| 462 | + return token; |
| 463 | + } |
| 464 | + return undefined; |
| 465 | + } |
| 466 | + |
| 467 | + protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers { |
| 468 | + if (opts.headers?.['Authorization'] || opts.headers?.['api-key']) { |
| 469 | + return {}; |
| 470 | + } |
| 471 | + const token = this._getAzureADToken(); |
| 472 | + if (token) { |
| 473 | + return { Authorization: `Bearer ${token}` }; |
| 474 | + } |
| 475 | + if (this.apiKey !== API_KEY_SENTINEL) { |
| 476 | + return { 'api-key': this.apiKey }; |
| 477 | + } |
| 478 | + throw new Errors.OpenAIError('Unable to handle auth'); |
| 479 | + } |
| 480 | +} |
| 481 | + |
| 482 | +const _deployments_endpoints = new Set([ |
| 483 | + '/completions', |
| 484 | + '/chat/completions', |
| 485 | + '/embeddings', |
| 486 | + '/audio/transcriptions', |
| 487 | + '/audio/translations', |
| 488 | + '/audio/speech', |
| 489 | + '/images/generations', |
| 490 | +]); |
| 491 | + |
| 492 | +const API_KEY_SENTINEL = '<Missing Key>'; |
| 493 | + |
| 494 | +// ---------------------- End Azure ---------------------- |
| 495 | + |
313 | 496 | export default OpenAI;
|
0 commit comments