azure.js 5.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AzureOpenAI = void 0;
  4. const tslib_1 = require("./internal/tslib.js");
  5. const headers_1 = require("./internal/headers.js");
  6. const Errors = tslib_1.__importStar(require("./error.js"));
  7. const utils_1 = require("./internal/utils.js");
  8. const client_1 = require("./client.js");
  9. /** API Client for interfacing with the Azure OpenAI API. */
  10. class AzureOpenAI extends client_1.OpenAI {
  11. /**
  12. * API Client for interfacing with the Azure OpenAI API.
  13. *
  14. * @param {string | undefined} [opts.apiVersion=process.env['OPENAI_API_VERSION'] ?? undefined]
  15. * @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/`
  16. * @param {string | undefined} [opts.apiKey=process.env['AZURE_OPENAI_API_KEY'] ?? undefined]
  17. * @param {string | undefined} opts.deployment - A model deployment, if given, sets the base client URL to include `/deployments/{deployment}`.
  18. * @param {string | null | undefined} [opts.organization=process.env['OPENAI_ORG_ID'] ?? null]
  19. * @param {string} [opts.baseURL=process.env['OPENAI_BASE_URL']] - Sets the base URL for the API, e.g. `https://example-resource.azure.openai.com/openai/`.
  20. * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
  21. * @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections.
  22. * @param {Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.
  23. * @param {number} [opts.maxRetries=2] - The maximum number of times the client will retry a request.
  24. * @param {Headers} opts.defaultHeaders - Default headers to include with every request to the API.
  25. * @param {DefaultQuery} opts.defaultQuery - Default query parameters to include with every request to the API.
  26. * @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.
  27. */
  28. constructor({ baseURL = (0, utils_1.readEnv)('OPENAI_BASE_URL'), apiKey = (0, utils_1.readEnv)('AZURE_OPENAI_API_KEY'), apiVersion = (0, utils_1.readEnv)('OPENAI_API_VERSION'), endpoint, deployment, azureADTokenProvider, dangerouslyAllowBrowser, ...opts } = {}) {
  29. if (!apiVersion) {
  30. throw new Errors.OpenAIError("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' }).");
  31. }
  32. if (typeof azureADTokenProvider === 'function') {
  33. dangerouslyAllowBrowser = true;
  34. }
  35. if (!azureADTokenProvider && !apiKey) {
  36. throw new Errors.OpenAIError('Missing credentials. Please pass one of `apiKey` and `azureADTokenProvider`, or set the `AZURE_OPENAI_API_KEY` environment variable.');
  37. }
  38. if (azureADTokenProvider && apiKey) {
  39. throw new Errors.OpenAIError('The `apiKey` and `azureADTokenProvider` arguments are mutually exclusive; only one can be passed at a time.');
  40. }
  41. opts.defaultQuery = { ...opts.defaultQuery, 'api-version': apiVersion };
  42. if (!baseURL) {
  43. if (!endpoint) {
  44. endpoint = process.env['AZURE_OPENAI_ENDPOINT'];
  45. }
  46. if (!endpoint) {
  47. throw new Errors.OpenAIError('Must provide one of the `baseURL` or `endpoint` arguments, or the `AZURE_OPENAI_ENDPOINT` environment variable');
  48. }
  49. baseURL = `${endpoint}/openai`;
  50. }
  51. else {
  52. if (endpoint) {
  53. throw new Errors.OpenAIError('baseURL and endpoint are mutually exclusive');
  54. }
  55. }
  56. super({
  57. apiKey: azureADTokenProvider ?? apiKey,
  58. baseURL,
  59. ...opts,
  60. ...(dangerouslyAllowBrowser !== undefined ? { dangerouslyAllowBrowser } : {}),
  61. });
  62. this.apiVersion = '';
  63. this.apiVersion = apiVersion;
  64. this.deploymentName = deployment;
  65. }
  66. async buildRequest(options, props = {}) {
  67. if (_deployments_endpoints.has(options.path) && options.method === 'post' && options.body !== undefined) {
  68. if (!(0, utils_1.isObj)(options.body)) {
  69. throw new Error('Expected request body to be an object');
  70. }
  71. const model = this.deploymentName || options.body['model'] || options.__metadata?.['model'];
  72. if (model !== undefined && !this.baseURL.includes('/deployments')) {
  73. options.path = `/deployments/${model}${options.path}`;
  74. }
  75. }
  76. return super.buildRequest(options, props);
  77. }
  78. async authHeaders(opts) {
  79. if (typeof this._options.apiKey === 'string') {
  80. return (0, headers_1.buildHeaders)([{ 'api-key': this.apiKey }]);
  81. }
  82. return super.authHeaders(opts);
  83. }
  84. }
  85. exports.AzureOpenAI = AzureOpenAI;
  86. const _deployments_endpoints = new Set([
  87. '/completions',
  88. '/chat/completions',
  89. '/embeddings',
  90. '/audio/transcriptions',
  91. '/audio/translations',
  92. '/audio/speech',
  93. '/images/generations',
  94. '/batches',
  95. '/images/edits',
  96. ]);
  97. //# sourceMappingURL=azure.js.map