|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\Platform\Bridge\Ollama; |
| 13 | + |
| 14 | +use Symfony\AI\Platform\Exception\InvalidArgumentException; |
| 15 | +use Symfony\AI\Platform\Model; |
| 16 | +use Symfony\AI\Platform\ModelClientInterface; |
| 17 | +use Symfony\AI\Platform\Result\RawHttpResult; |
| 18 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Christopher Hertel <[email protected]> |
| 22 | + */ |
| 23 | +final readonly class OllamaClient implements ModelClientInterface |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + private HttpClientInterface $httpClient, |
| 27 | + private string $hostUrl, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + public function supports(Model $model): bool |
| 32 | + { |
| 33 | + return $model instanceof Ollama; |
| 34 | + } |
| 35 | + |
| 36 | + public function request(Model $model, array|string $payload, array $options = []): RawHttpResult |
| 37 | + { |
| 38 | + $response = $this->httpClient->request('POST', \sprintf('%s/api/show', $this->hostUrl), [ |
| 39 | + 'json' => [ |
| 40 | + 'model' => $model->getName(), |
| 41 | + ], |
| 42 | + ]); |
| 43 | + |
| 44 | + $modelInformationsPayload = $response->toArray(); |
| 45 | + |
| 46 | + return match (true) { |
| 47 | + \in_array('completion', $modelInformationsPayload['capabilities'], true) => $this->doCompletionRequest($payload, $options), |
| 48 | + \in_array('embedding', $modelInformationsPayload['capabilities'], true) => $this->doEmbeddingsRequest($model, $payload, $options), |
| 49 | + default => throw new InvalidArgumentException(\sprintf('Unsupported model "%s".', $model::class)), |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param array<string|int, mixed> $payload |
| 55 | + * @param array<string, mixed> $options |
| 56 | + */ |
| 57 | + private function doCompletionRequest(array|string $payload, array $options = []): RawHttpResult |
| 58 | + { |
| 59 | + // Revert Ollama's default streaming behavior |
| 60 | + $options['stream'] ??= false; |
| 61 | + |
| 62 | + return new RawHttpResult($this->httpClient->request('POST', \sprintf('%s/api/chat', $this->hostUrl), [ |
| 63 | + 'headers' => ['Content-Type' => 'application/json'], |
| 64 | + 'json' => array_merge($options, $payload), |
| 65 | + ])); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param array<string|int, mixed> $payload |
| 70 | + * @param array<string, mixed> $options |
| 71 | + */ |
| 72 | + public function doEmbeddingsRequest(Model $model, array|string $payload, array $options = []): RawHttpResult |
| 73 | + { |
| 74 | + return new RawHttpResult($this->httpClient->request('POST', \sprintf('%s/api/embed', $this->hostUrl), [ |
| 75 | + 'json' => array_merge($options, [ |
| 76 | + 'model' => $model->getName(), |
| 77 | + 'input' => $payload, |
| 78 | + ]), |
| 79 | + ])); |
| 80 | + } |
| 81 | +} |
0 commit comments