Download the PHP package codewithkyrian/chromadb-laravel without Composer
On this page you can find all versions of the php package codewithkyrian/chromadb-laravel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package chromadb-laravel
ChromaDB PHP for Laravel
A Laravel convenient wrapper for the ChromaDB PHP library, used to interact with Chroma vector database seamlessly.
Note: This package is a wrapper around the ChromaDB PHP library. It is meant to be used in Laravel applications. If you are looking for a standalone or framework-agnostic way of interacting with Chroma in PHP, check out the ChromaDB PHP library instead.
Installation
You can install the package via composer:
After installing the package, you can publish the configuration file using the following command:
This will publish a chromadb.php
file in your config directory with the following content:
As you can see, all configuration options are retrieved from environment variables, so you can easily set them in
your .env
file without having to modify the configuration file itself.
Usage
Of course, you need to have the ChromaDB server running before you can use this package. Instructions on how to run ChromaDB can be found in the ChromaDB website.
For more usage examples, check out the ChromaDB PHP library.
Working with Eloquent Models
This package comes with a trait that you can use to associate your Eloquent models with a ChromaDB collection and automatically sync them to ChromaDB. To get started, add the ChromaModel interface and HasChromaCollection trait to your model.
After that, there are a few methods that you need to implement in your model.
-
documentFields()
- This method should return an array of fields that you want to use to form the document that will be embedded in the ChromaDB collection. If combines the fields in this array to a string and uses that as the document. This method is optional, but if you don't implement it, you must implement thetoChromaDocument()
method. -
embeddingFunction()
- This method should return the name of the embedding function that you want to use to embed your model in the ChromaDB collection. You can use any of the built-in embedding functions or create your own embedding function by implementing the EmbeddingFunction interface (including Anonymous Classes). collectionName()
- This method should return the name you want for the ChromaDB collection associated with your model. By default, it returns the model's table name.-
toChromaDocument()
(optional) - If you don't like the default way of combining the fields in thedocumentFields()
method, you can implement this method to return the document that will be embedded in the ChromaDB collection. -
metadataFields()
(optional) - This method should return an array of fields that you want to use to form the metadata that will be embedded in the ChromaDB collection. It'll be saved as a json object in the ChromaDB collection. By default, it only returns theid
field, so the metadata will be{ "id": 1 }
. toChromaMetadata()
(optional) - If you want more control over the metadata that will be embedded in the ChromaDB collection, you can implement this method to return the metadata that will be embedded in the ChromaDB collection. Be sure to return an associative array.
After implementing the methods above (only two are required), you model now has a getChromaCollection()
method that
you can use to get the ChromaDB collection associated with your model.
Syncing Models to ChromaDB
By default, the package will automatically sync your models to ChromaDB whenever they are created, updated or deleted
provided there was a change in the attributes since the last sync.
You can disable this by setting the chromadb.sync.enabled
config option to false
or better still, set
the CHROMA_SYNC_ENABLED
to false.
The syncing of models is queued so be sure to set up your queue and workers the Laravel recommended way. You can set the
queue, connection
and the number tries for the job in the config or using the CHROMA_SYNC_QUEUE
, CHROMA_SYNC_CONNECTION
and CHROMA_SYNC_TRIES
respectively.
However, you can set the CHROMA_SYNC_QUEUE
to false to disable using queues to perform the sync.
Querying the collection
While you can still query the collection after getting it from the getChromaCollection()
method, you can also query
the collection
using the model. The model has a queryChromaCollection()
scope that you can use to query the collection.
The arguments for the queryChromaCollection()
method are the same as the query()
method in the ChromaDB PHP library.
Also, this meethod
sorts the results by the distance
field in the results.
Truncating the collection
You can truncate the collection associated with a model using the truncateChromaCollection()
method on the model.
Testing
Contributors
- Kyrian Obikwelu
- Other contributors are welcome.
License
This project is licensed under the MIT License. See the LICENSE file for more information.
All versions of chromadb-laravel with dependencies
laravel/framework Version ^9.5.0|^10.34.2
codewithkyrian/chromadb-php Version ^0.1.0