Skip to content

Commit 54dc612

Browse files
* added explicit ollama version
* added getEmbeddingDimension for getting dimension of the current embedding model
1 parent e500275 commit 54dc612

9 files changed

Lines changed: 69 additions & 2 deletions

File tree

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ services:
2525
- utopia
2626

2727
ollama:
28-
build:
28+
build:
2929
context: .
3030
dockerfile: ollama.dockerfile
3131
container_name: ollama

ollama.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM ollama/ollama:latest
1+
FROM ollama/ollama:0.12.7
22

33
# Preload specific models
44
ENV MODELS="embeddinggemma"

src/Agents/Adapter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ abstract public function getSupportForEmbeddings(): bool;
108108
*/
109109
abstract public function embed(string $text): array;
110110

111+
/**
112+
* get embedding dimenion of the current model
113+
*/
114+
abstract public function getEmbeddingDimension(): int;
115+
111116
/**
112117
* Format error message
113118
*

src/Agents/Adapters/Anthropic.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,9 @@ public function embed(string $text): array
467467
{
468468
throw new \Exception('Embeddings are not supported for this adapter.');
469469
}
470+
471+
public function getEmbeddingDimension(): int
472+
{
473+
throw new \Exception('Embeddings are not supported for this adapter.');
474+
}
470475
}

src/Agents/Adapters/Deepseek.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,9 @@ public function embed(string $text): array
327327
{
328328
throw new \Exception('Embeddings are not supported for this adapter.');
329329
}
330+
331+
public function getEmbeddingDimension(): int
332+
{
333+
throw new \Exception('Embeddings are not supported for this adapter.');
334+
}
330335
}

src/Agents/Adapters/Gemini.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,9 @@ public function embed(string $text): array
358358
{
359359
throw new \Exception('Embeddings are not supported for this adapter.');
360360
}
361+
362+
public function getEmbeddingDimension(): int
363+
{
364+
throw new \Exception('Embeddings are not supported for this adapter.');
365+
}
361366
}

src/Agents/Adapters/Ollama.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class Ollama extends Adapter
2727

2828
public const MODELS = [self::MODEL_EMBEDDING_GEMMA];
2929

30+
/**
31+
* Embedding dimensions of specific embedding model
32+
*/
33+
protected const DIMENSIONS = [
34+
self::MODEL_EMBEDDING_GEMMA => 768,
35+
];
36+
3037
/**
3138
* Create a new Ollama adapter (no API key required for local call)
3239
*
@@ -101,6 +108,14 @@ public function getModel(): string
101108
return $this->model;
102109
}
103110

111+
/**
112+
* get embedding dimenion of the current model
113+
*/
114+
public function getEmbeddingDimension(): int
115+
{
116+
return self::DIMENSIONS[$this->model];
117+
}
118+
104119
/**
105120
* Set model to use for embedding
106121
*

src/Agents/Adapters/OpenAI.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,9 @@ public function embed(string $text): array
424424
{
425425
throw new \Exception('Embeddings are not supported for this adapter.');
426426
}
427+
428+
public function getEmbeddingDimension(): int
429+
{
430+
throw new \Exception('Embeddings are not supported for this adapter.');
431+
}
427432
}

tests/Agents/AgentTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,31 @@ public function testEmbedReturnsArrayWithEmbeddingAdapter(): void
108108
$this->assertArrayHasKey('load_duration', $result);
109109
$this->assertIsArray($result['embedding']);
110110
}
111+
112+
public function testEmbeddingDImensions(): void
113+
{
114+
$ollama = new Ollama();
115+
$agent = new Agent($ollama);
116+
$content = [
117+
'hi',
118+
'hello world',
119+
'this is a short sentence for testing',
120+
'In the midst of chaos, there is also opportunity — this simple truth often reveals itself when we least expect it.',
121+
'Artificial intelligence models like Ollama generate embeddings that map words, phrases, and even full documents into high-dimensional vector spaces where semantic similarity can be computed efficiently.', // long paragraph (~35 words)
122+
str_repeat('This is a repeated pattern. ', 100),
123+
];
124+
foreach ($content as $text) {
125+
$result = $agent->embed($text);
126+
$this->assertIsArray($result);
127+
$this->assertArrayHasKey('embedding', $result);
128+
$this->assertArrayHasKey('total_duration', $result);
129+
$this->assertArrayHasKey('load_duration', $result);
130+
$this->assertIsArray($result['embedding']);
131+
132+
$embedding = $result['embedding'];
133+
$dimension = count($embedding);
134+
135+
$this->assertEquals($ollama->getEmbeddingDimension(), $dimension);
136+
}
137+
}
111138
}

0 commit comments

Comments
 (0)