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

release: 4.41.1 #826

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 .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "4.41.0"
".": "4.41.1"
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 4.41.1 (2024-05-06)

Full Changelog: [v4.41.0...v4.41.1](https://github.com/openai/openai-node/compare/v4.41.0...v4.41.1)

### Bug Fixes

* **azure:** update build script ([#825](https://github.com/openai/openai-node/issues/825)) ([8afc6e7](https://github.com/openai/openai-node/commit/8afc6e7b49507b3be0228e93913d51b4c3211add))

## 4.41.0 (2024-05-05)

Full Changelog: [v4.40.2...v4.41.0](https://github.com/openai/openai-node/compare/v4.40.2...v4.41.0)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ You can import in Deno via:
<!-- x-release-please-start-version -->

```ts
import OpenAI from 'https://deno.land/x/[email protected].0/mod.ts';
import OpenAI from 'https://deno.land/x/[email protected].1/mod.ts';
```

<!-- x-release-please-end -->
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openai",
"version": "4.41.0",
"version": "4.41.1",
"description": "The official TypeScript library for the OpenAI API",
"author": "OpenAI <[email protected]>",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-deno
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This is a build produced from https://github.com/openai/openai-node – please g
Usage:

\`\`\`ts
import OpenAI from "https://deno.land/x/[email protected].0/mod.ts";
import OpenAI from "https://deno.land/x/[email protected].1/mod.ts";

const client = new OpenAI();
\`\`\`
Expand Down
2 changes: 1 addition & 1 deletion scripts/utils/fix-index-exports.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ const indexJs =
let before = fs.readFileSync(indexJs, 'utf8');
let after = before.replace(
/^\s*exports\.default\s*=\s*(\w+)/m,
'exports = module.exports = $1;\nexports.default = $1',
'exports = module.exports = $1;\nmodule.exports.AzureOpenAI = AzureOpenAI;\nexports.default = $1',
);
fs.writeFileSync(indexJs, after, 'utf8');
28 changes: 17 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,12 @@ export interface AzureClientOptions extends ClientOptions {
* A function that returns an access token for Microsoft Entra (formerly known as Azure Active Directory),
* which will be invoked on every request.
*/
azureADTokenProvider?: (() => string) | undefined;
azureADTokenProvider?: (() => Promise<string>) | undefined;
}

/** API Client for interfacing with the Azure OpenAI API. */
export class AzureOpenAI extends OpenAI {
private _azureADTokenProvider: (() => string) | undefined;
private _azureADTokenProvider: (() => Promise<string>) | undefined;
apiVersion: string = '';
/**
* API Client for interfacing with the Azure OpenAI API.
Expand Down Expand Up @@ -451,9 +451,9 @@ export class AzureOpenAI extends OpenAI {
return super.buildRequest(options);
}

private _getAzureADToken(): string | undefined {
private async _getAzureADToken(): Promise<string | undefined> {
if (typeof this._azureADTokenProvider === 'function') {
const token = this._azureADTokenProvider();
const token = await this._azureADTokenProvider();
if (!token || typeof token !== 'string') {
throw new Errors.OpenAIError(
`Expected 'azureADTokenProvider' argument to return a string but it returned ${token}`,
Expand All @@ -465,17 +465,23 @@ export class AzureOpenAI extends OpenAI {
}

protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers {
return {};
}

protected override async prepareOptions(opts: Core.FinalRequestOptions<unknown>): Promise<void> {
if (opts.headers?.['Authorization'] || opts.headers?.['api-key']) {
return {};
return super.prepareOptions(opts);
}
const token = this._getAzureADToken();
const token = await this._getAzureADToken();
opts.headers ??= {};
if (token) {
return { Authorization: `Bearer ${token}` };
}
if (this.apiKey !== API_KEY_SENTINEL) {
return { 'api-key': this.apiKey };
opts.headers['Authorization'] = `Bearer ${token}`;
} else if (this.apiKey !== API_KEY_SENTINEL) {
opts.headers['api-key'] = this.apiKey;
} else {
throw new Errors.OpenAIError('Unable to handle auth');
}
throw new Errors.OpenAIError('Unable to handle auth');
return super.prepareOptions(opts);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = '4.41.0'; // x-release-please-version
export const VERSION = '4.41.1'; // x-release-please-version
19 changes: 12 additions & 7 deletions tests/lib/azure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,24 +222,29 @@ describe('instantiate azure client', () => {
});

describe('Azure Active Directory (AD)', () => {
test('with azureADTokenProvider', () => {
test('with azureADTokenProvider', async () => {
const testFetch = async (url: RequestInfo, { headers }: RequestInit = {}): Promise<Response> => {
return new Response(JSON.stringify({ a: 1 }), { headers });
};
const client = new AzureOpenAI({
baseURL: 'http:https://localhost:5000/',
azureADTokenProvider: () => 'my token',
azureADTokenProvider: async () => 'my token',
apiVersion,
fetch: testFetch,
});
expect(client.buildRequest({ method: 'post', path: 'https://example.com' }).req.headers).toHaveProperty(
'authorization',
'Bearer my token',
);
expect(
(await client.request({ method: 'post', path: 'https://example.com' }).asResponse()).headers.get(
'authorization',
),
).toEqual('Bearer my token');
});

test('apiKey and azureADTokenProvider cant be combined', () => {
expect(
() =>
new AzureOpenAI({
baseURL: 'http:https://localhost:5000/',
azureADTokenProvider: () => 'my token',
azureADTokenProvider: async () => 'my token',
apiKey: 'My API Key',
apiVersion,
}),
Expand Down