diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fe354cb..17b946c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,12 +16,12 @@ jobs: services: chroma-wo-auth: - image: chromadb/chroma + image: chromadb/chroma:0.5.0 ports: - 8000:8000 chroma-w-auth: - image: chromadb/chroma + image: chromadb/chroma:0.5.0 ports: - 8001:8000 env: diff --git a/docker-compose.yml b/docker-compose.yml index b3a9327..6dd2b5f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,12 +2,12 @@ version: '3.9' services: chroma_wo_auth: - image: 'chromadb/chroma' + image: 'chromadb/chroma:0.5.0' ports: - '8000:8000' chroma_w_auth: - image: 'chromadb/chroma' + image: 'chromadb/chroma:0.5.0' ports: - '8001:8000' environment: diff --git a/examples/index.php b/examples/index.php index 0267aca..1b3c207 100644 --- a/examples/index.php +++ b/examples/index.php @@ -6,6 +6,7 @@ use Codewithkyrian\ChromaDB\ChromaDB; use Codewithkyrian\ChromaDB\Embeddings\JinaEmbeddingFunction; +use Codewithkyrian\ChromaDB\Embeddings\OllamaEmbeddingFunction; $chroma = ChromaDB::factory() ->withDatabase('test_database') @@ -14,22 +15,21 @@ $chroma->deleteAllCollections(); -$embeddingFunction = new JinaEmbeddingFunction( - 'jina_8cbaafb9543e42f1a2fc7430d456c3faKKPc93W8Eur5T2XjAkryfwQ9TOv8' -); +$embeddingFunction = new OllamaEmbeddingFunction(); $collection = $chroma->createCollection( name: 'test_collection', embeddingFunction: $embeddingFunction ); + $collection->add( - ids: ['hello', 'world'], - documents: ['This is a test document', 'The man is happy'] + ids: ['1', '2', '3'], + documents: ['He seems very happy', 'He was very sad when we last talked', 'She made him angry'] ); $queryResponse = $collection->query( - queryTexts: ['The man is excited'], + queryTexts: ['She annoyed him'], include: ['documents', 'distances'] ); diff --git a/src/Embeddings/HuggingFaceEmbeddingServerFunction.php b/src/Embeddings/HuggingFaceEmbeddingServerFunction.php index 87b3ad9..fdcd469 100644 --- a/src/Embeddings/HuggingFaceEmbeddingServerFunction.php +++ b/src/Embeddings/HuggingFaceEmbeddingServerFunction.php @@ -28,9 +28,9 @@ public function generate(array $texts): array try { $response = $client->post('embed', [ - 'body' => json_encode([ + 'json' => [ 'inputs' => $texts, - ]) + ] ]); } catch (GuzzleException $e) { throw new \RuntimeException('Failed to generate embeddings', 0, $e); diff --git a/src/Embeddings/OllamaEmbeddingFunction.php b/src/Embeddings/OllamaEmbeddingFunction.php new file mode 100644 index 0000000..e5684be --- /dev/null +++ b/src/Embeddings/OllamaEmbeddingFunction.php @@ -0,0 +1,53 @@ +client = new Client([ + 'base_uri' => $this->baseUrl, + 'headers' => [ + 'Content-Type' => 'application/json', + ] + ]); + } + + /** + * @inheritDoc + */ + public function generate(array $texts): array + { + try { + $embeddings = []; + + foreach ($texts as $text) { + $response = $this->client->post('api/embeddings', [ + 'json' => [ + 'prompt' => $text, + 'model' => $this->model, + ] + ]); + + $result = json_decode($response->getBody()->getContents(), true); + + $embeddings[] = $result['embedding']; + } + + return $embeddings; + } catch (\Exception $e) { + throw new \RuntimeException('Failed to generate embeddings', 0, $e); + } + } +} \ No newline at end of file