Skip to content

Commit bbd06dc

Browse files
authored
feat: Add possibility to react to content filter violation (#192)
* Add possibility to react to content filter violation * - * - * -
1 parent 8629d2c commit bbd06dc

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Bridge/OpenAI/GPT/ResponseConverter.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpLlm\LlmChain\Bridge\OpenAI\GPT;
66

77
use PhpLlm\LlmChain\Bridge\OpenAI\GPT;
8+
use PhpLlm\LlmChain\Exception\ContentFilterException;
89
use PhpLlm\LlmChain\Exception\RuntimeException;
910
use PhpLlm\LlmChain\Model\Model;
1011
use PhpLlm\LlmChain\Model\Response\Choice;
@@ -18,6 +19,7 @@
1819
use Symfony\Component\HttpClient\Chunk\ServerSentEvent;
1920
use Symfony\Component\HttpClient\EventSourceHttpClient;
2021
use Symfony\Component\HttpClient\Exception\JsonException;
22+
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
2123
use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse;
2224

2325
final class ResponseConverter implements PlatformResponseConverter
@@ -33,7 +35,17 @@ public function convert(HttpResponse $response, array $options = []): LlmRespons
3335
return $this->convertStream($response);
3436
}
3537

36-
$data = $response->toArray();
38+
try {
39+
$data = $response->toArray();
40+
} catch (ClientExceptionInterface $e) {
41+
$data = $response->toArray(throw: false);
42+
43+
if (isset($data['error']['code']) && 'content_filter' === $data['error']['code']) {
44+
throw new ContentFilterException(message: $data['error']['message'], previous: $e);
45+
}
46+
47+
throw $e;
48+
}
3749

3850
if (!isset($data['choices'])) {
3951
throw new RuntimeException('Response does not contain choices');
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\LlmChain\Exception;
6+
7+
class ContentFilterException extends InvalidArgumentException
8+
{
9+
}

0 commit comments

Comments
 (0)