Skip to content

Commit 09eef49

Browse files
committed
wip
1 parent 362672c commit 09eef49

19 files changed

+376
-14
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ testbench.yaml
3333
/coverage
3434

3535
/examples/*.php
36+
37+
.env

composer.json

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
{
22
"name": "upstash/vector",
3-
"description": "This is my package vector",
3+
"description": "An HTTP/REST based Vector DB client built on top of Upstash REST API.",
44
"keywords": [
55
"upstash",
6-
"vector"
6+
"vector",
7+
"php",
8+
"sdk",
9+
"ai",
10+
"rag"
711
],
8-
"homepage": "https://github.com/upstash/vector",
12+
"homepage": "https://github.com/upstash/vector-php",
913
"license": "MIT",
1014
"authors": [
1115
{
1216
"name": "Jorge Lapa",
1317
"email": "[email protected]",
14-
"role": "Developer"
18+
"role": "Software Engineer"
1519
}
1620
],
1721
"require": {
@@ -25,9 +29,11 @@
2529
"symfony/http-client": "^7.2"
2630
},
2731
"require-dev": {
28-
"phpunit/phpunit": "^10.3.2",
2932
"laravel/pint": "^1.0",
30-
"spatie/ray": "^1.28"
33+
"nunomaduro/collision": "^8.5",
34+
"phpunit/phpunit": "^10.3.2",
35+
"spatie/ray": "^1.28",
36+
"vlucas/phpdotenv": "^5.6"
3137
},
3238
"autoload": {
3339
"psr-4": {
@@ -41,7 +47,7 @@
4147
},
4248
"scripts": {
4349
"test": "vendor/bin/phpunit",
44-
"test-coverage": "vendor/bin/phpunit --coverage",
50+
"test-coverage": "vendor/bin/phpunit --coverage",
4551
"format": "vendor/bin/pint"
4652
},
4753
"config": {

phpunit.xml.dist

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
55
backupGlobals="false"
6-
bootstrap="vendor/autoload.php"
76
colors="true"
87
processIsolation="false"
98
stopOnFailure="false"
@@ -14,6 +13,7 @@
1413
beStrictAboutOutputDuringTests="true"
1514
cacheDirectory=".phpunit.cache"
1615
backupStaticProperties="false"
16+
bootstrap="tests/bootstrap.php"
1717
>
1818
<testsuites>
1919
<testsuite name="Upstash Test Suite">
@@ -36,7 +36,7 @@
3636
</include>
3737
</source>
3838
<php>
39-
<env name="UPSTASH_VECTOR_REST_URL" value="https://vector.upstash.com" force="true" />
40-
<env name="UPSTASH_VECTOR_REST_TOKEN" value="test-token" force="true" />
39+
<env name="UPSTASH_VECTOR_REST_URL" value="https://vector.upstash.com" />
40+
<env name="UPSTASH_VECTOR_REST_TOKEN" value="test-token" />
4141
</php>
4242
</phpunit>

src/Contracts/IndexNamespaceInterface.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Upstash\Vector\Contracts;
44

5-
use Upstash\Vector\NamespaceInfo;
5+
use Upstash\Vector\DataQuery;
66
use Upstash\Vector\DataUpsert;
7+
use Upstash\Vector\NamespaceInfo;
8+
use Upstash\Vector\VectorQuery;
79
use Upstash\Vector\VectorUpsert;
810

911
interface IndexNamespaceInterface
@@ -27,4 +29,8 @@ public function upsertData(DataUpsert $data): void;
2729
* @param array<DataUpsert> $data
2830
*/
2931
public function upsertDataMany(array $data): void;
32+
33+
public function query(VectorQuery $query): void;
34+
35+
public function queryData(DataQuery $query): void;
3036
}

src/DataQuery.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Upstash\Vector;
4+
5+
use Upstash\Vector\Enums\FusionAlgorithm;
6+
use Upstash\Vector\Enums\QueryMode;
7+
use Upstash\Vector\Enums\WeightingStrategy;
8+
9+
final readonly class DataQuery
10+
{
11+
public function __construct(
12+
public string $data,
13+
public int $topK = 10,
14+
public bool $includeMetadata = false,
15+
public bool $includeVectors = false,
16+
public bool $includeData = false,
17+
public string $filter = '',
18+
public ?WeightingStrategy $weightingStrategy = null,
19+
public ?FusionAlgorithm $fusionAlgorithm = null,
20+
public QueryMode $queryMode = QueryMode::HYBRID,
21+
) {}
22+
23+
public function toArray(): array
24+
{
25+
$data = [
26+
'data' => $this->data,
27+
'topK' => $this->topK,
28+
'includeMetadata' => $this->includeMetadata,
29+
'includeVectors' => $this->includeVectors,
30+
'includeData' => $this->includeData,
31+
'filter' => $this->filter,
32+
'queryMode' => $this->queryMode->value,
33+
];
34+
35+
if ($this->weightingStrategy !== null) {
36+
$data['weightingStrategy'] = $this->weightingStrategy->value;
37+
}
38+
39+
if ($this->fusionAlgorithm !== null) {
40+
$data['fusionAlgorithm'] = $this->fusionAlgorithm->value;
41+
}
42+
43+
return $data;
44+
}
45+
}

src/Enums/FusionAlgorithm.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Upstash\Vector\Enums;
4+
5+
enum FusionAlgorithm: string
6+
{
7+
case RECIPROCAL_RANK_FUSION = 'RRF';
8+
9+
case DISTRIBUTION_BASED_SCORE_FUSION = 'DBSF';
10+
}

src/Enums/QueryMode.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Upstash\Vector\Enums;
4+
5+
enum QueryMode: string
6+
{
7+
case HYBRID = 'HYBRID';
8+
case DENSE = 'DENSE';
9+
10+
case SPARSE = 'SPARSE';
11+
}

