Skip to content

Commit cb53ffe

Browse files
committed
createCollection hnswConfig initialized hkulekci#36
1 parent c897df4 commit cb53ffe

4 files changed

Lines changed: 197 additions & 1 deletion

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* HnswConfig
4+
*
5+
* @since Dec 2023
6+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
7+
*/
8+
9+
namespace Qdrant\Models\Request\CollectionConfig;
10+
11+
use Qdrant\Exception\InvalidArgumentException;
12+
use Qdrant\Models\Request\RequestModel;
13+
14+
class HnswConfig implements RequestModel
15+
{
16+
protected ?int $m = null;
17+
protected ?int $efConstruct = null;
18+
protected ?int $fullScanThreshold = null;
19+
protected ?int $maxIndexingThreads = null;
20+
protected ?bool $onDisk = null;
21+
protected ?int $payloadM = null;
22+
23+
public function setM(?int $m): HnswConfig
24+
{
25+
if ($this->efConstruct < 0) {
26+
throw new InvalidArgumentException('m should be bigger than 0');
27+
}
28+
$this->m = $m;
29+
30+
return $this;
31+
}
32+
33+
public function setEfConstruct(?int $efConstruct): HnswConfig
34+
{
35+
if ($efConstruct < 4) {
36+
throw new InvalidArgumentException('ef_construct should be bigger than 4');
37+
}
38+
$this->efConstruct = $efConstruct;
39+
40+
return $this;
41+
}
42+
43+
public function setFullScanThreshold(?int $fullScanThreshold): HnswConfig
44+
{
45+
if ($fullScanThreshold < 10) {
46+
throw new InvalidArgumentException('full_scan_threshold should be bigger than 10');
47+
}
48+
$this->fullScanThreshold = $fullScanThreshold;
49+
50+
return $this;
51+
}
52+
53+
public function setMaxIndexingThreads(?int $maxIndexingThreads): HnswConfig
54+
{
55+
if ($maxIndexingThreads < 0) {
56+
throw new InvalidArgumentException('max_indexing_threads should be bigger than 0');
57+
}
58+
$this->maxIndexingThreads = $maxIndexingThreads;
59+
60+
return $this;
61+
}
62+
63+
public function setOnDisk(?bool $onDisk): HnswConfig
64+
{
65+
$this->onDisk = $onDisk;
66+
67+
return $this;
68+
}
69+
70+
public function setPayloadM(?int $payloadM): HnswConfig
71+
{
72+
if ($payloadM < 0) {
73+
throw new InvalidArgumentException('payload_m should be bigger than 0');
74+
}
75+
$this->payloadM = $payloadM;
76+
77+
return $this;
78+
}
79+
80+
public function toArray(): array
81+
{
82+
$data = [];
83+
if ($this->m) {
84+
$data['m'] = $this->m;
85+
}
86+
if ($this->efConstruct) {
87+
$data['ef_construct'] = $this->efConstruct;
88+
}
89+
if ($this->fullScanThreshold) {
90+
$data['full_scan_threshold'] = $this->fullScanThreshold;
91+
}
92+
if ($this->maxIndexingThreads) {
93+
$data['max_indexing_threads'] = $this->maxIndexingThreads;
94+
}
95+
if ($this->onDisk) {
96+
$data['on_disk'] = $this->onDisk;
97+
}
98+
if ($this->payloadM) {
99+
$data['payload_m'] = $this->payloadM;
100+
}
101+
102+
return $data;
103+
}
104+
}

src/Models/Request/CreateCollection.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Qdrant\Models\Request;
1010

1111
use Qdrant\Models\Request\CollectionConfig\DisabledQuantization;
12+
use Qdrant\Models\Request\CollectionConfig\HnswConfig;
1213
use Qdrant\Models\Request\CollectionConfig\OptimizersConfigDiff;
1314
use Qdrant\Models\Request\CollectionConfig\QuantizationConfig;
1415

