Skip to content

Commit 1453757

Browse files
committed
address feedback
1 parent 9086c73 commit 1453757

File tree

4 files changed

+21
-47
lines changed

4 files changed

+21
-47
lines changed

src/azure.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface AzureClientOptions extends ClientOptions {
3838
export class AzureOpenAI extends OpenAI {
3939
deploymentName: string | undefined;
4040
apiVersion: string = '';
41+
override apiKey: string;
4142

4243
/**
4344
* API Client for interfacing with the Azure OpenAI API.
@@ -109,16 +110,15 @@ export class AzureOpenAI extends OpenAI {
109110
}
110111

111112
super({
112-
apiKey,
113+
apiKey: !azureADTokenProvider ? apiKey : async () => ({ token: await azureADTokenProvider() }),
113114
baseURL,
114-
tokenProvider:
115-
!azureADTokenProvider ? undefined : async () => ({ token: await azureADTokenProvider() }),
116115
...opts,
117116
...(dangerouslyAllowBrowser !== undefined ? { dangerouslyAllowBrowser } : {}),
118117
});
119118

120119
this.apiVersion = apiVersion;
121120
this.deploymentName = deployment;
121+
this.apiKey = apiKey ?? 'PLACEHOLDER_FOR_AZURE_AD_TOKEN_PROVIDER';
122122
}
123123

124124
override async buildRequest(

src/beta/realtime/websocket.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ export class OpenAIRealtimeWebSocket extends OpenAIRealtimeEmitter {
3737
super();
3838

3939
const dangerouslyAllowBrowser =
40-
props.dangerouslyAllowBrowser ??
41-
(client as any)?._options?.dangerouslyAllowBrowser ??
42-
(client?.apiKey.startsWith('ek_') ? true : null);
40+
(
41+
props.dangerouslyAllowBrowser ??
42+
(client as any)?._options?.dangerouslyAllowBrowser ??
43+
typeof client?.apiKey === 'function'
44+
) ?
45+
true
46+
: undefined;
4347

4448
if (!dangerouslyAllowBrowser && isRunningInBrowser()) {
4549
throw new OpenAIError(

src/client.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,7 @@ export interface ClientOptions {
215215
/**
216216
* Defaults to process.env['OPENAI_API_KEY'].
217217
*/
218-
apiKey?: string | undefined;
219-
/**
220-
* A function that returns a token to use for authentication.
221-
*/
222-
tokenProvider?: TokenProvider | undefined;
218+
apiKey?: string | TokenProvider | undefined;
223219
/**
224220
* Defaults to process.env['OPENAI_ORG_ID'].
225221
*/
@@ -314,7 +310,7 @@ export interface ClientOptions {
314310
* API Client for interfacing with the OpenAI API.
315311
*/
316312
export class OpenAI {
317-
apiKey: string;
313+
apiKey: string | TokenProvider;
318314
organization: string | null;
319315
project: string | null;
320316
webhookSecret: string | null;
@@ -330,7 +326,6 @@ export class OpenAI {
330326
#encoder: Opts.RequestEncoder;
331327
protected idempotencyHeader?: string;
332328
private _options: ClientOptions;
333-
private _tokenProvider: TokenProvider | undefined;
334329

335330
/**
336331
* API Client for interfacing with the OpenAI API.
@@ -354,18 +349,11 @@ export class OpenAI {
354349
organization = readEnv('OPENAI_ORG_ID') ?? null,
355350
project = readEnv('OPENAI_PROJECT_ID') ?? null,
356351
webhookSecret = readEnv('OPENAI_WEBHOOK_SECRET') ?? null,
357-
tokenProvider,
358352
...opts
359353
}: ClientOptions = {}) {
360-
if (apiKey === undefined && !tokenProvider) {
361-
throw new Errors.OpenAIError(
362-
'Missing credentials. Please pass one of `apiKey` and `tokenProvider`, or set the `OPENAI_API_KEY` environment variable.',
363-
);
364-
}
365-
366-
if (tokenProvider && apiKey) {
354+
if (apiKey === undefined) {
367355
throw new Errors.OpenAIError(
368-
'The `apiKey` and `tokenProvider` arguments are mutually exclusive; only one can be passed at a time.',
356+
'Missing credentials. Please pass an `apiKey`, or set the `OPENAI_API_KEY` environment variable.',
369357
);
370358
}
371359

@@ -374,7 +362,6 @@ export class OpenAI {
374362
organization,
375363
project,
376364
webhookSecret,
377-
tokenProvider,
378365
...opts,
379366
baseURL: baseURL || `https://api.openai.com/v1`,
380367
};
@@ -403,7 +390,6 @@ export class OpenAI {
403390
this._options = options;
404391

405392
this.apiKey = apiKey ?? 'Missing Key';
406-
this._tokenProvider = tokenProvider;
407393
this.organization = organization;
408394
this.project = project;
409395
this.webhookSecret = webhookSecret;
@@ -423,7 +409,6 @@ export class OpenAI {
423409
fetch: this.fetch,
424410
fetchOptions: this.fetchOptions,
425411
apiKey: this.apiKey,
426-
tokenProvider: this._tokenProvider,
427412
organization: this.organization,
428413
project: this.project,
429414
webhookSecret: this.webhookSecret,
@@ -473,12 +458,12 @@ export class OpenAI {
473458
}
474459

475460
async _setToken(): Promise<boolean> {
476-
if (typeof this._tokenProvider === 'function') {
461+
if (typeof this.apiKey === 'function') {
477462
try {
478-
const token = await this._tokenProvider();
463+
const token = await this.apiKey();
479464
if (!token || typeof token.token !== 'string') {
480465
throw new Errors.OpenAIError(
481-
`Expected 'tokenProvider' argument to return a string but it returned ${token}`,
466+
`Expected 'apiKey' function argument to return a string but it returned ${token}`,
482467
);
483468
}
484469
this.apiKey = token.token;
@@ -488,7 +473,7 @@ export class OpenAI {
488473
throw err;
489474
}
490475
throw new Errors.OpenAIError(
491-
`Failed to get token from 'tokenProvider' function: ${err.message}`,
476+
`Failed to get token from 'apiKey' function: ${err.message}`,
492477
// @ts-ignore
493478
{ cause: err },
494479
);

tests/index.test.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ describe('retries', () => {
736736
};
737737
const client = new OpenAI({
738738
baseURL: 'http://localhost:5000/',
739-
tokenProvider: async () => ({ token: 'my token' }),
739+
apiKey: async () => ({ token: 'my token' }),
740740
fetch: testFetch,
741741
});
742742
expect(
@@ -763,12 +763,12 @@ describe('retries', () => {
763763
});
764764
};
765765
let counter = 0;
766-
async function tokenProvider() {
766+
async function apiKey() {
767767
return { token: `token-${counter++}` };
768768
}
769769
const client = new OpenAI({
770770
baseURL: 'http://localhost:5000/',
771-
tokenProvider,
771+
apiKey,
772772
fetch: testFetch,
773773
});
774774
expect(
@@ -783,21 +783,6 @@ describe('retries', () => {
783783
).toEqual('Bearer token-1');
784784
});
785785

786-
test('mutual exclusive', () => {
787-
try {
788-
new OpenAI({
789-
baseURL: 'http://localhost:5000/',
790-
tokenProvider: async () => ({ token: 'my token' }),
791-
apiKey: 'my api key',
792-
});
793-
} catch (error: any) {
794-
expect(error).toBeInstanceOf(Error);
795-
expect(error.message).toEqual(
796-
'The `apiKey` and `tokenProvider` arguments are mutually exclusive; only one can be passed at a time.',
797-
);
798-
}
799-
});
800-
801786
test('at least one', () => {
802787
try {
803788
new OpenAI({

0 commit comments

Comments
 (0)