|
| 1 | +## ChromaDB PHP |
| 2 | + |
| 3 | +**A PHP library for interacting with ChromaDB vector database seamlessly.** |
| 4 | + |
| 5 | +[](https://travis-ci.org/ChromaDatabase/ChromaDB-PHP) |
| 6 | +[](https://coveralls.io/github/ChromaDatabase/ChromaDB-PHP?branch=master) |
| 7 | +[](https://packagist.org/packages/chromadb/chromadb-php) |
| 8 | +[](https://packagist.org/packages/chromadb/chromadb-php) |
| 9 | +[](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 | + |
0 commit comments