Skip to content

Commit 13cdd27

Browse files
committed
[Azure] Use model in request body for audio operations
1 parent 145ff67 commit 13cdd27

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

src/azure/audio.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Audio, Transcriptions, Translations } from '../resources/audio';
2+
import type { OpenAI } from '../index';
3+
4+
class AzureTranslation extends Translations {
5+
constructor(
6+
client: OpenAI,
7+
private deployments: string[],
8+
) {
9+
super(client);
10+
}
11+
// @ts-ignore we don't want to redeclare all the method signatures
12+
override create(...args: Parameters<Translations['create']>): ReturnType<Translations['create']> {
13+
this.deployments.push(args[0].model);
14+
return super.create(args[0], args[1]);
15+
}
16+
}
17+
18+
class AzureTranscription extends Transcriptions {
19+
constructor(
20+
client: OpenAI,
21+
private deployments: string[],
22+
) {
23+
super(client);
24+
}
25+
// @ts-ignore we don't want to redeclare all the method signatures
26+
override create(...args: Parameters<Transcriptions['create']>): ReturnType<Transcriptions['create']> {
27+
this.deployments.push(args[0].model);
28+
return super.create(args[0], args[1]);
29+
}
30+
}
31+
32+
export class AzureAudio extends Audio {
33+
constructor(
34+
client: OpenAI,
35+
private _deployments: string[],
36+
) {
37+
super(client);
38+
}
39+
// @ts-ignore we don't want to redeclare all the method signatures
40+
override translations: Audio.Translations = new AzureTranslation(this._client, this._deployments);
41+
// @ts-ignore we don't want to redeclare all the method signatures
42+
override transcriptions: Audio.Transcriptions = new AzureTranscription(this._client, this._deployments);
43+
}

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ import {
106106
UploadCreateParams,
107107
Uploads as UploadsAPIUploads,
108108
} from './resources/uploads/uploads';
109+
import { AzureAudio } from './azure/audio';
109110

110111
export interface ClientOptions {
111112
/**
@@ -492,6 +493,7 @@ export interface AzureClientOptions extends ClientOptions {
492493
/** API Client for interfacing with the Azure OpenAI API. */
493494
export class AzureOpenAI extends OpenAI {
494495
private _azureADTokenProvider: (() => Promise<string>) | undefined;
496+
private _audioDeployments: string[] = [];
495497
deploymentName: string | undefined;
496498
apiVersion: string = '';
497499
/**
@@ -578,6 +580,8 @@ export class AzureOpenAI extends OpenAI {
578580
this.deploymentName = deployment;
579581
}
580582

583+
override audio: API.Audio = new AzureAudio(this, this._audioDeployments);
584+
581585
override buildRequest(
582586
options: Core.FinalRequestOptions<unknown>,
583587
props: { retryCount?: number } = {},
@@ -590,7 +594,7 @@ export class AzureOpenAI extends OpenAI {
590594
if (!Core.isObj(options.body)) {
591595
throw new Error('Expected request body to be an object');
592596
}
593-
const model = this.deploymentName || options.body['model'];
597+
const model = this.deploymentName || options.body['model'] || this._audioDeployments.shift();
594598
if (model !== undefined && !this.baseURL.includes('/deployments')) {
595599
options.path = `/deployments/${model}${options.path}`;
596600
}

tests/lib/azure.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,21 +495,23 @@ describe('azure request building', () => {
495495
);
496496
});
497497

498-
test('Audio translations is not handled', async () => {
498+
test('handles audio translations', async () => {
499499
const { url } = (await client.audio.translations.create({
500500
model: deployment,
501501
file: { url: 'https://example.com', blob: () => 0 as any },
502502
})) as any;
503-
expect(url).toStrictEqual(`https://example.com/openai/audio/translations?api-version=${apiVersion}`);
503+
expect(url).toStrictEqual(
504+
`https://example.com/openai/deployments/deployment/audio/translations?api-version=${apiVersion}`,
505+
);
504506
});
505507

506-
test('Audio transcriptions is not handled', async () => {
508+
test('handles audio transcriptions', async () => {
507509
const { url } = (await client.audio.transcriptions.create({
508510
model: deployment,
509511
file: { url: 'https://example.com', blob: () => 0 as any },
510512
})) as any;
511513
expect(url).toStrictEqual(
512-
`https://example.com/openai/audio/transcriptions?api-version=${apiVersion}`,
514+
`https://example.com/openai/deployments/deployment/audio/transcriptions?api-version=${apiVersion}`,
513515
);
514516
});
515517

0 commit comments

Comments
 (0)