Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Requests/Collections/CreateCollectionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CreateCollectionRequest extends Request implements HasBody
*/
public function __construct(
protected readonly string $name,
protected readonly int $size = 1536,
protected readonly ?int $size = null,
protected readonly string $distance = 'Cosine',
protected readonly ?array $namedVectors = null,
protected readonly ?array $sparseVectors = null,
Expand All @@ -40,6 +40,13 @@ protected function defaultBody(): array
if ($this->namedVectors !== null) {
$body = ['vectors' => $this->namedVectors];
} else {
if ($this->size === null) {
throw new \InvalidArgumentException(
'Collection vector size is required: pass $size or $namedVectors. '
.'A silent default dimension would have to match your embedding model by luck.'
);
}

$body = [
'vectors' => [
'size' => $this->size,
Expand Down
13 changes: 10 additions & 3 deletions tests/Unit/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,11 @@
describe('CreateCollectionRequest with sparse vectors', function (): void {
it('includes sparse_vectors in body', function (): void {
$sparse = ['sparse' => ['modifier' => 'idf']];
$request = new CreateCollectionRequest('test', sparseVectors: $sparse);
$request = new CreateCollectionRequest('test', size: 1024, sparseVectors: $sparse);
$body = invade($request)->defaultBody();

expect($body['sparse_vectors'])->toBe($sparse)
->and($body['vectors'])->toBe(['size' => 1536, 'distance' => 'Cosine']);
->and($body['vectors'])->toBe(['size' => 1024, 'distance' => 'Cosine']);
});

it('combines named vectors with sparse vectors', function (): void {
Expand All @@ -416,11 +416,18 @@
});

it('omits sparse_vectors when null', function (): void {
$request = new CreateCollectionRequest('test');
$request = new CreateCollectionRequest('test', size: 1024);
$body = invade($request)->defaultBody();

expect($body)->not->toHaveKey('sparse_vectors');
});

it('throws when neither size nor named vectors are given', function (): void {
$request = new CreateCollectionRequest('test');

expect(fn () => invade($request)->defaultBody())
->toThrow(InvalidArgumentException::class, 'Collection vector size is required');
});
});

describe('AliasRequest', function (): void {
Expand Down
Loading