Skip to content
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
3 changes: 3 additions & 0 deletions src/platform/src/Bridge/OpenAi/Whisper/ModelClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function __construct(
if ('' === $apiKey) {
throw new InvalidArgumentException('The API key must not be empty.');
}
if (!str_starts_with($apiKey, 'sk-')) {
throw new InvalidArgumentException('The API key must start with "sk-".');
}
}

public function supports(Model $model): bool
Expand Down
39 changes: 35 additions & 4 deletions src/platform/tests/Bridge/OpenAi/Whisper/ModelClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,51 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\TestWith;
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\AI\Platform\Exception\InvalidArgumentException;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

#[CoversClass(ModelClient::class)]
#[Small]
final class ModelClientTest extends TestCase
{
public function testItThrowsExceptionWhenApiKeyIsEmpty()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The API key must not be empty.');

new ModelClient(new MockHttpClient(), '');
}

#[TestWith(['api-key-without-prefix'])]
#[TestWith(['pk-api-key'])]
#[TestWith(['SK-api-key'])]
#[TestWith(['skapikey'])]
#[TestWith(['sk api-key'])]
#[TestWith(['sk'])]
public function testItThrowsExceptionWhenApiKeyDoesNotStartWithSk(string $invalidApiKey)
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The API key must start with "sk-".');

new ModelClient(new MockHttpClient(), $invalidApiKey);
}

public function testItAcceptsValidApiKey()
{
$modelClient = new ModelClient(new MockHttpClient(), 'sk-valid-api-key');

$this->assertInstanceOf(ModelClient::class, $modelClient);
}

public function testItSupportsWhisperModel()
{
$client = new ModelClient(new MockHttpClient(), 'test-key');
$client = new ModelClient(new MockHttpClient(), 'sk-test-key');
$this->assertTrue($client->supports(new Whisper()));
}

Expand All @@ -41,7 +72,7 @@ function ($method, $url): MockResponse {
},
]);

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

$this->assertSame(1, $httpClient->getRequestsCount());
Expand All @@ -58,7 +89,7 @@ function ($method, $url): MockResponse {
},
]);

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

$this->assertSame(1, $httpClient->getRequestsCount());
Expand All @@ -75,7 +106,7 @@ function ($method, $url): MockResponse {
},
]);

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

$this->assertSame(1, $httpClient->getRequestsCount());
Expand Down