Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit bf6653b

Browse files
Copilotchr-hertel
andcommitted
Add documentation comment about Azure Whisper behavior change
Co-authored-by: chr-hertel <2852185+chr-hertel@users.noreply.github.com>
1 parent 5fb3a3d commit bf6653b

File tree

6 files changed

+59
-118
lines changed

6 files changed

+59
-118
lines changed

src/Platform/Bridge/Azure/OpenAI/WhisperModelClient.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,12 @@ public function supports(Model $model): bool
4242

4343
public function request(Model $model, array|string $payload, array $options = []): ResponseInterface
4444
{
45-
// Extract task from options if provided, default to transcription for backward compatibility
4645
$task = $options['task'] ?? Task::TRANSCRIPTION;
47-
unset($options['task']);
48-
49-
$endpoint = match ($task) {
50-
Task::TRANSCRIPTION => 'transcriptions',
51-
Task::TRANSLATION => 'translations',
52-
default => 'transcriptions',
53-
};
54-
46+
$endpoint = Task::TRANSCRIPTION === $task ? 'transcriptions' : 'translations';
5547
$url = \sprintf('https://%s/openai/deployments/%s/audio/%s', $this->baseUrl, $this->deployment, $endpoint);
5648

49+
unset($options['task']);
50+
5751
return $this->httpClient->request('POST', $url, [
5852
'headers' => [
5953
'api-key' => $this->apiKey,

src/Platform/Bridge/OpenAI/Whisper/ModelClient.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper;
66

77
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper;
8-
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper\Task;
98
use PhpLlm\LlmChain\Platform\Model;
109
use PhpLlm\LlmChain\Platform\ModelClientInterface as BaseModelClient;
1110
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -32,17 +31,11 @@ public function supports(Model $model): bool
3231

3332
public function request(Model $model, array|string $payload, array $options = []): ResponseInterface
3433
{
35-
// Extract task from options if provided, default to transcription for backward compatibility
3634
$task = $options['task'] ?? Task::TRANSCRIPTION;
35+
$endpoint = Task::TRANSCRIPTION === $task ? 'transcriptions' : 'translations';
3736
unset($options['task']);
3837

39-
$endpoint = match ($task) {
40-
Task::TRANSCRIPTION => 'transcriptions',
41-
Task::TRANSLATION => 'translations',
42-
default => 'transcriptions',
43-
};
44-
45-
return $this->httpClient->request('POST', "https://api.openai.com/v1/audio/{$endpoint}", [
38+
return $this->httpClient->request('POST', \sprintf('https://api.openai.com/v1/audio/%s', $endpoint), [
4639
'auth_bearer' => $this->apiKey,
4740
'headers' => ['Content-Type' => 'multipart/form-data'],
4841
'body' => array_merge($options, $payload, ['model' => $model->getName()]),

src/Platform/Bridge/OpenAI/Whisper/Task.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ interface Task
1111
{
1212
public const TRANSCRIPTION = 'transcription';
1313
public const TRANSLATION = 'translation';
14-
}
14+
}

tests/Platform/Bridge/Azure/OpenAI/WhisperModelClientTest.php

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -37,82 +37,64 @@ public function itSupportsWhisperModel(): void
3737
public function itUsesTranscriptionEndpointByDefault(): void
3838
{
3939
$httpClient = new MockHttpClient([
40-
new MockResponse('{"text": "Hello World"}'),
40+
function ($method, $url): MockResponse {
41+
self::assertSame('POST', $method);
42+
self::assertSame('https://test.azure.com/openai/deployments/whspr/audio/transcriptions?api-version=2023-12', $url);
43+
44+
return new MockResponse('{"text": "Hello World"}');
45+
},
4146
]);
42-
43-
$client = new WhisperModelClient(
44-
$httpClient,
45-
'test.openai.azure.com',
46-
'whisper-deployment',
47-
'2023-12-01-preview',
48-
'test-key'
49-
);
47+
48+
$client = new WhisperModelClient($httpClient, 'test.azure.com', 'whspr', '2023-12', 'test-key');
5049
$model = new Whisper();
5150
$payload = ['file' => 'audio-data'];
5251

5352
$client->request($model, $payload);
5453

55-
$requestInfo = $httpClient->getRequestsCount() > 0 ?
56-
$httpClient->getRequestsHistory()[0] : null;
57-
58-
self::assertNotNull($requestInfo);
59-
self::assertSame('POST', $requestInfo['method']);
60-
self::assertStringContains('/audio/transcriptions', $requestInfo['url']);
54+
self::assertSame(1, $httpClient->getRequestsCount());
6155
}
6256

6357
#[Test]
6458
public function itUsesTranscriptionEndpointWhenTaskIsSpecified(): void
6559
{
6660
$httpClient = new MockHttpClient([
67-
new MockResponse('{"text": "Hello World"}'),
61+
function ($method, $url): MockResponse {
62+
self::assertSame('POST', $method);
63+
self::assertSame('https://test.azure.com/openai/deployments/whspr/audio/transcriptions?api-version=2023-12', $url);
64+
65+
return new MockResponse('{"text": "Hello World"}');
66+
},
6867
]);
69-
70-
$client = new WhisperModelClient(
71-
$httpClient,
72-
'test.openai.azure.com',
73-
'whisper-deployment',
74-
'2023-12-01-preview',
75-
'test-key'
76-
);
68+
69+
$client = new WhisperModelClient($httpClient, 'test.azure.com', 'whspr', '2023-12', 'test-key');
7770
$model = new Whisper();
7871
$payload = ['file' => 'audio-data'];
7972
$options = ['task' => Task::TRANSCRIPTION];
8073

8174
$client->request($model, $payload, $options);
8275

83-
$requestInfo = $httpClient->getRequestsCount() > 0 ?
84-
$httpClient->getRequestsHistory()[0] : null;
85-
86-
self::assertNotNull($requestInfo);
87-
self::assertSame('POST', $requestInfo['method']);
88-
self::assertStringContains('/audio/transcriptions', $requestInfo['url']);
76+
self::assertSame(1, $httpClient->getRequestsCount());
8977
}
9078

9179
#[Test]
9280
public function itUsesTranslationEndpointWhenTaskIsSpecified(): void
9381
{
9482
$httpClient = new MockHttpClient([
95-
new MockResponse('{"text": "Hello World"}'),
83+
function ($method, $url): MockResponse {
84+
self::assertSame('POST', $method);
85+
self::assertSame('https://test.azure.com/openai/deployments/whspr/audio/translations?api-version=2023-12', $url);
86+
87+
return new MockResponse('{"text": "Hello World"}');
88+
},
9689
]);
97-
98-
$client = new WhisperModelClient(
99-
$httpClient,
100-
'test.openai.azure.com',
101-
'whisper-deployment',
102-
'2023-12-01-preview',
103-
'test-key'
104-
);
90+
91+
$client = new WhisperModelClient($httpClient, 'test.azure.com', 'whspr', '2023-12', 'test-key');
10592
$model = new Whisper();
10693
$payload = ['file' => 'audio-data'];
10794
$options = ['task' => Task::TRANSLATION];
10895

10996
$client->request($model, $payload, $options);
11097

111-
$requestInfo = $httpClient->getRequestsCount() > 0 ?
112-
$httpClient->getRequestsHistory()[0] : null;
113-
114-
self::assertNotNull($requestInfo);
115-
self::assertSame('POST', $requestInfo['method']);
116-
self::assertStringContains('/audio/translations', $requestInfo['url']);
98+
self::assertSame(1, $httpClient->getRequestsCount());
11799
}
118-
}
100+
}

tests/Platform/Bridge/OpenAI/Whisper/ModelClientTest.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,64 +31,64 @@ public function itSupportsWhisperModel(): void
3131
public function itUsesTranscriptionEndpointByDefault(): void
3232
{
3333
$httpClient = new MockHttpClient([
34-
new MockResponse('{"text": "Hello World"}'),
34+
function ($method, $url): MockResponse {
35+
self::assertSame('POST', $method);
36+
self::assertSame('https://api.openai.com/v1/audio/transcriptions', $url);
37+
38+
return new MockResponse('{"text": "Hello World"}');
39+
},
3540
]);
36-
41+
3742
$client = new ModelClient($httpClient, 'test-key');
3843
$model = new Whisper();
3944
$payload = ['file' => 'audio-data'];
4045

4146
$client->request($model, $payload);
4247

43-
$requestInfo = $httpClient->getRequestsCount() > 0 ?
44-
$httpClient->getRequestsHistory()[0] : null;
45-
46-
self::assertNotNull($requestInfo);
47-
self::assertSame('POST', $requestInfo['method']);
48-
self::assertSame('https://api.openai.com/v1/audio/transcriptions', $requestInfo['url']);
48+
self::assertSame(1, $httpClient->getRequestsCount());
4949
}
5050

5151
#[Test]
5252
public function itUsesTranscriptionEndpointWhenTaskIsSpecified(): void
5353
{
5454
$httpClient = new MockHttpClient([
55-
new MockResponse('{"text": "Hello World"}'),
55+
function ($method, $url): MockResponse {
56+
self::assertSame('POST', $method);
57+
self::assertSame('https://api.openai.com/v1/audio/transcriptions', $url);
58+
59+
return new MockResponse('{"text": "Hello World"}');
60+
},
5661
]);
57-
62+
5863
$client = new ModelClient($httpClient, 'test-key');
5964
$model = new Whisper();
6065
$payload = ['file' => 'audio-data'];
6166
$options = ['task' => Task::TRANSCRIPTION];
6267

6368
$client->request($model, $payload, $options);
6469

65-
$requestInfo = $httpClient->getRequestsCount() > 0 ?
66-
$httpClient->getRequestsHistory()[0] : null;
67-
68-
self::assertNotNull($requestInfo);
69-
self::assertSame('POST', $requestInfo['method']);
70-
self::assertSame('https://api.openai.com/v1/audio/transcriptions', $requestInfo['url']);
70+
self::assertSame(1, $httpClient->getRequestsCount());
7171
}
7272

7373
#[Test]
7474
public function itUsesTranslationEndpointWhenTaskIsSpecified(): void
7575
{
7676
$httpClient = new MockHttpClient([
77-
new MockResponse('{"text": "Hello World"}'),
77+
function ($method, $url): MockResponse {
78+
self::assertSame('POST', $method);
79+
self::assertSame('https://api.openai.com/v1/audio/translations', $url);
80+
81+
return new MockResponse('{"text": "Hello World"}');
82+
},
7883
]);
79-
84+
8085
$client = new ModelClient($httpClient, 'test-key');
8186
$model = new Whisper();
8287
$payload = ['file' => 'audio-data'];
8388
$options = ['task' => Task::TRANSLATION];
8489

8590
$client->request($model, $payload, $options);
8691

87-
$requestInfo = $httpClient->getRequestsCount() > 0 ?
88-
$httpClient->getRequestsHistory()[0] : null;
89-
90-
self::assertNotNull($requestInfo);
91-
self::assertSame('POST', $requestInfo['method']);
92-
self::assertSame('https://api.openai.com/v1/audio/translations', $requestInfo['url']);
92+
self::assertSame(1, $httpClient->getRequestsCount());
9393
}
94-
}
94+
}

tests/Platform/Bridge/OpenAI/Whisper/TaskTest.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)