Skip to content

Commit 3f7ce55

Browse files
Merge pull request #8 from CodeWithKyrian/ollama-embedding-function
Add support for Ollama Embedding Function
2 parents 0a65490 + 222235e commit 3f7ce55

File tree

5 files changed

+65
-12
lines changed

5 files changed

+65
-12
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ jobs:
1616

1717
services:
1818
chroma-wo-auth:
19-
image: chromadb/chroma
19+
image: chromadb/chroma:0.5.0
2020
ports:
2121
- 8000:8000
2222

2323
chroma-w-auth:
24-
image: chromadb/chroma
24+
image: chromadb/chroma:0.5.0
2525
ports:
2626
- 8001:8000
2727
env:

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ version: '3.9'
22

33
services:
44
chroma_wo_auth:
5-
image: 'chromadb/chroma'
5+
image: 'chromadb/chroma:0.5.0'
66
ports:
77
- '8000:8000'
88

99
chroma_w_auth:
10-
image: 'chromadb/chroma'
10+
image: 'chromadb/chroma:0.5.0'
1111
ports:
1212
- '8001:8000'
1313
environment:

examples/index.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Codewithkyrian\ChromaDB\ChromaDB;
88
use Codewithkyrian\ChromaDB\Embeddings\JinaEmbeddingFunction;
9+
use Codewithkyrian\ChromaDB\Embeddings\OllamaEmbeddingFunction;
910

1011
$chroma = ChromaDB::factory()
1112
->withDatabase('test_database')
@@ -14,22 +15,21 @@
1415

1516
$chroma->deleteAllCollections();
1617

17-
$embeddingFunction = new JinaEmbeddingFunction(
18-
'jina_8cbaafb9543e42f1a2fc7430d456c3faKKPc93W8Eur5T2XjAkryfwQ9TOv8'
19-
);
18+
$embeddingFunction = new OllamaEmbeddingFunction();
2019

2120
$collection = $chroma->createCollection(
2221
name: 'test_collection',
2322
embeddingFunction: $embeddingFunction
2423
);
2524

25+
2626
$collection->add(
27-
ids: ['hello', 'world'],
28-
documents: ['This is a test document', 'The man is happy']
27+
ids: ['1', '2', '3'],
28+
documents: ['He seems very happy', 'He was very sad when we last talked', 'She made him angry']
2929
);
3030

3131
$queryResponse = $collection->query(
32-
queryTexts: ['The man is excited'],
32+
queryTexts: ['She annoyed him'],
3333
include: ['documents', 'distances']
3434
);
3535

src/Embeddings/HuggingFaceEmbeddingServerFunction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public function generate(array $texts): array
2828

2929
try {
3030
$response = $client->post('embed', [
31-
'body' => json_encode([
31+
'json' => [
3232
'inputs' => $texts,
33-
])
33+
]
3434
]);
3535
} catch (GuzzleException $e) {
3636
throw new \RuntimeException('Failed to generate embeddings', 0, $e);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
namespace Codewithkyrian\ChromaDB\Embeddings;
7+
8+
use GuzzleHttp\Client;
9+
10+
class OllamaEmbeddingFunction implements EmbeddingFunction
11+
{
12+
private Client $client;
13+
14+
public function __construct(
15+
public readonly string $baseUrl = 'http://localhost:11434',
16+
public readonly string $model = 'all-minilm',
17+
)
18+
{
19+
$this->client = new Client([
20+
'base_uri' => $this->baseUrl,
21+
'headers' => [
22+
'Content-Type' => 'application/json',
23+
]
24+
]);
25+
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function generate(array $texts): array
31+
{
32+
try {
33+
$embeddings = [];
34+
35+
foreach ($texts as $text) {
36+
$response = $this->client->post('api/embeddings', [
37+
'json' => [
38+
'prompt' => $text,
39+
'model' => $this->model,
40+
]
41+
]);
42+
43+
$result = json_decode($response->getBody()->getContents(), true);
44+
45+
$embeddings[] = $result['embedding'];
46+
}
47+
48+
return $embeddings;
49+
} catch (\Exception $e) {
50+
throw new \RuntimeException('Failed to generate embeddings', 0, $e);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)