Skip to content

Commit 23aa500

Browse files
feat: use Collection as a resource directly instead of the CollectionResource
1 parent b7c9824 commit 23aa500

13 files changed

Lines changed: 447 additions & 492 deletions

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

src/Api.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@
22

33
declare(strict_types=1);
44

5-
65
namespace Codewithkyrian\ChromaDB;
76

87
use Codewithkyrian\ChromaDB\Exceptions\ChromaConnectionException;
98
use Codewithkyrian\ChromaDB\Exceptions\ChromaException;
109
use Codewithkyrian\ChromaDB\Models\Collection;
1110
use Codewithkyrian\ChromaDB\Models\Database;
1211
use Codewithkyrian\ChromaDB\Models\Tenant;
13-
use Codewithkyrian\ChromaDB\Requests\AddEmbeddingRequest;
12+
use Codewithkyrian\ChromaDB\Requests\AddItemsRequest;
1413
use Codewithkyrian\ChromaDB\Requests\CreateCollectionRequest;
1514
use Codewithkyrian\ChromaDB\Requests\CreateDatabaseRequest;
1615
use Codewithkyrian\ChromaDB\Requests\CreateTenantRequest;
17-
use Codewithkyrian\ChromaDB\Requests\DeleteEmbeddingRequest;
16+
use Codewithkyrian\ChromaDB\Requests\DeleteItemsRequest;
1817
use Codewithkyrian\ChromaDB\Requests\GetEmbeddingRequest;
19-
use Codewithkyrian\ChromaDB\Requests\QueryEmbeddingRequest;
18+
use Codewithkyrian\ChromaDB\Requests\QueryItemsRequest;
2019
use Codewithkyrian\ChromaDB\Requests\UpdateCollectionRequest;
21-
use Codewithkyrian\ChromaDB\Requests\UpdateEmbeddingRequest;
20+
use Codewithkyrian\ChromaDB\Requests\UpdateItemsRequest;
2221
use Codewithkyrian\ChromaDB\Requests\UpdateTenantRequest;
2322
use Codewithkyrian\ChromaDB\Responses\GetItemsResponse;
2423
use Codewithkyrian\ChromaDB\Responses\QueryItemsResponse;
@@ -28,11 +27,10 @@
2827
use Psr\Http\Client\ClientExceptionInterface;
2928

