|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpLlm\LlmChain\Bridge\Google; |
| 6 | + |
| 7 | +use PhpLlm\LlmChain\Exception\RuntimeException; |
| 8 | +use PhpLlm\LlmChain\Model\Message\MessageBagInterface; |
| 9 | +use PhpLlm\LlmChain\Model\Model; |
| 10 | +use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse; |
| 11 | +use PhpLlm\LlmChain\Model\Response\StreamResponse; |
| 12 | +use PhpLlm\LlmChain\Model\Response\TextResponse; |
| 13 | +use PhpLlm\LlmChain\Platform\ModelClient; |
| 14 | +use PhpLlm\LlmChain\Platform\ResponseConverter; |
| 15 | +use Symfony\Component\HttpClient\Chunk\ServerSentEvent; |
| 16 | +use Symfony\Component\HttpClient\EventSourceHttpClient; |
| 17 | +use Symfony\Component\HttpClient\Exception\JsonException; |
| 18 | +use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; |
| 19 | +use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; |
| 20 | +use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; |
| 21 | +use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; |
| 22 | +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 23 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 24 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 25 | +use Webmozart\Assert\Assert; |
| 26 | + |
| 27 | +final readonly class ModelHandler implements ModelClient, ResponseConverter |
| 28 | +{ |
| 29 | + private EventSourceHttpClient $httpClient; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + HttpClientInterface $httpClient, |
| 33 | + #[\SensitiveParameter] private string $apiKey, |
| 34 | + ) { |
| 35 | + $this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient); |
| 36 | + } |
| 37 | + |
| 38 | + public function supports(Model $model, array|string|object $input): bool |
| 39 | + { |
| 40 | + return $model instanceof GoogleModel && $input instanceof MessageBagInterface; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @throws TransportExceptionInterface |
| 45 | + */ |
| 46 | + public function request(Model $model, object|array|string $input, array $options = []): ResponseInterface |
| 47 | + { |
| 48 | + Assert::isInstanceOf($input, MessageBagInterface::class); |
| 49 | + |
| 50 | + $body = new GoogleRequestBodyProducer($input); |
| 51 | + |
| 52 | + return $this->httpClient->request('POST', sprintf('https://generativelanguage.googleapis.com/v1beta/models/%s:generateContent', $model->getVersion()), [ |
| 53 | + 'headers' => [ |
| 54 | + 'x-goog-api-key' => $this->apiKey, |
| 55 | + ], |
| 56 | + 'json' => $body, |
| 57 | + ]); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @throws TransportExceptionInterface |
| 62 | + * @throws ServerExceptionInterface |
| 63 | + * @throws RedirectionExceptionInterface |
| 64 | + * @throws DecodingExceptionInterface |
| 65 | + * @throws ClientExceptionInterface |
| 66 | + */ |
| 67 | + public function convert(ResponseInterface $response, array $options = []): LlmResponse |
| 68 | + { |
| 69 | + if ($options['stream'] ?? false) { |
| 70 | + return new StreamResponse($this->convertStream($response)); |
| 71 | + } |
| 72 | + |
| 73 | + $data = $response->toArray(); |
| 74 | + |
| 75 | + if (!isset($data['candidates'][0]['content']['parts'][0]['text'])) { |
| 76 | + throw new RuntimeException('Response does not contain any content'); |
| 77 | + } |
| 78 | + |
| 79 | + return new TextResponse($data['candidates'][0]['content']['parts'][0]['text']); |
| 80 | + } |
| 81 | + |
| 82 | + private function convertStream(ResponseInterface $response): \Generator |
| 83 | + { |
| 84 | + foreach ((new EventSourceHttpClient())->stream($response) as $chunk) { |
| 85 | + if (!$chunk instanceof ServerSentEvent || '[DONE]' === $chunk->getData()) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + try { |
| 90 | + $data = $chunk->getArrayData(); |
| 91 | + } catch (JsonException) { |
| 92 | + // try catch only needed for Symfony 6.4 |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + if (!isset($data['candidates'][0]['content']['parts'][0]['text'])) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + yield $data['candidates'][0]['content']['parts'][0]['text']; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments