diff --git a/src/Bridge/OpenAI/GPT/ResponseConverter.php b/src/Bridge/OpenAI/GPT/ResponseConverter.php index 20e9e27..c3daa65 100644 --- a/src/Bridge/OpenAI/GPT/ResponseConverter.php +++ b/src/Bridge/OpenAI/GPT/ResponseConverter.php @@ -5,6 +5,7 @@ namespace PhpLlm\LlmChain\Bridge\OpenAI\GPT; use PhpLlm\LlmChain\Bridge\OpenAI\GPT; +use PhpLlm\LlmChain\Exception\ContentFilterException; use PhpLlm\LlmChain\Exception\RuntimeException; use PhpLlm\LlmChain\Model\Model; use PhpLlm\LlmChain\Model\Response\Choice; @@ -18,6 +19,7 @@ use Symfony\Component\HttpClient\Chunk\ServerSentEvent; use Symfony\Component\HttpClient\EventSourceHttpClient; use Symfony\Component\HttpClient\Exception\JsonException; +use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse; final class ResponseConverter implements PlatformResponseConverter @@ -33,7 +35,17 @@ public function convert(HttpResponse $response, array $options = []): LlmRespons return $this->convertStream($response); } - $data = $response->toArray(); + try { + $data = $response->toArray(); + } catch (ClientExceptionInterface $e) { + $data = $response->toArray(throw: false); + + if (isset($data['error']['code']) && 'content_filter' === $data['error']['code']) { + throw new ContentFilterException(message: $data['error']['message'], previous: $e); + } + + throw $e; + } if (!isset($data['choices'])) { throw new RuntimeException('Response does not contain choices'); diff --git a/src/Exception/ContentFilterException.php b/src/Exception/ContentFilterException.php new file mode 100644 index 0000000..5936222 --- /dev/null +++ b/src/Exception/ContentFilterException.php @@ -0,0 +1,9 @@ +