Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
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
7 changes: 6 additions & 1 deletion src/Platform/Bridge/Azure/OpenAI/WhisperModelClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpLlm\LlmChain\Platform\Bridge\Azure\OpenAI;

use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper;
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper\Task;
use PhpLlm\LlmChain\Platform\Model;
use PhpLlm\LlmChain\Platform\ModelClientInterface;
use Symfony\Component\HttpClient\EventSourceHttpClient;
Expand Down Expand Up @@ -41,7 +42,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/Bridge/OpenAI/Whisper/ModelClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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
14 changes: 14 additions & 0 deletions src/Platform/Bridge/OpenAI/Whisper/Task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper;

/**
* @author Christopher Hertel <mail@christopher-hertel.de>
*/
interface Task
{
public const TRANSCRIPTION = 'transcription';
public const TRANSLATION = 'translation';
}
100 changes: 100 additions & 0 deletions tests/Platform/Bridge/Azure/OpenAI/WhisperModelClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\Platform\Bridge\Azure\OpenAI;

use PhpLlm\LlmChain\Platform\Bridge\Azure\OpenAI\WhisperModelClient;
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper;
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper\Task;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
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());
}
}
94 changes: 94 additions & 0 deletions tests/Platform/Bridge/OpenAI/Whisper/ModelClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\Platform\Bridge\OpenAI\Whisper;

use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper;
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper\ModelClient;
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper\Task;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
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());
}
}