-
Notifications
You must be signed in to change notification settings - Fork 0
Added ollama agent for embeddings #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d59a69b
added new embedding model -> ollama(poc)
ArnabChatterjee20k e500275
updated ollama agent
ArnabChatterjee20k 54dc612
* added explicit ollama version
ArnabChatterjee20k cbb0365
updated docker files
ArnabChatterjee20k 0abffab
updated docker compose structure
ArnabChatterjee20k b448a4d
linting
ArnabChatterjee20k 31e44d8
* added full error messages from the exception
ArnabChatterjee20k 9c8f63b
linting
ArnabChatterjee20k 22ed668
linting
ArnabChatterjee20k 688a636
fixed the typings for the json response in the ollama json response
ArnabChatterjee20k a23055d
linting issues fixed
ArnabChatterjee20k 63bc0da
added checks for invalid model setting
ArnabChatterjee20k 083113d
Update docker-compose.yml
abnegate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| FROM ollama/ollama:0.12.7 | ||
|
|
||
| # Preload specific models | ||
| ARG MODELS="embeddinggemma" | ||
| ENV OLLAMA_KEEP_ALIVE=24h | ||
|
|
||
| # Pre-pull models at build time for Docker layer caching | ||
| RUN ollama serve & \ | ||
| sleep 5 && \ | ||
| for m in $MODELS; do \ | ||
| echo "Pulling model $m..."; \ | ||
| ollama pull $m || exit 1; \ | ||
| done && \ | ||
| pkill ollama | ||
|
|
||
| # Expose Ollama default port | ||
| EXPOSE 11434 | ||
|
|
||
| # On container start, quickly ensure models exist (no re-download unless missing) | ||
| ENTRYPOINT ["/bin/bash", "-c", "(sleep 2; for m in $MODELS; do ollama list | grep -q $m || ollama pull $m; done) & exec ollama $0"] | ||
| CMD ["serve"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Agents\Adapters; | ||
|
|
||
| use Utopia\Agents\Adapter; | ||
| use Utopia\Agents\Message; | ||
| use Utopia\Fetch\Client; | ||
|
|
||
| class Ollama extends Adapter | ||
| { | ||
| /** | ||
| * EmbeddingGemma - Gemma embedding model for Ollama | ||
| */ | ||
| public const MODEL_EMBEDDING_GEMMA = 'embeddinggemma'; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| protected string $model; | ||
|
|
||
| private string $endpoint = 'http://ollama:11434/api/embed'; | ||
|
|
||
| public const MODELS = [self::MODEL_EMBEDDING_GEMMA]; | ||
|
|
||
| /** | ||
| * Embedding dimensions of specific embedding model | ||
| */ | ||
| protected const DIMENSIONS = [ | ||
| self::MODEL_EMBEDDING_GEMMA => 768, | ||
| ]; | ||
abnegate marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Create a new Ollama adapter (no API key required for local call) | ||
| * | ||
| * @param string $model | ||
| * @param int $timeout | ||
| */ | ||
| public function __construct( | ||
| string $model = self::MODEL_EMBEDDING_GEMMA, | ||
| int $timeout = 90 | ||
| ) { | ||
| if (! in_array($model, self::MODELS, true)) { | ||
| throw new \InvalidArgumentException("Invalid model: {$model}. Supported models: ".implode(', ', self::MODELS)); | ||
| } | ||
|
|
||
| $this->model = $model; | ||
| $this->setTimeout($timeout); | ||
| } | ||
|
|
||
| /** | ||
| * Embedding generation (Ollama only supports embeddings, not chat) | ||
| * | ||
| * @param string $text | ||
| * @return array{ | ||
| * embedding: array<int, float>, | ||
| * total_duration: int|null, | ||
| * load_duration: int|null | ||
| * } | ||
| * | ||
| * @throws \Exception | ||
| */ | ||
| public function embed(string $text): array | ||
| { | ||
| $client = new Client(); | ||
| $client->setTimeout($this->timeout); | ||
| $client->addHeader('Content-Type', 'application/json'); | ||
| $payload = [ | ||
| 'model' => $this->model, | ||
| 'input' => $text, | ||
| ]; | ||
| $response = $client->fetch( | ||
| $this->getEndpoint(), | ||
| Client::METHOD_POST, | ||
| $payload | ||
| ); | ||
| $body = $response->getBody(); | ||
| $json = is_string($body) ? json_decode($body, true) : null; | ||
|
|
||
| if (! is_array($json)) { | ||
| throw new \Exception('Invalid response format received from the API'); | ||
| } | ||
|
|
||
| if (isset($json['error'])) { | ||
| throw new \Exception($json['error'], $response->getStatusCode()); | ||
| } | ||
|
|
||
| return [ | ||
| 'embedding' => $json['embeddings'][0] ?? [], | ||
| 'total_duration' => $json['total_duration'] ?? null, | ||
| 'load_duration' => $json['load_duration'] ?? null, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Get available models for embeddings (for now, only embeddinggemma) | ||
| * | ||
| * @return array<string> | ||
| */ | ||
| public function getModels(): array | ||
| { | ||
| return self::MODELS; | ||
| } | ||
|
|
||
| /** | ||
| * Get currently selected embedding model | ||
| * | ||
| * @return string | ||
| */ | ||
| public function getModel(): string | ||
| { | ||
| return $this->model; | ||
| } | ||
|
|
||
| /** | ||
| * get embedding dimenion of the current model | ||
| */ | ||
| public function getEmbeddingDimension(): int | ||
| { | ||
| return self::DIMENSIONS[$this->model]; | ||
| } | ||
ArnabChatterjee20k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Set model to use for embedding | ||
| * | ||
| * @param string $model | ||
| * @return self | ||
| */ | ||
| public function setModel(string $model): self | ||
| { | ||
| if (! in_array($model, self::MODELS, true)) { | ||
| throw new \InvalidArgumentException("Invalid model: {$model}. Supported models: ".implode(', ', self::MODELS)); | ||
| } | ||
| $this->model = $model; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Not applicable for embedding-only adapters. | ||
| * | ||
| * @param array<\Utopia\Agents\Message> $messages | ||
| * @param callable|null $listener | ||
| * | ||
| * @throws \Exception | ||
| */ | ||
| public function send(array $messages, ?callable $listener = null): Message | ||
| { | ||
| throw new \Exception('OllamaAdapter does not support chat or messages. Use embed() instead.'); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Embeddings do not support schema. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function isSchemaSupported(): bool | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Get the adapter name | ||
| * | ||
| * @return string | ||
| */ | ||
| public function getName(): string | ||
| { | ||
| return 'ollama'; | ||
| } | ||
|
|
||
| /** | ||
| * Error formatter (minimal) | ||
| * | ||
| * @param mixed $json | ||
| * @return string | ||
| */ | ||
| protected function formatErrorMessage($json): string | ||
| { | ||
| if (! is_array($json)) { | ||
| return '(unknown_error) Unknown error'; | ||
| } | ||
|
|
||
| return $json['error'] ?? ($json['message'] ?? 'Unknown error'); | ||
| } | ||
|
|
||
| /** | ||
| * Get the API endpoint | ||
| * | ||
| * @return string | ||
| */ | ||
| public function getEndpoint(): string | ||
| { | ||
| return $this->endpoint; | ||
| } | ||
|
|
||
| /** | ||
| * Set the API endpoint | ||
| * | ||
| * @param string $endpoint | ||
| * @return self | ||
| */ | ||
| public function setEndpoint(string $endpoint): self | ||
| { | ||
| $this->endpoint = $endpoint; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function getSupportForEmbeddings(): bool | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.