From 88e43bf92dc88135e0d07f2714863a93647b7401 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Mon, 6 Jan 2025 13:53:25 +0100 Subject: [PATCH 1/4] Add possibility to react to content filter violation --- src/Bridge/OpenAI/GPT/ResponseConverter.php | 12 +++++++++++- src/Exception/ContentFilterViolationException.php | 9 +++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/Exception/ContentFilterViolationException.php diff --git a/src/Bridge/OpenAI/GPT/ResponseConverter.php b/src/Bridge/OpenAI/GPT/ResponseConverter.php index 20e9e27..775f0af 100644 --- a/src/Bridge/OpenAI/GPT/ResponseConverter.php +++ b/src/Bridge/OpenAI/GPT/ResponseConverter.php @@ -33,7 +33,17 @@ public function convert(HttpResponse $response, array $options = []): LlmRespons return $this->convertStream($response); } - $data = $response->toArray(); + try { + $data = $response->toArray(); + } catch (\Throwable $e) { + $data = $response->toArray(throw: false); + + if (isset($data['error']['code']) && 'content_filter' === $data['error']['code']) { + throw new ContentFilterViolaftionException(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/ContentFilterViolationException.php b/src/Exception/ContentFilterViolationException.php new file mode 100644 index 0000000..3a96bdb --- /dev/null +++ b/src/Exception/ContentFilterViolationException.php @@ -0,0 +1,9 @@ + Date: Thu, 9 Jan 2025 11:57:51 +0100 Subject: [PATCH 2/4] - --- src/Bridge/OpenAI/GPT/ResponseConverter.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Bridge/OpenAI/GPT/ResponseConverter.php b/src/Bridge/OpenAI/GPT/ResponseConverter.php index 775f0af..b5d9232 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\ContentFilterViolationException; use PhpLlm\LlmChain\Exception\RuntimeException; use PhpLlm\LlmChain\Model\Model; use PhpLlm\LlmChain\Model\Response\Choice; @@ -39,7 +40,7 @@ public function convert(HttpResponse $response, array $options = []): LlmRespons $data = $response->toArray(throw: false); if (isset($data['error']['code']) && 'content_filter' === $data['error']['code']) { - throw new ContentFilterViolaftionException(message: $data['error']['message'], previous: $e); + throw new ContentFilterViolationException(message: $data['error']['message'], previous: $e); } throw $e; From ee7262d3cb6b5e702f7ca1f8a0ca3ecbbbe9d5ff Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Thu, 9 Jan 2025 11:58:23 +0100 Subject: [PATCH 3/4] - --- src/Bridge/OpenAI/GPT/ResponseConverter.php | 4 ++-- ...ilterViolationException.php => ContentFilterException.php} | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/Exception/{ContentFilterViolationException.php => ContentFilterException.php} (51%) diff --git a/src/Bridge/OpenAI/GPT/ResponseConverter.php b/src/Bridge/OpenAI/GPT/ResponseConverter.php index b5d9232..54c29ea 100644 --- a/src/Bridge/OpenAI/GPT/ResponseConverter.php +++ b/src/Bridge/OpenAI/GPT/ResponseConverter.php @@ -5,7 +5,7 @@ namespace PhpLlm\LlmChain\Bridge\OpenAI\GPT; use PhpLlm\LlmChain\Bridge\OpenAI\GPT; -use PhpLlm\LlmChain\Exception\ContentFilterViolationException; +use PhpLlm\LlmChain\Exception\ContentFilterException; use PhpLlm\LlmChain\Exception\RuntimeException; use PhpLlm\LlmChain\Model\Model; use PhpLlm\LlmChain\Model\Response\Choice; @@ -40,7 +40,7 @@ public function convert(HttpResponse $response, array $options = []): LlmRespons $data = $response->toArray(throw: false); if (isset($data['error']['code']) && 'content_filter' === $data['error']['code']) { - throw new ContentFilterViolationException(message: $data['error']['message'], previous: $e); + throw new ContentFilterException(message: $data['error']['message'], previous: $e); } throw $e; diff --git a/src/Exception/ContentFilterViolationException.php b/src/Exception/ContentFilterException.php similarity index 51% rename from src/Exception/ContentFilterViolationException.php rename to src/Exception/ContentFilterException.php index 3a96bdb..5936222 100644 --- a/src/Exception/ContentFilterViolationException.php +++ b/src/Exception/ContentFilterException.php @@ -4,6 +4,6 @@ namespace PhpLlm\LlmChain\Exception; -class ContentFilterViolationException extends InvalidArgumentException +class ContentFilterException extends InvalidArgumentException { } From 199838c9426d3cca0a6e14cacfc623e8100cca52 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Mon, 13 Jan 2025 13:21:23 +0100 Subject: [PATCH 4/4] - --- src/Bridge/OpenAI/GPT/ResponseConverter.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Bridge/OpenAI/GPT/ResponseConverter.php b/src/Bridge/OpenAI/GPT/ResponseConverter.php index 54c29ea..c3daa65 100644 --- a/src/Bridge/OpenAI/GPT/ResponseConverter.php +++ b/src/Bridge/OpenAI/GPT/ResponseConverter.php @@ -19,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 @@ -36,7 +37,7 @@ public function convert(HttpResponse $response, array $options = []): LlmRespons try { $data = $response->toArray(); - } catch (\Throwable $e) { + } catch (ClientExceptionInterface $e) { $data = $response->toArray(throw: false); if (isset($data['error']['code']) && 'content_filter' === $data['error']['code']) {