Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Azure] Refresh AAD token on retry #5

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ export class AzureOpenAI extends OpenAI {
}

protected override async prepareOptions(opts: Core.FinalRequestOptions<unknown>): Promise<void> {
if (opts.headers?.['Authorization'] || opts.headers?.['api-key']) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this will overwrite an Authorization header passed by the user?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't support the scenario of passing a single AAD token in the authorization header manually. The client constructor requires the user to provide either an API key or an AAD token provider.

if (opts.headers?.['api-key']) {
return super.prepareOptions(opts);
}
const token = await this._getAzureADToken();
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/azure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,43 @@ describe('instantiate azure client', () => {
/The `apiKey` and `azureADTokenProvider` arguments are mutually exclusive; only one can be passed at a time./,
);
});

test('AAD token is refreshed', async () => {
let fail = true;
const testFetch = async (url: RequestInfo, req: RequestInit | undefined): Promise<Response> => {
if (fail) {
fail = false;
return new Response(undefined, {
status: 429,
headers: {
'Retry-After': '0.1',
},
});
}
return new Response(
JSON.stringify({ auth: (req?.headers as Record<string, string>)['authorization'] }),
{ headers: { 'content-type': 'application/json' } },
);
};
let counter = 0;
async function azureADTokenProvider() {
return `token-${counter++}`;
}
const client = new AzureOpenAI({
baseURL: 'http://localhost:5000/',
azureADTokenProvider,
apiVersion,
fetch: testFetch,
});
expect(
await client.chat.completions.create({
model,
messages: [{ role: 'system', content: 'Hello' }],
}),
).toStrictEqual({
auth: 'Bearer token-1',
});
});
});

test('with endpoint', () => {
Expand Down