Skip to content

Commit b2bb5dc

Browse files
Feat: Initial Commit
0 parents  commit b2bb5dc

File tree

11 files changed

+481
-0
lines changed

11 files changed

+481
-0
lines changed

.github/workflows/tests.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: tests
2+
3+
on: ['push', 'pull_request']
4+
5+
jobs:
6+
ci:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
fail-fast: true
10+
matrix:
11+
php: [8.1, 8.2, 8.3]
12+
laravel: [11.*, 10.*, 9.*]
13+
dependency-version: [prefer-lowest, prefer-stable]
14+
exclude:
15+
- php: 8.1
16+
laravel: 11.*
17+
18+
name: Tests on PHP${{ matrix.php }} - Laravel${{ matrix.laravel }} - ${{ matrix.dependency-version }}
19+
20+
services:
21+
chroma:
22+
image: chromadb/chroma
23+
ports:
24+
- 8000:8000
25+
26+
steps:
27+
28+
- name: Checkout
29+
uses: actions/checkout@v3
30+
31+
- name: Cache dependencies
32+
uses: actions/cache@v3
33+
with:
34+
path: ~/.composer/cache/files
35+
key: dependencies-php-${{ matrix.php }}-L${{ matrix.laravel }}-${{ matrix.dependency-version }}-composer-${{ hashFiles('composer.json') }}
36+
37+
- name: Setup PHP
38+
uses: shivammathur/setup-php@v2
39+
with:
40+
php-version: ${{ matrix.php }}
41+
extensions: dom, mbstring, zip
42+
coverage: none
43+
44+
- name: Require Laravel Version
45+
run: >
46+
composer require
47+
"laravel/framework:${{ matrix.laravel }}"
48+
--no-interaction --no-update
49+
50+
- name: Install Composer dependencies
51+
run: composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist
52+
53+
- name: Integration Tests
54+
run: php ./vendor/bin/pest

.gitignore

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

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: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
## ChromaDB PHP for Laravel
2+
3+
**A Laravel convenient wrapper for the ChromaDB PHP library, used to interact
4+
with [Chroma](https://github.com/chroma-core/chroma) vector database seamlessly.**
5+
6+
[![MIT Licensed](https://img.shields.io/badge/license-mit-blue.svg)](https://github.com/CodeWithKyrian/chromadb-laravel/blob/main/LICENSE)
7+
[![GitHub Tests Action Status](https://github.com/CodeWithKyrian/chromadb-laravel/actions/workflows/tests.yml/badge.svg)](https://github.com/CodeWithKyrian/chromadb-laravel/actions/workflows/tests.yml)
8+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/codewithkyrian/chromadb-laravel.svg)](https://packagist.org/packages/codewithkyrian/chromadb-laravel)
9+
10+
11+
> **Note:** This package is a wrapper around the [ChromaDB PHP library](https://github.com/CodeWithKyrian/chromadb-php).
12+
> It is meant to be used in Laravel applications.
13+
> If you are looking for a standalone or framework-agnostic way of interacting with Chroma in PHP, check out
14+
> the [ChromaDB PHP library](https://github.com/CodeWithKyrian/chromadb-php) instead.
15+
16+
## Installation
17+
18+
You can install the package via composer:
19+
20+
```bash
21+
composer require codewithkyrian/chromadb-laravel
22+
```
23+
24+
After installing the package, you can publish the configuration file using the following command:
25+
26+
```bash
27+
php artisan vendor:publish --provider="Codewithkyrian\ChromaDB\ChromaServiceProvider" --tag="config"
28+
```
29+
30+
This will publish a `chromadb.php` file in your config directory with the following content:
31+
32+
```php
33+
return [
34+
/*
35+
|--------------------------------------------------------------------------
36+
| ChromaDB Host
37+
|--------------------------------------------------------------------------
38+
|
39+
| Here you may specify your ChromaDB Host. This is the host where your ChromaDB
40+
| instance is running. This is used to connect to your ChromaDB instance.
41+
*/
42+
'host' => env('CHROMADB_HOST', 'localhost'),
43+
44+
/*
45+
|--------------------------------------------------------------------------
46+
| ChromaDB Port
47+
|--------------------------------------------------------------------------
48+
|
49+
| Here you may specify your ChromaDB Port. This is the port where your ChromaDB
50+
| instance is running. This is used to connect to your ChromaDB instance.
51+
*/
52+
'port' => env('CHROMADB_PORT', 8000),
53+
54+
/*
55+
|--------------------------------------------------------------------------
56+
| ChromaDB Tenant
57+
|--------------------------------------------------------------------------
58+
|
59+
| This is the tenant that you want to connect to.
60+
*/
61+
'tenant' => env('CHROMADB_TENANT', 'default_tenant'),
62+
63+
/*
64+
|--------------------------------------------------------------------------
65+
| ChromaDB Database
66+
|--------------------------------------------------------------------------
67+
|
68+
| This is the database that you want to connect to.
69+
*/
70+
'database' => env('CHROMADB_DATABASE', 'default_database'),
71+
];
72+
```
73+
74+
As you can see, all configuration options are retrieved from environment variables, so you can easily set them in
75+
your `.env` file without having to modify the configuration file itself.
76+
77+
```dotenv
78+
CHROMA_HOST=http://localhost
79+
CHROMA_PORT=8080
80+
CHROMA_TENANT=default
81+
CHROMA_DATABASE=default
82+
```
83+
84+
## Usage
85+
86+
Of course, you need to have the ChromaDB server running before you can use this package. Instructions on how to run
87+
ChromaDB can be found in the [ChromaDB website](https://docs.trychroma.com/deployment).
88+
89+
```php
90+
use Codewithkyrian\ChromaDB\Facades\ChromaDB;
91+
92+
ChromaDB::version(); // Eg. 1.0.0
93+
94+
$collection = ChromaDB::createCollection('collection_name');
95+
96+
$collection = ChromaDB::getCollection('collection_name');
97+
98+
$collection = ChromaDB::deleteCollection('collection_name');
99+
100+
$collections = ChromaDB::listCollections();
101+
```
102+
103+
For more usage examples, check out the [ChromaDB PHP library](https://github.com/CodeWithKyrian/chromadb-php).
104+
105+
## Testing
106+
107+
```
108+
// Run chroma by running the docker compose file in the repo
109+
docker compose up -d
110+
111+
composer test
112+
```
113+
114+
## Contributors
115+
116+
- [Kyrian Obikwelu](https://github.com/CodeWithKyrian)
117+
- Other contributors are welcome.
118+
119+
## License
120+
121+
This project is licensed under the MIT License. See
122+
the [LICENSE](https://github.com/codewithkyrian/chromadb-php/blob/main/LICENSE) file for more information.
123+
124+
125+
126+
127+
128+
129+
130+
131+
132+

composer.json

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"name": "codewithkyrian/chromadb-laravel",
3+
"description": "ChromaDB Laravel is a Laravel client for the Chroma Open Source Embedding Database",
4+
"keywords": [
5+
"chromadb",
6+
"php",
7+
"laravel",
8+
"embedding",
9+
"sdk",
10+
"database",
11+
"vectors",
12+
"semantic",
13+
"search",
14+
"chroma",
15+
"open-source"
16+
],
17+
"type": "library",
18+
"license": "MIT",
19+
"require": {
20+
"php": "^8.1",
21+
"laravel/framework": "^9.46.0|^10.34.2|^11.0",
22+
"codewithkyrian/chromadb-php": "^0.1.0"
23+
},
24+
"require-dev": {
25+
"pestphp/pest": "^2.19",
26+
"symfony/var-dumper": "^6.3 |^7.0.1",
27+
"mockery/mockery": "^1.6"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"Codewithkyrian\\ChromaDB\\": "src/"
32+
}
33+
},
34+
"authors": [
35+
{
36+
"name": "Kyrian Obikwelu",
37+
"email": "[email protected]"
38+
}
39+
],
40+
"config": {
41+
"allow-plugins": {
42+
"pestphp/pest-plugin": true
43+
}
44+
},
45+
"minimum-stability": "dev",
46+
"prefer-stable": true,
47+
"extra": {
48+
"laravel": {
49+
"providers": [
50+
"Codewithkyrian\\ChromaDB\\ChromaServiceProvider"
51+
]
52+
}
53+
},
54+
"scripts": {
55+
"test": "vendor/bin/pest",
56+
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage"
57+
}
58+
}

config/chromadb.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
/*
7+
|--------------------------------------------------------------------------
8+
| ChromaDB Host
9+
|--------------------------------------------------------------------------
10+
|
11+
| Here you may specify your ChromaDB Host. This is the host where your ChromaDB
12+
| instance is running. This is used to connect to your ChromaDB instance.
13+
*/
14+
'host' => env('CHROMADB_HOST', 'localhost'),
15+
16+
/*
17+
|--------------------------------------------------------------------------
18+
| ChromaDB Port
19+
|--------------------------------------------------------------------------
20+
|
21+
| Here you may specify your ChromaDB Port. This is the port where your ChromaDB
22+
| instance is running. This is used to connect to your ChromaDB instance.
23+
*/
24+
'port' => env('CHROMADB_PORT', 8000),
25+
26+
/*
27+
|--------------------------------------------------------------------------
28+
| ChromaDB Tenant
29+
|--------------------------------------------------------------------------
30+
|
31+
| This is the tenant that you want to connect to.
32+
*/
33+
'tenant' => env('CHROMADB_TENANT', 'default_tenant'),
34+
35+
/*
36+
|--------------------------------------------------------------------------
37+
| ChromaDB Database
38+
|--------------------------------------------------------------------------
39+
|
40+
| This is the database that you want to connect to.
41+
*/
42+
'database' => env('CHROMADB_DATABASE', 'default_database'),
43+
];
44+

phpunit.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
>
7+
<testsuites>
8+
<testsuite name="Test Suite">
9+
<directory suffix=".php">./tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
<source>
13+
<include>
14+
<directory suffix=".php">./src</directory>
15+
</include>
16+
</source>
17+
</phpunit>

src/ChromaServiceProvider.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
namespace Codewithkyrian\ChromaDB;
7+
8+
use Codewithkyrian\ChromaDB\ChromaDB;
9+
use Illuminate\Contracts\Support\DeferrableProvider;
10+
use Illuminate\Support\ServiceProvider;
11+
12+
/**
13+
* @internal
14+
*/
15+
class ChromaServiceProvider extends ServiceProvider implements DeferrableProvider
16+
{
17+
public function boot(): void
18+
{
19+
$this->publishes([
20+
__DIR__ . '/../config/chromadb.php' => config_path('chromadb.php'),
21+
], 'config');
22+
}
23+
24+
public function register(): void
25+
{
26+
$this->app->singleton('chromadb', function () {
27+
return ChromaDB::factory()
28+
->withHost(config('chromadb.host'))
29+
->withPort(config('chromadb.port'))
30+
->withDatabase(config('chromadb.database'))
31+
->withTenant(config('chromadb.tenant'))
32+
->connect();
33+
});
34+
}
35+
36+
public function provides(): array
37+
{
38+
return ['chromadb'];
39+
}
40+
41+
}

0 commit comments

Comments
 (0)