Skip to content

Commit e4863e5

Browse files
authored
refactor: move error handling to chain + logging (#182)
1 parent fc8db39 commit e4863e5

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/Chain.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111
use PhpLlm\LlmChain\Chain\OutputProcessor;
1212
use PhpLlm\LlmChain\Exception\InvalidArgumentException;
1313
use PhpLlm\LlmChain\Exception\MissingModelSupport;
14+
use PhpLlm\LlmChain\Exception\RuntimeException;
1415
use PhpLlm\LlmChain\Model\LanguageModel;
1516
use PhpLlm\LlmChain\Model\Message\MessageBagInterface;
1617
use PhpLlm\LlmChain\Model\Response\AsyncResponse;
1718
use PhpLlm\LlmChain\Model\Response\ResponseInterface;
19+
use Psr\Log\LoggerInterface;
20+
use Psr\Log\NullLogger;
21+
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
22+
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
1823

1924
final readonly class Chain implements ChainInterface
2025
{
@@ -37,6 +42,7 @@ public function __construct(
3742
private LanguageModel $llm,
3843
iterable $inputProcessor = [],
3944
iterable $outputProcessor = [],
45+
private LoggerInterface $logger = new NullLogger(),
4046
) {
4147
$this->inputProcessor = $this->initializeProcessors($inputProcessor, InputProcessor::class);
4248
$this->outputProcessor = $this->initializeProcessors($outputProcessor, OutputProcessor::class);
@@ -58,10 +64,21 @@ public function call(MessageBagInterface $messages, array $options = []): Respon
5864
throw MissingModelSupport::forImageInput($llm::class);
5965
}
6066

61-
$response = $this->platform->request($llm, $messages, $options);
67+
try {
68+
$response = $this->platform->request($llm, $messages, $options);
6269

63-
if ($response instanceof AsyncResponse) {
64-
$response = $response->unwrap();
70+
if ($response instanceof AsyncResponse) {
71+
$response = $response->unwrap();
72+
}
73+
} catch (ClientExceptionInterface $e) {
74+
$message = $e->getMessage();
75+
$content = $e->getResponse()->toArray(false);
76+
77+
$this->logger->debug($message, $content);
78+
79+
throw new InvalidArgumentException('' === $message ? 'Invalid request to model or platform' : $message, 0, $e);
80+
} catch (HttpExceptionInterface $e) {
81+
throw new RuntimeException('Failed to request model', 0, $e);
6582
}
6683

6784
$output = new Output($llm, $response, $messages, $options);

src/Platform.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44

55
namespace PhpLlm\LlmChain;
66

7-
use PhpLlm\LlmChain\Exception\InvalidArgumentException;
87
use PhpLlm\LlmChain\Exception\RuntimeException;
98
use PhpLlm\LlmChain\Model\Model;
109
use PhpLlm\LlmChain\Model\Response\AsyncResponse;
1110
use PhpLlm\LlmChain\Model\Response\ResponseInterface;
1211
use PhpLlm\LlmChain\Platform\ModelClient;
1312
use PhpLlm\LlmChain\Platform\ResponseConverter;
14-
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
15-
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
1613
use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse;
1714

1815
final readonly class Platform implements PlatformInterface
@@ -41,17 +38,9 @@ public function request(Model $model, array|string|object $input, array $options
4138
{
4239
$options = array_merge($model->getOptions(), $options);
4340

44-
try {
45-
$response = $this->doRequest($model, $input, $options);
41+
$response = $this->doRequest($model, $input, $options);
4642

47-
return $this->convertResponse($model, $input, $response, $options);
48-
} catch (ClientExceptionInterface $e) {
49-
$message = $e->getMessage();
50-
51-
throw new InvalidArgumentException('' === $message ? 'Invalid request to model or platform' : $message, 0, $e);
52-
} catch (HttpExceptionInterface $e) {
53-
throw new RuntimeException('Failed to request model', 0, $e);
54-
}
43+
return $this->convertResponse($model, $input, $response, $options);
5544
}
5645

5746
/**

0 commit comments

Comments
 (0)