3029
/**
31-
* Client for ChromaDB API (v.0.1.0)
30+
* Client for ChromaDB API
3231
*/
3332
class Api
3433
{
35-
3634
public function __construct(
3735
public readonly Client $httpClient,
3836
) {}
@@ -58,15 +56,15 @@ public function getUserIdentity(): array
5856
* @param string $database The database name.
5957
* @param string $tenant The tenant name.
6058
*/
61-
public function getCollectionByCrn(string $crn): Collection
59+
public function getCollectionByCrn(string $crn, string $database, string $tenant): Collection
6260
{
6361
try {
6462
$response = $this->httpClient->get("/api/v2/collections/{$crn}");
6563
} catch (ClientExceptionInterface $e) {
6664
$this->handleChromaApiException($e);
6765
}
6866

69-
return Collection::make(json_decode($response->getBody()->getContents(), true));
67+
return Collection::make(json_decode($response->getBody()->getContents(), true), $this, $database, $tenant);
7068
}
7169

7270
/**
@@ -288,7 +286,7 @@ public function listCollections(string $database, string $tenant, ?int $limit =
288286

289287
$result = json_decode($response->getBody()->getContents(), true);
290288

291-
return array_map(fn(array $item) => Collection::make($item), $result);
289+
return array_map(fn(array $item) => Collection::make($item, $this, $database, $tenant), $result);
292290
}
293291

294292
/**
@@ -312,7 +310,7 @@ public function createCollection(string $database, string $tenant, CreateCollect
312310

313311
$result = json_decode($response->getBody()->getContents(), true);
314312

315-
return Collection::make($result);
313+
return Collection::make($result, $this, $database, $tenant);
316314
}
317315

318316
/**
@@ -334,7 +332,7 @@ public function getCollection(string $collectionId, string $database, string $te
334332

335333
$result = json_decode($response->getBody()->getContents(), true);
336334

337-
return Collection::make($result);
335+
return Collection::make($result, $this, $database, $tenant);
338336
}
339337

340338
/**
@@ -398,9 +396,9 @@ public function countCollections(string $database, string $tenant): int
398396
* @param string $collectionId The UUID of the collection to add items to.
399397
* @param string $database The database name to add items to.
400398
* @param string $tenant The tenant ID to add items to.
401-
* @param AddEmbeddingRequest $request The request to add items to the collection.
399+
* @param AddItemsRequest $request The request to add items to the collection.
402400
*/
403-
public function addCollectionItems(string $collectionId, string $database, string $tenant, AddEmbeddingRequest $request): void
401+
public function addCollectionItems(string $collectionId, string $database, string $tenant, AddItemsRequest $request): void
404402
{
405403
try {
406404
$this->httpClient->post("/api/v2/tenants/$tenant/databases/$database/collections/$collectionId/add", [
@@ -437,9 +435,9 @@ public function countCollectionItems(string $collectionId, string $database, str
437435
* @param string $collectionId The UUID of the collection to update items in.
438436
* @param string $database The database name to update items in.
439437
* @param string $tenant The tenant ID to update items in.
440-
* @param UpdateEmbeddingRequest $request The request to update items in the collection.
438+
* @param UpdateItemsRequest $request The request to update items in the collection.
441439
*/
442-
public function updateCollectionItems(string $collectionId, string $database, string $tenant, UpdateEmbeddingRequest $request): void
440+
public function updateCollectionItems(string $collectionId, string $database, string $tenant, UpdateItemsRequest $request): void
443441
{
444442
try {
445443
$this->httpClient->post("/api/v2/tenants/$tenant/databases/$database/collections/$collectionId/update", [
@@ -456,9 +454,9 @@ public function updateCollectionItems(string $collectionId, string $database, st
456454
* @param string $collectionId The UUID of the collection to upsert items in.
457455
* @param string $database The database name to upsert items in.
458456
* @param string $tenant The tenant ID to upsert items in.
459-
* @param AddEmbeddingRequest $request The request to upsert items in the collection.
457+
* @param AddItemsRequest $request The request to upsert items in the collection.
460458
*/
461-
public function upsertCollectionItems(string $collectionId, string $database, string $tenant, AddEmbeddingRequest $request): void
459+
public function upsertCollectionItems(string $collectionId, string $database, string $tenant, AddItemsRequest $request): void
462460
{
463461
try {
464462
$this->httpClient->post("/api/v2/tenants/$tenant/databases/$database/collections/$collectionId/upsert", [
@@ -470,7 +468,7 @@ public function upsertCollectionItems(string $collectionId, string $database, st
470468
}
471469

472470
/**
473-
* Retrieves records from a collection by ID or metadata filter.
471+
* Retrieves items from a collection by ID or metadata filter.
474472
*
475473
* @param string $collectionId The UUID of the collection to get items from.
476474
* @param string $database The database name to get items from.
@@ -494,7 +492,15 @@ public function getCollectionItems(string $collectionId, string $database, strin
494492
return GetItemsResponse::from($result);
495493
}
496494

497-
public function deleteCollectionItems(string $collectionId, string $database, string $tenant, DeleteEmbeddingRequest $request): void
495+
/**
496+
* Deletes items from a collection by ID or metadata filter.
497+
*
498+
* @param string $collectionId The UUID of the collection to delete items from.
499+
* @param string $database The database name to delete items from.
500+
* @param string $tenant The tenant ID to delete items from.
501+
* @param DeleteItemsRequest $request The request to delete items from the collection.
502+
*/
503+
public function deleteCollectionItems(string $collectionId, string $database, string $tenant, DeleteItemsRequest $request): void
498504
{
499505
try {
500506
$this->httpClient->post("/api/v2/tenants/$tenant/databases/$database/collections/$collectionId/delete", [
@@ -511,11 +517,11 @@ public function deleteCollectionItems(string $collectionId, string $database, st
511517
* @param string $collectionId The UUID of the collection to query.
512518
* @param string $database The database name to query the collection in.
513519
* @param string $tenant The tenant ID to query the collection in.
514-
* @param QueryEmbeddingRequest $request The request to query the collection.
520+
* @param QueryItemsRequest $request The request to query the collection.
515521
*
516522
* @return QueryItemsResponse
517523
*/
518-
public function queryCollectionItems(string $collectionId, string $database, string $tenant, QueryEmbeddingRequest $request): QueryItemsResponse
524+
public function queryCollectionItems(string $collectionId, string $database, string $tenant, QueryItemsRequest $request): QueryItemsResponse
519525
{
520526
try {
521527
$response = $this->httpClient->post("/api/v2/tenants/$tenant/databases/$database/collections/$collectionId/query", [

src/ChromaDB.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Codewithkyrian\ChromaDB;
66

7-
87
class ChromaDB
98
{
109
public static function client(): Client

src/Client.php

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Codewithkyrian\ChromaDB\Requests\CreateDatabaseRequest;
1212
use Codewithkyrian\ChromaDB\Requests\CreateTenantRequest;
1313
use Codewithkyrian\ChromaDB\Requests\CreateCollectionRequest;
14-
use Codewithkyrian\ChromaDB\Resources\CollectionResource;
1514

1615
class Client
1716
{
@@ -23,7 +22,6 @@ public function __construct(
2322
$this->initDatabaseAndTenant();
2423
}
2524

26-
2725
public function initDatabaseAndTenant(): void
2826
{
2927
try {
@@ -78,21 +76,19 @@ public function listCollections(): array
7876
* @param ?array $metadata Optional metadata associated with the collection.
7977
* @param ?EmbeddingFunction $embeddingFunction Optional custom embedding function for the collection.
8078
*
81-
* @return CollectionResource
79+
* @return Collection
8280
*/
83-
public function createCollection(string $name, ?array $metadata = null, ?EmbeddingFunction $embeddingFunction = null): CollectionResource
81+
public function createCollection(string $name, ?array $metadata = null, ?EmbeddingFunction $embeddingFunction = null): Collection
8482
{
8583
$request = new CreateCollectionRequest($name, $metadata);
8684

8785
$collection = $this->api->createCollection($this->database, $this->tenant, $request);
8886

89-
return CollectionResource::make(
90-
$collection,
91-
$this->database,
92-
$this->tenant,
93-
$embeddingFunction,
94-
$this->api
95-
);
87+
if ($embeddingFunction) {
88+
$collection->setEmbeddingFunction($embeddingFunction);
89+
}
90+
91+
return $collection;
9692
}
9793

9894
/**
@@ -102,21 +98,19 @@ public function createCollection(string $name, ?array $metadata = null, ?Embeddi
10298
* @param ?array $metadata Optional metadata associated with the collection.
10399
* @param ?EmbeddingFunction $embeddingFunction Optional custom embedding function for the collection.
104100
*
105-
* @return CollectionResource
101+
* @return Collection
106102
*/
107-
public function getOrCreateCollection(string $name, ?array $metadata = null, ?EmbeddingFunction $embeddingFunction = null): CollectionResource
103+
public function getOrCreateCollection(string $name, ?array $metadata = null, ?EmbeddingFunction $embeddingFunction = null): Collection
108104
{
109105
$request = new CreateCollectionRequest($name, $metadata, true);
110106

111-
$collection = $this->api->createCollection($this->database, $this->tenant, $request);
107+
$collection = $this->api->createCollection($this->database, $this->tenant, $request);
112108

113-
return CollectionResource::make(
114-
$collection,
115-
$this->database,
116-
$this->tenant,
117-
$embeddingFunction,
118-
$this->api
119-
);
109+
if ($embeddingFunction) {
110+
$collection->setEmbeddingFunction($embeddingFunction);
111+
}
112+
113+
return $collection;
120114
}
121115

122116
/**
@@ -126,19 +120,17 @@ public function getOrCreateCollection(string $name, ?array $metadata = null, ?Em
126120
* @param string $name The name of the collection.
127121
* @param ?EmbeddingFunction $embeddingFunction Optional custom embedding function for the collection.
128122
*
129-
* @return CollectionResource
123+
* @return Collection
130124
*/
131-
public function getCollection(string $name, ?EmbeddingFunction $embeddingFunction = null): CollectionResource
125+
public function getCollection(string $name, ?EmbeddingFunction $embeddingFunction = null): Collection
132126
{
133127
$collection = $this->api->getCollection($name, $this->database, $this->tenant);
134128

135-
return CollectionResource::make(
136-
$collection,
137-
$this->database,
138-
$this->tenant,
139-
$embeddingFunction,
140-
$this->api
141-
);
129+
if ($embeddingFunction) {
130+
$collection->setEmbeddingFunction($embeddingFunction);
131+
}
132+
133+
return $collection;
142134
}
143135

144136
/**

0 commit comments

Comments
 (0)