Skip to content

Commit d6a2a55

Browse files
Initial Commit
0 parents  commit d6a2a55

40 files changed

Lines changed: 3095 additions & 0 deletions

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/.phpunit.cache
2+
/.php-cs-fixer.cache
3+
/.php-cs-fixer.php
4+
/composer.lock
5+
/phpunit.xml
6+
/vendor/
7+
*.swp
8+
*.swo
9+
playground/*
10+
.idea
11+
index.php

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Obikwelu Kyrian Sochima
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
## ChromaDB PHP
2+
3+
**A PHP library for interacting with ChromaDB vector database seamlessly.**
4+
5+
[![Build Status](https://travis-ci.org/ChromaDatabase/ChromaDB-PHP.svg?branch=master)](https://travis-ci.org/ChromaDatabase/ChromaDB-PHP)
6+
[![Coverage Status](https://coveralls.io/repos/github/ChromaDatabase/ChromaDB-PHP/badge.svg?branch=master)](https://coveralls.io/github/ChromaDatabase/ChromaDB-PHP?branch=master)
7+
[![Latest Stable Version](https://poser.pugx.org/chromadb/chromadb-php/v/stable)](https://packagist.org/packages/chromadb/chromadb-php)
8+
[![Total Downloads](https://poser.pugx.org/chromadb/chromadb-php/downloads)](https://packagist.org/packages/chromadb/chromadb-php)
9+
[![License](https://poser.pugx.org/chromadb/chromadb-php/license)](https://packagist.org/packages/chromadb/chromadb-php)
10+
11+
## Description
12+
13+
Chroma is an open-source vector database that allows you to store, search, and analyze high-dimensional data at scale.
14+
It is designed to be fast, scalable, and reliable. It makes it easy to build LLM (Large Language Model) applications and
15+
services that require high-dimensional vector search.
16+
17+
Chroma PHP provides a simple and intuitive interface for interacting with ChromaDB from PHP. It enables you to:
18+
19+
- Create, read, update, and delete documents.
20+
- Execute queries and aggregations.
21+
- Manage collections and indexes.
22+
- Handle authentication and authorization.
23+
- Utilize other ChromaDB features seamlessly.
24+
- And more...
25+
26+
## Small Example
27+
28+
```php
29+
use Codewithkyrian\ChromaDB\ChromaDB;
30+
31+
$chromaDB = ChromaDB::client();
32+
33+
// Check current ChromaDB version
34+
echo $chromaDB->version();
35+
36+
// Create a collection
37+
$collection = $chromaDB->createCollection('test-collection');
38+
39+
echo $collection->name; // test-collection
40+
echo $collection->id; // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx
41+
42+
// Insert some documents into the collection
43+
$ids = ['test1', 'test2', 'test3'];
44+
$embeddings = [
45+
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
46+
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
47+
[10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0],
48+
];
49+
$metadatas = [
50+
['url' => 'https://example.com/test1'],
51+
['url' => 'https://example.com/test2'],
52+
['url' => 'https://example.com/test3'],
53+
];
54+
55+
$collection->add($ids, $embeddings, $metadatas);
56+
57+
// Search for similar embeddings
58+
$queryResponse = $collection->query(
59+
queryEmbeddings: [
60+
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
61+
],
62+
nResults: 2
63+
);
64+
65+
// Print results
66+
echo $queryResponse->ids[0][0]; // test1
67+
echo $queryResponse->ids[0][1]; // test2
68+
69+
70+
```
71+
72+
## Requirements
73+
74+
- PHP 8.1 or higher
75+
- ChromaDB 0.4.0 or higher running in client/server mode
76+
77+
## Running ChromaDB
78+
79+
In order to use this library, you need to have ChromaDB running somewhere. You can either run it locally or in the
80+
cloud.
81+
(Chroma doesn't support cloud yet, but it will soon.)
82+
83+
For now, ChromaDB can only run in-memory in Python. You can however run it in client/server mode by either running the
84+
python
85+
project or using the docker image (recommended).
86+
87+
To run the docker image, you can use the following command:
88+
89+
```bash
90+
docker run -p 8000:8000 chromadb/chroma
91+
```
92+
93+
You can also pass in some environment variables using a `.env` file:
94+
95+
```bash
96+
docker run -p 8000:8000 --env-file .env chromadb/chroma
97+
```
98+
99+
Or if you prefer using a docker-compose file, you can use the following:
100+
101+
```yaml
102+
version: '3.9'
103+
104+
services:
105+
server:
106+
image: 'chromadb/chroma'
107+
command: uvicorn chromadb.app:app --reload --workers 1 --host 0.0.0.0 --port 8000 --log-config chromadb/log_config.yml --timeout-keep-alive 30
108+
ports:
109+
- '8000:8000'
110+
volumes:
111+
- chroma-data:/chroma/chroma
112+
environment:
113+
- IS_PERSISTENT=TRUE
114+
- CHROMA_SERVER_NOFILE=65535
115+
- ALLOW_RESET=true
116+
networks:
117+
- net
118+
119+
networks:
120+
net:
121+
driver: bridge
122+
123+
volumes:
124+
chroma-data:
125+
driver: local
126+
```
127+
128+
And then run it using:
129+
130+
```bash
131+
docker-compose up -d
132+
```
133+
134+
(Check out the [Chroma Documentation](https://docs.trychroma.com/deployment) for more information on how to run
135+
ChromaDB.)
136+
137+
Either way, you can now access ChromaDB at `http://localhost:8000`.
138+
139+
## Installation
140+
141+
```bash
142+
composer require codewithkyrian/chromadb-php
143+
```
144+
145+
## Usage
146+
147+
### Connecting to ChromaDB
148+
149+
```php
150+
use Codewithkyrian\ChromaDB\ChromaDB;
151+
152+
$chromaDB = ChromaDB::client();
153+
154+
```
155+
156+
By default, ChromaDB will try to connect to `http://localhost:8000` using the default database name `default_database`
157+
and default tenant name `default_tenant`. You can however change these values by constructing the client using the
158+
factory method:
159+
160+
```php
161+
use Codewithkyrian\ChromaDB\ChromaDB;
162+
163+
$chromaDB = ChromaDB::factory()
164+
->withHost('http://localhost')
165+
->withPort(8000)
166+
->withDatabase('new_database')
167+
->withTenant('new_tenant')
168+
->connect();
169+
```
170+
171+
If the tenant or database doesn't exist, the package will automatically create them for you.
172+
173+
### Creating a Collection
174+
175+
```php
176+
177+
$collection = $chromaDB->createCollection('test-collection');
178+
179+
```
180+
181+
If the collection already exists in the database, the package will automatically fetch it for you.
182+
183+
### Inserting Documents
184+
185+
```php
186+
$ids = ['test1', 'test2', 'test3'];
187+
$embeddings = [
188+
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
189+
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
190+
[10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0],
191+
];
192+
$metadatas = [
193+
['url' => 'https://example.com/test1'],
194+
['url' => 'https://example.com/test2'],
195+
['url' => 'https://example.com/test3'],
196+
];
197+
198+
$collection->add($ids, $embeddings, $metadatas);
199+
```
200+
To insert documents into a collection, you need to provide the following:
201+
202+
- `ids`: An array of document ids. The ids must be unique and must be strings.
203+
- `embeddings`: An array of document embeddings. The embeddings must be a 1D array of floats with a length of 10. You can
204+
compute the embeddings using any embedding model of your choice (just make sure that's what you use when querying as well).
205+
- `metadatas`: An array of document metadatas. The metadatas must be an array of key-value pairs.
206+
207+
If you don't have the embeddings, you can pass in the documents and provide an embedding function that will be used to
208+
compute the embeddings for you.
209+
210+
### Passing in Embedding Function
211+
212+
To use an embedding function, you need to pass it in as an argument when creating the collection:
213+
214+
```php
215+
use CodeWithKyrian\ChromaDB\EmbeddingFunction\EmbeddingFunctionInterface;
216+
217+
$embeddingFunction = new OpenAIEmbeddingFunction('api-key', 'org-id', 'model-name');
218+
219+
$collection = $chromaDB->createCollection('test-collection', embeddingFunction: $embeddingFunction);
220+
```
221+
222+
The embedding function must be an instance of `EmbeddingFunctionInterface`. There are a few built-in embedding functions
223+
that you can use:
224+
225+
- `OpenAIEmbeddingFunction`: This embedding function uses the OpenAI API to compute the embeddings. You can use it like this:
226+
```php
227+
use CodeWithKyrian\ChromaDB\EmbeddingFunction\OpenAIEmbeddingFunction;
228+
229+
$embeddingFunction = new OpenAIEmbeddingFunction('api-key', 'org-id', 'model-name');
230+
231+
$collection = $chromaDB->createCollection('test-collection', embeddingFunction: $embeddingFunction);
232+
```
233+
234+
- `HuggingFaceEmbeddingFunction`: This embedding function uses the HuggingFace API to compute the embeddings. You can use it like this:
235+
236+
```php
237+
use CodeWithKyrian\ChromaDB\EmbeddingFunction\HuggingFaceEmbeddingFunction;
238+
239+
$embeddingFunction = new HuggingFaceEmbeddingFunction('api-key', 'model-name');
240+
241+
$collection = $chromaDB->createCollection('test-collection', embeddingFunction: $embeddingFunction);
242+
```
243+
244+
You can also create your own embedding function by implementing the `EmbeddingFunctionInterface` interface.
245+
246+
```php
247+
use CodeWithKyrian\ChromaDB\EmbeddingFunction\EmbeddingFunctionInterface;
248+
249+
$embeddingFunction = new class implements EmbeddingFunctionInterface {
250+
public function generate(array $texts): array
251+
{
252+
// Compute the embeddings here and return them as an array of arrays
253+
}
254+
};
255+
256+
$collection = $chromaDB->createCollection('test-collection', embeddingFunction: $embeddingFunction);
257+
```
258+
259+
260+
261+
262+
263+
264+
265+
266+
267+
268+

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "codewithkyrian/chromadb-php",
3+
"description": "A PHP client for the ChromaDB open source embedding database API",
4+
"type": "library",
5+
"require": {
6+
"php": "^8.1",
7+
"guzzlehttp/guzzle": "^7.0"
8+
},
9+
"require-dev": {
10+
"pestphp/pest": "^2.19",
11+
"symfony/var-dumper": "^6.3",
12+
"mockery/mockery": "^1.6"
13+
},
14+
"autoload": {
15+
"psr-4": {
16+
"Codewithkyrian\\ChromaDB\\": "src/"
17+
}
18+
},
19+
"authors": [
20+
{
21+
"name": "Kyrian Obikwelu",
22+
"email": "kyrianobikwelu@gmail.com"
23+
}
24+
],
25+
"config": {
26+
"allow-plugins": {
27+
"pestphp/pest-plugin": true
28+
}
29+
},
30+
"scripts": {
31+
"test": "vendor/bin/pest",
32+
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage"
33+
}
34+
}

src/ChromaDB.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codewithkyrian\ChromaDB;
6+
7+
8+
class ChromaDB
9+
{
10+
public static function client(): Client
11+
{
12+
return self::factory()->connect();
13+
}
14+
15+
/**
16+
* Creates a new factory instance to configure a custom Alchemy Client
17+
*/
18+
public static function factory(): Factory
19+
{
20+
return new Factory();
21+
}
22+
23+
/**
24+
* Resets the database. This will delete all collections and entries and
25+
* return true if the database was reset successfully.
26+
*/
27+
public static function reset() : bool
28+
{
29+
return (new Factory())->createApiClient()->reset();
30+
}
31+
}

0 commit comments

Comments
 (0)