src/Enums/WeightingStrategy.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Upstash\Vector\Enums;
4+
5+
enum WeightingStrategy: string
6+
{
7+
case INVERSE_DOCUMENT_FREQUENCY = 'IDF';
8+
}

src/Index.php

+10
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,14 @@ public function upsertDataMany(array $data): void
9494
{
9595
$this->namespace('')->upsertDataMany($data);
9696
}
97+
98+
public function query(VectorQuery $query): void
99+
{
100+
$this->namespace('')->query($query);
101+
}
102+
103+
public function queryData(DataQuery $query): void
104+
{
105+
$this->namespace('')->queryData($query);
106+
}
97107
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Upstash\Vector\Operations;
4+
5+
use Upstash\Vector\Contracts\TransporterInterface;
6+
7+
/**
8+
* @internal
9+
*/
10+
final readonly class FetchRandomVectorOperation
11+
{
12+
public function __construct(private string $namespace, private TransporterInterface $transporter) {}
13+
14+
public function fetch(): void
15+
{
16+
// TODO: Implement
17+
}
18+
}

src/Operations/QueryDataOperation.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace Upstash\Vector\Operations;
44

55
use Upstash\Vector\Contracts\TransporterInterface;
6+
use Upstash\Vector\DataQuery;
7+
use Upstash\Vector\Transporter\ContentType;
8+
use Upstash\Vector\Transporter\Method;
9+
use Upstash\Vector\Transporter\TransporterRequest;
610

711
/**
812
* @internal
@@ -14,5 +18,23 @@ public function __construct(
1418
private TransporterInterface $transporter,
1519
) {}
1620

17-
public function query(): void {}
21+
public function query(DataQuery $query): void
22+
{
23+
$namespace = trim($this->namespace);
24+
$path = '/query-data';
25+
if ($namespace !== '') {
26+
$path = "/query-data/$namespace";
27+
}
28+
29+
$request = new TransporterRequest(
30+
contentType: ContentType::JSON,
31+
method: Method::POST,
32+
path: $path,
33+
data: $query->toArray(),
34+
);
35+
36+
$response = $this->transporter->sendRequest($request);
37+
38+
dd($response);
39+
}
1840
}

src/Operations/QueryVectorsOperation.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace Upstash\Vector\Operations;
44

55
use Upstash\Vector\Contracts\TransporterInterface;
6+
use Upstash\Vector\Transporter\ContentType;
7+
use Upstash\Vector\Transporter\Method;
8+
use Upstash\Vector\Transporter\TransporterRequest;
9+
use Upstash\Vector\VectorQuery;
610

711
/**
812
* @internal
@@ -14,5 +18,23 @@ public function __construct(
1418
private TransporterInterface $transporter,
1519
) {}
1620

17-
public function query(): void {}
21+
public function query(VectorQuery $query): void
22+
{
23+
$namespace = trim($this->namespace);
24+
$path = '/query';
25+
if ($namespace !== '') {
26+
$path = "/query/$namespace";
27+
}
28+
29+
$request = new TransporterRequest(
30+
contentType: ContentType::JSON,
31+
method: Method::POST,
32+
path: $path,
33+
data: $query->toArray(),
34+
);
35+
36+
$response = $this->transporter->sendRequest($request);
37+
38+
dd($response);
39+
}
1840
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Upstash\Vector\Operations;
4+
5+
/**
6+
* @internal
7+
*/
8+
final readonly class RangeVectorsOperation
9+
{
10+
public function __construct(private string $namespace, private TransporterInterface $transporter) {}
11+
12+
public function range(): void
13+
{
14+
// TODO: Implement
15+
}
16+
}

src/Operations/UpsertDataOperation.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
namespace Upstash\Vector\Operations;
44

55
use Upstash\Vector\Contracts\TransporterInterface;
6+
use Upstash\Vector\DataUpsert;
67
use Upstash\Vector\Exceptions\OperationFailedException;
78
use Upstash\Vector\Transporter\ContentType;
89
use Upstash\Vector\Transporter\Method;
910
use Upstash\Vector\Transporter\TransporterRequest;
1011
use Upstash\Vector\Transporter\TransporterResponse;
11-
use Upstash\Vector\DataUpsert;
1212

1313
/**
1414
* @internal

src/VectorQuery.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Upstash\Vector;
4+
5+
use Upstash\Vector\Enums\FusionAlgorithm;
6+
use Upstash\Vector\Enums\WeightingStrategy;
7+
8+
final readonly class VectorQuery
9+
{
10+
/**
11+
* @param array<float> $vector
12+
*/
13+
public function __construct(
14+
public array $vector,
15+
public int $topK = 10,
16+
public bool $includeMetadata = false,
17+
public bool $includeVectors = false,
18+
public bool $includeData = false,
19+
public string $filter = '',
20+
public ?WeightingStrategy $weightingStrategy = null,
21+
public ?FusionAlgorithm $fusionAlgorithm = null,
22+
) {}
23+
24+
public function toArray(): array
25+
{
26+
$data = [
27+
'vector' => $this->vector,
28+
'topK' => $this->topK,
29+
'includeMetadata' => $this->includeMetadata,
30+
'includeVectors' => $this->includeVectors,
31+
'includeData' => $this->includeData,
32+
'filter' => $this->filter,
33+
];
34+
35+
if ($this->weightingStrategy !== null) {
36+
$data['weightingStrategy'] = $this->weightingStrategy->value;
37+
}
38+
39+
if ($this->fusionAlgorithm !== null) {
40+
$data['fusionAlgorithm'] = $this->fusionAlgorithm->value;
41+
}
42+
43+
return $data;
44+
}
45+
}

0 commit comments

Comments
 (0)