Skip to content
Draft
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
10 changes: 10 additions & 0 deletions examples/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\AI\Platform\Exception\ModelException;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Dotenv\Dotenv;
Expand Down Expand Up @@ -52,3 +53,12 @@ function logger(): LoggerInterface

return new ConsoleLogger(new ConsoleOutput($verbosity));
}

set_exception_handler(function ($exception) {
if ($exception instanceof ModelException) {
echo $exception->getMessage().\PHP_EOL;
exit(1);
}

throw $exception;
});
5 changes: 5 additions & 0 deletions src/platform/src/Bridge/Anthropic/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\AI\Platform\Bridge\Anthropic;

use Symfony\AI\Platform\Exception\ModelException;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\Result\RawHttpResult;
Expand Down Expand Up @@ -44,6 +45,10 @@ public function convert(RawHttpResult|RawResultInterface $result, array $options

$data = $result->getData();

if (isset($data['error'])) {
throw new ModelException($data['error']['message'], $data['error']);
}

if (!isset($data['content']) || [] === $data['content']) {
throw new RuntimeException('Response does not contain any content');
}
Expand Down
6 changes: 3 additions & 3 deletions src/platform/src/Bridge/OpenAI/GPT/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\AI\Platform\Bridge\OpenAI\GPT;

use Symfony\AI\Platform\Bridge\OpenAI\GPT;
use Symfony\AI\Platform\Exception\ContentFilterException;
use Symfony\AI\Platform\Exception\ModelException;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\Result\Choice;
Expand Down Expand Up @@ -49,8 +49,8 @@ public function convert(RawResultInterface|RawHttpResult $result, array $options

$data = $result->getData();

if (isset($data['error']['code']) && 'content_filter' === $data['error']['code']) {
throw new ContentFilterException($data['error']['message']);
if (isset($data['error'])) {
throw new ModelException($data['error']['message'], $data['error']);
}

if (!isset($data['choices'])) {
Expand Down
7 changes: 6 additions & 1 deletion src/platform/src/Bridge/Replicate/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\AI\Platform\Bridge\Replicate;

use Symfony\AI\Platform\Exception\ModelException;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
Expand Down Expand Up @@ -40,7 +41,11 @@ public function request(string $model, string $endpoint, array $body): ResponseI
'auth_bearer' => $this->apiKey,
'json' => ['input' => $body],
]);
$data = $response->toArray();
$data = $response->toArray(false);

if (isset($data['detail'])) {
throw new ModelException($data['detail'], $data);
}

while (!\in_array($data['status'], ['succeeded', 'failed', 'canceled'], true)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the response if 4xx or 5xx, status might not exist.

$this->clock->sleep(1); // we need to wait until the prediction is ready
Expand Down
5 changes: 5 additions & 0 deletions src/platform/src/Bridge/Voyage/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\AI\Platform\Bridge\Voyage;

use Symfony\AI\Platform\Exception\ModelException;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\Result\RawResultInterface;
Expand All @@ -33,6 +34,10 @@ public function convert(RawResultInterface $result, array $options = []): Result
{
$result = $result->getData();

if (isset($result['detail'])) {
throw new ModelException($result['detail']);
}

if (!isset($result['data'])) {
throw new RuntimeException('Response does not contain embedding data');
}
Expand Down
22 changes: 22 additions & 0 deletions src/platform/src/Exception/ModelException.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 <[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\Exception;

final class ModelException extends \Exception implements ExceptionInterface
{
public function __construct(
string $message,
public array $errorDetails = [],
) {
parent::__construct($message);
}
}
Loading