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
8 changes: 8 additions & 0 deletions src/platform/src/Bridge/OpenAi/Gpt/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\AI\Platform\Bridge\OpenAi\Gpt;

use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
use Symfony\AI\Platform\Exception\AuthenticationException;
use Symfony\AI\Platform\Exception\ContentFilterException;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\Model;
Expand Down Expand Up @@ -42,6 +43,13 @@ public function supports(Model $model): bool

public function convert(RawResultInterface|RawHttpResult $result, array $options = []): ResultInterface
{
$response = $result->getObject();

if (401 === $response->getStatusCode()) {
$errorMessage = json_decode($response->getContent(false), true)['error']['message'];
throw new AuthenticationException($errorMessage);
}

if ($options['stream'] ?? false) {
return new StreamResult($this->convertStream($result->getObject()));
}
Expand Down
22 changes: 22 additions & 0 deletions src/platform/src/Exception/AuthenticationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

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

namespace Symfony\AI\Platform\Exception;

/**
* Exception thrown when API authentication fails.
*
* @author Vitalii Kyktov <vitalii.kyktov@gmail.com>
* @author Dmytro Liashko <dmlyashko@gmail.com>
*/
class AuthenticationException extends RuntimeException
{
}
18 changes: 18 additions & 0 deletions src/platform/tests/Bridge/OpenAi/Gpt/ResultConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\OpenAi\Gpt\ResultConverter;
use Symfony\AI\Platform\Exception\AuthenticationException;
use Symfony\AI\Platform\Exception\ContentFilterException;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\Result\ChoiceResult;
Expand Down Expand Up @@ -155,6 +156,23 @@ public function getResponse(): ResponseInterface
$converter->convert(new RawHttpResult($httpResponse));
}

public function testThrowsAuthenticationExceptionOnInvalidApiKey()
{
$converter = new ResultConverter();
$httpResponse = self::createMock(ResponseInterface::class);
$httpResponse->method('getStatusCode')->willReturn(401);
$httpResponse->method('getContent')->willReturn(json_encode([
'error' => [
'message' => 'Invalid API key provided: sk-invalid',
],
]));

$this->expectException(AuthenticationException::class);
$this->expectExceptionMessage('Invalid API key provided: sk-invalid');

$converter->convert(new RawHttpResult($httpResponse));
}

public function testThrowsExceptionWhenNoChoices()
{
$converter = new ResultConverter();
Expand Down