@@ -29,6 +30,8 @@ class CreateCollection implements RequestModel
2930

3031
protected ?OptimizersConfigDiff $optimizersConfig = null;
3132

33+
protected ?HnswConfig $hnswConfig = null;
34+
3235
protected ?QuantizationConfig $quantizationConfig = null;
3336

3437
public function addVector(VectorParams $vectorParams, string $name = null): CreateCollection
@@ -112,6 +115,12 @@ public function toArray(): array
112115
if ($this->initFrom !== null) {
113116
$data['init_from'] = $this->initFrom->toArray();
114117
}
118+
if ($this->optimizersConfig !== null) {
119+
$data['optimizers_config'] = $this->optimizersConfig->toArray();
120+
}
121+
if ($this->hnswConfig !== null) {
122+
$data['hnsw_config'] = $this->hnswConfig->toArray();
123+
}
115124

116125
if ($this->quantizationConfig instanceof DisabledQuantization) {
117126
$data['quantization_config'] = 'Disabled';
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* @since Oct 2023
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Qdrant\Tests\Unit\Models\Request\CollectionConfig;
8+
9+
use PHPUnit\Framework\TestCase;
10+
use Qdrant\Models\Request\CollectionConfig\HnswConfig;
11+
12+
class HnswConfigTest extends TestCase
13+
{
14+
public function testBasic(): void
15+
{
16+
$config = new HnswConfig();
17+
18+
$this->assertEquals([], $config->toArray());
19+
}
20+
21+
public function testWithM(): void
22+
{
23+
$config = (new HnswConfig())->setM(10);
24+
25+
$this->assertEquals([
26+
'm' => 10
27+
], $config->toArray());
28+
}
29+
30+
public function testWithEfConstruct(): void
31+
{
32+
$config = (new HnswConfig())->setEfConstruct(10);
33+
34+
$this->assertEquals([
35+
'ef_construct' => 10
36+
], $config->toArray());
37+
}
38+
39+
public function testWithFullScanThreshold(): void
40+
{
41+
$config = (new HnswConfig())->setFullScanThreshold(10);
42+
43+
$this->assertEquals([
44+
'full_scan_threshold' => 10
45+
], $config->toArray());
46+
}
47+
48+
public function testWithDeletedThreshold(): void
49+
{
50+
$config = (new HnswConfig())->setMaxIndexingThreads(9);
51+
52+
$this->assertEquals([
53+
'max_indexing_threads' => 9
54+
], $config->toArray());
55+
}
56+
57+
public function testWithOnDisk(): void
58+
{
59+
$config = (new HnswConfig())->setOnDisk(true);
60+
61+
$this->assertEquals([
62+
'on_disk' => 10
63+
], $config->toArray());
64+
}
65+
66+
public function testWithDefaultSegmentNumber(): void
67+
{
68+
$config = (new HnswConfig())->setPayloadM(10);
69+
70+
$this->assertEquals([
71+
'payload_m' => 10
72+
], $config->toArray());
73+
}
74+
75+
public function testWithFlushIntervalSec(): void
76+
{
77+
$config = (new HnswConfig())->setMaxIndexingThreads(10)->setPayloadM(10);
78+
79+
$this->assertEquals([
80+
'max_indexing_threads' => 10,
81+
'payload_m' => 10,
82+
], $config->toArray());
83+
}
84+
}

tests/Unit/Models/Request/CollectionConfig/OptimizerConfigDiffTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
namespace Qdrant\Tests\Unit\Models\Request\CollectionConfig;
88

99
use PHPUnit\Framework\TestCase;
10-
use Qdrant\Models\Request\CollectionConfig\BinaryQuantization;
1110
use Qdrant\Models\Request\CollectionConfig\OptimizersConfigDiff;
1211

1312
class OptimizerConfigDiffTest extends TestCase

0 commit comments

Comments
 (0)