Skip to content

fix: consistent Whisper task option for OpenAI and Azure bridges #49

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

Merged
merged 1 commit into from
Jul 1, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\AI\Platform\Bridge\Azure\OpenAI;

use Symfony\AI\Platform\Bridge\OpenAI\Whisper;
use Symfony\AI\Platform\Bridge\OpenAI\Whisper\Task;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\ModelClientInterface;
use Symfony\Component\HttpClient\EventSourceHttpClient;
Expand Down Expand Up @@ -48,7 +49,11 @@ public function supports(Model $model): bool

public function request(Model $model, array|string $payload, array $options = []): ResponseInterface
{
$url = \sprintf('https://%s/openai/deployments/%s/audio/translations', $this->baseUrl, $this->deployment);
$task = $options['task'] ?? Task::TRANSCRIPTION;
$endpoint = Task::TRANSCRIPTION === $task ? 'transcriptions' : 'translations';
$url = \sprintf('https://%s/openai/deployments/%s/audio/%s', $this->baseUrl, $this->deployment, $endpoint);

unset($options['task']);

return $this->httpClient->request('POST', $url, [
'headers' => [
Expand Down
6 changes: 5 additions & 1 deletion src/platform/src/Bridge/OpenAI/Whisper/ModelClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public function supports(Model $model): bool

public function request(Model $model, array|string $payload, array $options = []): ResponseInterface
{
return $this->httpClient->request('POST', 'https://api.openai.com/v1/audio/transcriptions', [
$task = $options['task'] ?? Task::TRANSCRIPTION;
$endpoint = Task::TRANSCRIPTION === $task ? 'transcriptions' : 'translations';
unset($options['task']);

return $this->httpClient->request('POST', \sprintf('https://api.openai.com/v1/audio/%s', $endpoint), [
'auth_bearer' => $this->apiKey,
'headers' => ['Content-Type' => 'multipart/form-data'],
'body' => array_merge($options, $payload, ['model' => $model->getName()]),
Expand Down
21 changes: 21 additions & 0 deletions src/platform/src/Bridge/OpenAI/Whisper/Task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Bridge\OpenAI\Whisper;

/**
* @author Christopher Hertel <[email protected]>
*/
interface Task
{
public const TRANSCRIPTION = 'transcription';
public const TRANSLATION = 'translation';
}
107 changes: 107 additions & 0 deletions src/platform/tests/Bridge/Azure/OpenAI/WhisperModelClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Tests\Bridge\Azure\OpenAI;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\Azure\OpenAI\WhisperModelClient;
use Symfony\AI\Platform\Bridge\OpenAI\Whisper;
use Symfony\AI\Platform\Bridge\OpenAI\Whisper\Task;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

#[CoversClass(WhisperModelClient::class)]
#[Small]
final class WhisperModelClientTest extends TestCase
{
#[Test]
public function itSupportsWhisperModel(): void
{
$client = new WhisperModelClient(
new MockHttpClient(),
'test.openai.azure.com',
'whisper-deployment',
'2023-12-01-preview',
'test-key'
);
$model = new Whisper();

self::assertTrue($client->supports($model));
}

#[Test]
public function itUsesTranscriptionEndpointByDefault(): void
{
$httpClient = new MockHttpClient([
function ($method, $url): MockResponse {
self::assertSame('POST', $method);
self::assertSame('https://test.azure.com/openai/deployments/whspr/audio/transcriptions?api-version=2023-12', $url);

return new MockResponse('{"text": "Hello World"}');
},
]);

$client = new WhisperModelClient($httpClient, 'test.azure.com', 'whspr', '2023-12', 'test-key');
$model = new Whisper();
$payload = ['file' => 'audio-data'];

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

self::assertSame(1, $httpClient->getRequestsCount());
}

#[Test]
public function itUsesTranscriptionEndpointWhenTaskIsSpecified(): void
{
$httpClient = new MockHttpClient([
function ($method, $url): MockResponse {
self::assertSame('POST', $method);
self::assertSame('https://test.azure.com/openai/deployments/whspr/audio/transcriptions?api-version=2023-12', $url);

return new MockResponse('{"text": "Hello World"}');
},
]);

$client = new WhisperModelClient($httpClient, 'test.azure.com', 'whspr', '2023-12', 'test-key');
$model = new Whisper();
$payload = ['file' => 'audio-data'];
$options = ['task' => Task::TRANSCRIPTION];

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

self::assertSame(1, $httpClient->getRequestsCount());
}

#[Test]
public function itUsesTranslationEndpointWhenTaskIsSpecified(): void
{
$httpClient = new MockHttpClient([
function ($method, $url): MockResponse {
self::assertSame('POST', $method);
self::assertSame('https://test.azure.com/openai/deployments/whspr/audio/translations?api-version=2023-12', $url);

return new MockResponse('{"text": "Hello World"}');
},
]);

$client = new WhisperModelClient($httpClient, 'test.azure.com', 'whspr', '2023-12', 'test-key');
$model = new Whisper();
$payload = ['file' => 'audio-data'];
$options = ['task' => Task::TRANSLATION];

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

self::assertSame(1, $httpClient->getRequestsCount());
}
}
101 changes: 101 additions & 0 deletions src/platform/tests/Bridge/OpenAI/Whisper/ModelClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Tests\Bridge\OpenAI\Whisper;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\OpenAI\Whisper;
use Symfony\AI\Platform\Bridge\OpenAI\Whisper\ModelClient;
use Symfony\AI\Platform\Bridge\OpenAI\Whisper\Task;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

#[CoversClass(ModelClient::class)]
#[Small]
final class ModelClientTest extends TestCase
{
#[Test]
public function itSupportsWhisperModel(): void
{
$client = new ModelClient(new MockHttpClient(), 'test-key');
$model = new Whisper();

self::assertTrue($client->supports($model));
}

#[Test]
public function itUsesTranscriptionEndpointByDefault(): void
{
$httpClient = new MockHttpClient([
function ($method, $url): MockResponse {
self::assertSame('POST', $method);
self::assertSame('https://api.openai.com/v1/audio/transcriptions', $url);

return new MockResponse('{"text": "Hello World"}');
},
]);

$client = new ModelClient($httpClient, 'test-key');
$model = new Whisper();
$payload = ['file' => 'audio-data'];

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

self::assertSame(1, $httpClient->getRequestsCount());
}

#[Test]
public function itUsesTranscriptionEndpointWhenTaskIsSpecified(): void
{
$httpClient = new MockHttpClient([
function ($method, $url): MockResponse {
self::assertSame('POST', $method);
self::assertSame('https://api.openai.com/v1/audio/transcriptions', $url);

return new MockResponse('{"text": "Hello World"}');
},
]);

$client = new ModelClient($httpClient, 'test-key');
$model = new Whisper();
$payload = ['file' => 'audio-data'];
$options = ['task' => Task::TRANSCRIPTION];

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

self::assertSame(1, $httpClient->getRequestsCount());
}

#[Test]
public function itUsesTranslationEndpointWhenTaskIsSpecified(): void
{
$httpClient = new MockHttpClient([
function ($method, $url): MockResponse {
self::assertSame('POST', $method);
self::assertSame('https://api.openai.com/v1/audio/translations', $url);

return new MockResponse('{"text": "Hello World"}');
},
]);

$client = new ModelClient($httpClient, 'test-key');
$model = new Whisper();
$payload = ['file' => 'audio-data'];
$options = ['task' => Task::TRANSLATION];

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

self::assertSame(1, $httpClient->getRequestsCount());
}
}