|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Utopia\Agents\Adapters; |
| 4 | + |
| 5 | +use Utopia\Agents\Adapter; |
| 6 | +use Utopia\Agents\Message; |
| 7 | +use Utopia\Fetch\Client; |
| 8 | + |
| 9 | +class Ollama extends Adapter |
| 10 | +{ |
| 11 | + /** |
| 12 | + * EmbeddingGemma - Gemma embedding model for Ollama |
| 13 | + */ |
| 14 | + public const MODEL_EMBEDDING_GEMMA = 'embeddinggemma'; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected string $model; |
| 20 | + |
| 21 | + private string $endpoint = 'http://ollama:11434/api/embed'; |
| 22 | + |
| 23 | + public const MODELS = [self::MODEL_EMBEDDING_GEMMA]; |
| 24 | + |
| 25 | + /** |
| 26 | + * Embedding dimensions of specific embedding model |
| 27 | + */ |
| 28 | + protected const DIMENSIONS = [ |
| 29 | + self::MODEL_EMBEDDING_GEMMA => 768, |
| 30 | + ]; |
| 31 | + |
| 32 | + /** |
| 33 | + * Create a new Ollama adapter (no API key required for local call) |
| 34 | + * |
| 35 | + * @param string $model |
| 36 | + * @param int $timeout |
| 37 | + */ |
| 38 | + public function __construct( |
| 39 | + string $model = self::MODEL_EMBEDDING_GEMMA, |
| 40 | + int $timeout = 90 |
| 41 | + ) { |
| 42 | + if (! in_array($model, self::MODELS, true)) { |
| 43 | + throw new \InvalidArgumentException("Invalid model: {$model}. Supported models: ".implode(', ', self::MODELS)); |
| 44 | + } |
| 45 | + |
| 46 | + $this->model = $model; |
| 47 | + $this->setTimeout($timeout); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Embedding generation (Ollama only supports embeddings, not chat) |
| 52 | + * |
| 53 | + * @param string $text |
| 54 | + * @return array{ |
| 55 | + * embedding: array<int, float>, |
| 56 | + * total_duration: int|null, |
| 57 | + * load_duration: int|null |
| 58 | + * } |
| 59 | + * |
| 60 | + * @throws \Exception |
| 61 | + */ |
| 62 | + public function embed(string $text): array |
| 63 | + { |
| 64 | + $client = new Client(); |
| 65 | + $client->setTimeout($this->timeout); |
| 66 | + $client->addHeader('Content-Type', 'application/json'); |
| 67 | + $payload = [ |
| 68 | + 'model' => $this->model, |
| 69 | + 'input' => $text, |
| 70 | + ]; |
| 71 | + $response = $client->fetch( |
| 72 | + $this->getEndpoint(), |
| 73 | + Client::METHOD_POST, |
| 74 | + $payload |
| 75 | + ); |
| 76 | + $body = $response->getBody(); |
| 77 | + $json = is_string($body) ? json_decode($body, true) : null; |
| 78 | + |
| 79 | + if (! is_array($json)) { |
| 80 | + throw new \Exception('Invalid response format received from the API'); |
| 81 | + } |
| 82 | + |
| 83 | + if (isset($json['error'])) { |
| 84 | + throw new \Exception($json['error'], $response->getStatusCode()); |
| 85 | + } |
| 86 | + |
| 87 | + return [ |
| 88 | + 'embedding' => $json['embeddings'][0] ?? [], |
| 89 | + 'total_duration' => $json['total_duration'] ?? null, |
| 90 | + 'load_duration' => $json['load_duration'] ?? null, |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get available models for embeddings (for now, only embeddinggemma) |
| 96 | + * |
| 97 | + * @return array<string> |
| 98 | + */ |
| 99 | + public function getModels(): array |
| 100 | + { |
| 101 | + return self::MODELS; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Get currently selected embedding model |
| 106 | + * |
| 107 | + * @return string |
| 108 | + */ |
| 109 | + public function getModel(): string |
| 110 | + { |
| 111 | + return $this->model; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * get embedding dimenion of the current model |
| 116 | + */ |
| 117 | + public function getEmbeddingDimension(): int |
| 118 | + { |
| 119 | + return self::DIMENSIONS[$this->model]; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Set model to use for embedding |
| 124 | + * |
| 125 | + * @param string $model |
| 126 | + * @return self |
| 127 | + */ |
| 128 | + public function setModel(string $model): self |
| 129 | + { |
| 130 | + if (! in_array($model, self::MODELS, true)) { |
| 131 | + throw new \InvalidArgumentException("Invalid model: {$model}. Supported models: ".implode(', ', self::MODELS)); |
| 132 | + } |
| 133 | + $this->model = $model; |
| 134 | + |
| 135 | + return $this; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Not applicable for embedding-only adapters. |
| 140 | + * |
| 141 | + * @param array<\Utopia\Agents\Message> $messages |
| 142 | + * @param callable|null $listener |
| 143 | + * |
| 144 | + * @throws \Exception |
| 145 | + */ |
| 146 | + public function send(array $messages, ?callable $listener = null): Message |
| 147 | + { |
| 148 | + throw new \Exception('OllamaAdapter does not support chat or messages. Use embed() instead.'); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Embeddings do not support schema. |
| 153 | + * |
| 154 | + * @return bool |
| 155 | + */ |
| 156 | + public function isSchemaSupported(): bool |
| 157 | + { |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Get the adapter name |
| 163 | + * |
| 164 | + * @return string |
| 165 | + */ |
| 166 | + public function getName(): string |
| 167 | + { |
| 168 | + return 'ollama'; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Error formatter (minimal) |
| 173 | + * |
| 174 | + * @param mixed $json |
| 175 | + * @return string |
| 176 | + */ |
| 177 | + protected function formatErrorMessage($json): string |
| 178 | + { |
| 179 | + if (! is_array($json)) { |
| 180 | + return '(unknown_error) Unknown error'; |
| 181 | + } |
| 182 | + |
| 183 | + return $json['error'] ?? ($json['message'] ?? 'Unknown error'); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Get the API endpoint |
| 188 | + * |
| 189 | + * @return string |
| 190 | + */ |
| 191 | + public function getEndpoint(): string |
| 192 | + { |
| 193 | + return $this->endpoint; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Set the API endpoint |
| 198 | + * |
| 199 | + * @param string $endpoint |
| 200 | + * @return self |
| 201 | + */ |
| 202 | + public function setEndpoint(string $endpoint): self |
| 203 | + { |
| 204 | + $this->endpoint = $endpoint; |
| 205 | + |
| 206 | + return $this; |
| 207 | + } |
| 208 | + |
| 209 | + public function getSupportForEmbeddings(): bool |
| 210 | + { |
| 211 | + return true; |
| 212 | + } |
| 213 | +} |
0 commit comments