11
11
use PhpLlm \LlmChain \Chain \OutputProcessor ;
12
12
use PhpLlm \LlmChain \Exception \InvalidArgumentException ;
13
13
use PhpLlm \LlmChain \Exception \MissingModelSupport ;
14
+ use PhpLlm \LlmChain \Exception \RuntimeException ;
14
15
use PhpLlm \LlmChain \Model \LanguageModel ;
15
16
use PhpLlm \LlmChain \Model \Message \MessageBagInterface ;
16
17
use PhpLlm \LlmChain \Model \Response \AsyncResponse ;
17
18
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 ;
18
23
19
24
final readonly class Chain implements ChainInterface
20
25
{
@@ -37,6 +42,7 @@ public function __construct(
37
42
private LanguageModel $ llm ,
38
43
iterable $ inputProcessor = [],
39
44
iterable $ outputProcessor = [],
45
+ private LoggerInterface $ logger = new NullLogger (),
40
46
) {
41
47
$ this ->inputProcessor = $ this ->initializeProcessors ($ inputProcessor , InputProcessor::class);
42
48
$ this ->outputProcessor = $ this ->initializeProcessors ($ outputProcessor , OutputProcessor::class);
@@ -58,10 +64,21 @@ public function call(MessageBagInterface $messages, array $options = []): Respon
58
64
throw MissingModelSupport::forImageInput ($ llm ::class);
59
65
}
60
66
61
- $ response = $ this ->platform ->request ($ llm , $ messages , $ options );
67
+ try {
68
+ $ response = $ this ->platform ->request ($ llm , $ messages , $ options );
62
69
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 );
65
82
}
66
83
67
84
$ output = new Output ($ llm , $ response , $ messages , $ options );
0 commit comments