Skip to content

Commit a5ce6ac

Browse files
committed
address feedback
1 parent 64a0231 commit a5ce6ac

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
@@ -200,11 +200,7 @@ export interface ClientOptions {
200200
/**
201201
* Defaults to process.env['OPENAI_API_KEY'].
202202
*/
203-
apiKey?: string | undefined;
204-
/**
205-
* A function that returns a token to use for authentication.
206-
*/
207-
tokenProvider?: TokenProvider | undefined;
203+
apiKey?: string | TokenProvider | undefined;
208204
/**
209205
* Defaults to process.env['OPENAI_ORG_ID'].
210206
*/
@@ -299,7 +295,7 @@ export interface ClientOptions {
299295
* API Client for interfacing with the OpenAI API.
300296
*/
301297
export class OpenAI {
302-
apiKey: string;
298+
apiKey: string | TokenProvider;
303299
organization: string | null;
304300
project: string | null;
305301
webhookSecret: string | null;
@@ -315,7 +311,6 @@ export class OpenAI {
315311
#encoder: Opts.RequestEncoder;
316312
protected idempotencyHeader?: string;
317313
private _options: ClientOptions;
318-
private _tokenProvider: TokenProvider | undefined;
319314

320315
/**
321316
* API Client for interfacing with the OpenAI API.
@@ -339,18 +334,11 @@ export class OpenAI {
339334
organization = readEnv('OPENAI_ORG_ID') ?? null,
340335
project = readEnv('OPENAI_PROJECT_ID') ?? null,
341336
webhookSecret = readEnv('OPENAI_WEBHOOK_SECRET') ?? null,
342-
tokenProvider,
343337
...opts
344338
}: ClientOptions = {}) {
345-
if (apiKey === undefined && !tokenProvider) {
346-
throw new Errors.OpenAIError(
347-
'Missing credentials. Please pass one of `apiKey` and `tokenProvider`, or set the `OPENAI_API_KEY` environment variable.',
348-
);
349-
}
350-
351-
if (tokenProvider && apiKey) {
339+
if (apiKey === undefined) {
352340
throw new Errors.OpenAIError(
353-
'The `apiKey` and `tokenProvider` arguments are mutually exclusive; only one can be passed at a time.',
341+
'Missing credentials. Please pass an `apiKey`, or set the `OPENAI_API_KEY` environment variable.',
354342
);
355343
}
356344

@@ -359,7 +347,6 @@ export class OpenAI {
359347
organization,
360348
project,
361349
webhookSecret,
362-
tokenProvider,
363350
...opts,
364351
baseURL: baseURL || `https://api.openai.com/v1`,
365352
};
@@ -388,7 +375,6 @@ export class OpenAI {
388375
this._options = options;
389376

390377
this.apiKey = apiKey ?? 'Missing Key';
391-
this._tokenProvider = tokenProvider;
392378
this.organization = organization;
393379
this.project = project;
394380
this.webhookSecret = webhookSecret;
@@ -408,7 +394,6 @@ export class OpenAI {
408394
fetch: this.fetch,
409395
fetchOptions: this.fetchOptions,
410396
apiKey: this.apiKey,
411-
tokenProvider: this._tokenProvider,
412397
organization: this.organization,
413398
project: this.project,
414399
webhookSecret: this.webhookSecret,
@@ -458,12 +443,12 @@ export class OpenAI {
458443
}
459444

460445
async _setToken(): Promise<boolean> {
461-
if (typeof this._tokenProvider === 'function') {
446+
if (typeof this.apiKey === 'function') {
462447
try {
463-
const token = await this._tokenProvider();
448+
const token = await this.apiKey();
464449
if (!token || typeof token.token !== 'string') {
465450
throw new Errors.OpenAIError(
466-
`Expected 'tokenProvider' argument to return a string but it returned ${token}`,
451+
`Expected 'apiKey' function argument to return a string but it returned ${token}`,
467452
);
468453
}
469454
this.apiKey = token.token;
@@ -473,7 +458,7 @@ export class OpenAI {
473458
throw err;
474459
}
475460
throw new Errors.OpenAIError(
476-
`Failed to get token from 'tokenProvider' function: ${err.message}`,
461+
`Failed to get token from 'apiKey' function: ${err.message}`,
477462
// @ts-ignore
478463
{ cause: err },
479464
);

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)