|
5 | 5 | namespace PhpLlm\LlmChain\Bridge\Anthropic;
|
6 | 6 |
|
7 | 7 | use PhpLlm\LlmChain\Chain\Toolbox\Metadata;
|
8 |
| -use PhpLlm\LlmChain\Exception\RuntimeException; |
9 | 8 | use PhpLlm\LlmChain\Model\Message\AssistantMessage;
|
10 | 9 | use PhpLlm\LlmChain\Model\Message\MessageBagInterface;
|
11 | 10 | use PhpLlm\LlmChain\Model\Message\MessageInterface;
|
12 | 11 | use PhpLlm\LlmChain\Model\Message\ToolCallMessage;
|
13 | 12 | use PhpLlm\LlmChain\Model\Model;
|
14 |
| -use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse; |
15 |
| -use PhpLlm\LlmChain\Model\Response\StreamResponse; |
16 |
| -use PhpLlm\LlmChain\Model\Response\TextResponse; |
17 | 13 | use PhpLlm\LlmChain\Model\Response\ToolCall;
|
18 |
| -use PhpLlm\LlmChain\Model\Response\ToolCallResponse; |
19 |
| -use PhpLlm\LlmChain\Platform\ModelClient; |
20 |
| -use PhpLlm\LlmChain\Platform\ResponseConverter; |
21 |
| -use Symfony\Component\HttpClient\Chunk\ServerSentEvent; |
| 14 | +use PhpLlm\LlmChain\Platform\ModelClient as PlatformModelClient; |
22 | 15 | use Symfony\Component\HttpClient\EventSourceHttpClient;
|
23 |
| -use Symfony\Component\HttpClient\Exception\JsonException; |
24 | 16 | use Symfony\Contracts\HttpClient\HttpClientInterface;
|
25 | 17 | use Symfony\Contracts\HttpClient\ResponseInterface;
|
26 | 18 | use Webmozart\Assert\Assert;
|
27 | 19 |
|
28 |
| -final readonly class ModelHandler implements ModelClient, ResponseConverter |
| 20 | +final readonly class ModelClient implements PlatformModelClient |
29 | 21 | {
|
30 | 22 | private EventSourceHttpClient $httpClient;
|
31 | 23 |
|
@@ -108,55 +100,4 @@ public function request(Model $model, object|array|string $input, array $options
|
108 | 100 | 'json' => array_merge($options, $body),
|
109 | 101 | ]);
|
110 | 102 | }
|
111 |
| - |
112 |
| - public function convert(ResponseInterface $response, array $options = []): LlmResponse |
113 |
| - { |
114 |
| - if ($options['stream'] ?? false) { |
115 |
| - return new StreamResponse($this->convertStream($response)); |
116 |
| - } |
117 |
| - |
118 |
| - $data = $response->toArray(); |
119 |
| - |
120 |
| - if (!isset($data['content']) || 0 === count($data['content'])) { |
121 |
| - throw new RuntimeException('Response does not contain any content'); |
122 |
| - } |
123 |
| - |
124 |
| - if (!isset($data['content'][0]['text'])) { |
125 |
| - throw new RuntimeException('Response content does not contain any text'); |
126 |
| - } |
127 |
| - |
128 |
| - $toolCalls = []; |
129 |
| - foreach ($data['content'] as $content) { |
130 |
| - if ('tool_use' === $content['type']) { |
131 |
| - $toolCalls[] = new ToolCall($content['id'], $content['name'], $content['input']); |
132 |
| - } |
133 |
| - } |
134 |
| - if (!empty($toolCalls)) { |
135 |
| - return new ToolCallResponse(...$toolCalls); |
136 |
| - } |
137 |
| - |
138 |
| - return new TextResponse($data['content'][0]['text']); |
139 |
| - } |
140 |
| - |
141 |
| - private function convertStream(ResponseInterface $response): \Generator |
142 |
| - { |
143 |
| - foreach ((new EventSourceHttpClient())->stream($response) as $chunk) { |
144 |
| - if (!$chunk instanceof ServerSentEvent || '[DONE]' === $chunk->getData()) { |
145 |
| - continue; |
146 |
| - } |
147 |
| - |
148 |
| - try { |
149 |
| - $data = $chunk->getArrayData(); |
150 |
| - } catch (JsonException) { |
151 |
| - // try catch only needed for Symfony 6.4 |
152 |
| - continue; |
153 |
| - } |
154 |
| - |
155 |
| - if ('content_block_delta' != $data['type'] || !isset($data['delta']['text'])) { |
156 |
| - continue; |
157 |
| - } |
158 |
| - |
159 |
| - yield $data['delta']['text']; |
160 |
| - } |
161 |
| - } |
162 | 103 | }
|
0 commit comments