From 07e31b141a85535bf2225d80ccbb96428f427dc8 Mon Sep 17 00:00:00 2001 From: Jorge Lapa <2780099+heyjorgedev@users.noreply.github.com> Date: Tue, 18 Feb 2025 21:21:25 +0700 Subject: [PATCH 1/3] feat: expose more information about the index --- src/DenseIndexInfo.php | 23 +++++++++++++++++ src/Enums/IndexType.php | 11 ++++++++ src/IndexInfo.php | 7 ++++++ src/Operations/GetIndexInfoOperation.php | 28 +++++++++++++++++++++ src/SparseIndexInfo.php | 19 ++++++++++++++ tests/Dense/Operations/GetIndexInfoTest.php | 6 +++++ 6 files changed, 94 insertions(+) create mode 100644 src/DenseIndexInfo.php create mode 100644 src/Enums/IndexType.php create mode 100644 src/SparseIndexInfo.php diff --git a/src/DenseIndexInfo.php b/src/DenseIndexInfo.php new file mode 100644 index 0000000..380a92f --- /dev/null +++ b/src/DenseIndexInfo.php @@ -0,0 +1,23 @@ + $this->dimension, + 'similarityFunction' => $this->similarityFunction, + 'embeddingModel' => $this->embeddingModel, + ]; + } +} diff --git a/src/Enums/IndexType.php b/src/Enums/IndexType.php new file mode 100644 index 0000000..90f833a --- /dev/null +++ b/src/Enums/IndexType.php @@ -0,0 +1,11 @@ + $this->dimension, 'similarityFunction' => $this->similarityFunction, 'namespaces' => $this->namespaces, + 'indexType' => $this->indexType->value, + 'denseIndex' => $this->denseIndex?->toArray(), + 'sparseIndex' => $this->sparseIndex?->toArray(), ]; } } diff --git a/src/Operations/GetIndexInfoOperation.php b/src/Operations/GetIndexInfoOperation.php index 8f8ff60..910337d 100644 --- a/src/Operations/GetIndexInfoOperation.php +++ b/src/Operations/GetIndexInfoOperation.php @@ -3,9 +3,12 @@ namespace Upstash\Vector\Operations; use Upstash\Vector\Contracts\TransporterInterface; +use Upstash\Vector\DenseIndexInfo; +use Upstash\Vector\Enums\IndexType; use Upstash\Vector\IndexInfo; use Upstash\Vector\NamespaceInfo; use Upstash\Vector\Operations\Concerns\AssertsApiResponseErrors; +use Upstash\Vector\SparseIndexInfo; use Upstash\Vector\Transporter\ContentType; use Upstash\Vector\Transporter\Method; use Upstash\Vector\Transporter\TransporterRequest; @@ -55,6 +58,31 @@ private function transformResponse(TransporterResponse $response): IndexInfo dimension: $result['dimension'], similarityFunction: $result['similarityFunction'], namespaces: $namespaces, + indexType: IndexType::tryFrom($result['indexType'] ?? '') ?? IndexType::UNKNOWN, + denseIndex: isset($result['denseIndex']) ? $this->transformDenseIndex($result['denseIndex']) : null, + sparseIndex: isset($result['sparseIndex']) ? $this->transformSparseIndex($result['sparseIndex']) : null, + ); + } + + /** + * @param array $data + */ + private function transformDenseIndex(array $data): DenseIndexInfo + { + return new DenseIndexInfo( + dimension: $data['dimension'] ?? 0, + similarityFunction: $data['similarityFunction'] ?? '', + embeddingModel: $data['embeddingModel'] ?? '', + ); + } + + /** + * @param array $data + */ + private function transformSparseIndex(array $data): SparseIndexInfo + { + return new SparseIndexInfo( + embeddingModel: $data['embeddingModel'] ?? '', ); } } diff --git a/src/SparseIndexInfo.php b/src/SparseIndexInfo.php new file mode 100644 index 0000000..2907d7f --- /dev/null +++ b/src/SparseIndexInfo.php @@ -0,0 +1,19 @@ + $this->embeddingModel, + ]; + } +} diff --git a/tests/Dense/Operations/GetIndexInfoTest.php b/tests/Dense/Operations/GetIndexInfoTest.php index 19a51c3..b4e05c4 100644 --- a/tests/Dense/Operations/GetIndexInfoTest.php +++ b/tests/Dense/Operations/GetIndexInfoTest.php @@ -3,6 +3,7 @@ namespace Upstash\Vector\Tests\Dense\Operations; use PHPUnit\Framework\TestCase; +use Upstash\Vector\Enums\IndexType; use Upstash\Vector\IndexInfo; use Upstash\Vector\Tests\Concerns\UsesDenseIndex; @@ -15,5 +16,10 @@ public function test_get_info(): void $info = $this->index->getInfo(); $this->assertInstanceOf(IndexInfo::class, $info); + $this->assertSame(IndexType::DENSE, $info->indexType); + $this->assertNull($info->sparseIndex); + $this->assertNotNull($info->denseIndex); + $this->assertSame(2, $info->denseIndex->dimension); + $this->assertSame('COSINE', $info->denseIndex->similarityFunction); } } From cf6f4d390dd4412f6a70e1dabd47ffe76a44c165 Mon Sep 17 00:00:00 2001 From: ytkimirti Date: Wed, 19 Feb 2025 11:25:55 +0100 Subject: [PATCH 2/3] test: add tests for dense and hybrid index info --- tests/Hybrid/Operations/GetIndexInfoTest.php | 23 +++++++++++++++++++ tests/Sparse/Operations/GetIndexInfoTest.php | 24 ++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/Hybrid/Operations/GetIndexInfoTest.php create mode 100644 tests/Sparse/Operations/GetIndexInfoTest.php diff --git a/tests/Hybrid/Operations/GetIndexInfoTest.php b/tests/Hybrid/Operations/GetIndexInfoTest.php new file mode 100644 index 0000000..b93043a --- /dev/null +++ b/tests/Hybrid/Operations/GetIndexInfoTest.php @@ -0,0 +1,23 @@ +index->getInfo(); + + $this->assertInstanceOf(IndexInfo::class, $info); + $this->assertSame(IndexType::HYBRID, $info->indexType); + $this->assertNotNull($info->sparseIndex); + $this->assertNotNull($info->denseIndex); + } +} diff --git a/tests/Sparse/Operations/GetIndexInfoTest.php b/tests/Sparse/Operations/GetIndexInfoTest.php new file mode 100644 index 0000000..a1df07c --- /dev/null +++ b/tests/Sparse/Operations/GetIndexInfoTest.php @@ -0,0 +1,24 @@ +index->getInfo(); + + $this->assertInstanceOf(IndexInfo::class, $info); + $this->assertSame(IndexType::SPARSE, $info->indexType); + $this->assertNotNull($info->sparseIndex); + $this->assertNull($info->denseIndex); + $this->assertSame('BM25', $info->sparseIndex->embeddingModel); + } +} From 9c729c3c55e01dc8dc8f8465313d60f0ba20a758 Mon Sep 17 00:00:00 2001 From: ytkimirti Date: Wed, 19 Feb 2025 11:26:15 +0100 Subject: [PATCH 3/3] chore: remove prefix in import --- src/Iterators/VectorRangeIterator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Iterators/VectorRangeIterator.php b/src/Iterators/VectorRangeIterator.php index c7f6964..e9d6d55 100644 --- a/src/Iterators/VectorRangeIterator.php +++ b/src/Iterators/VectorRangeIterator.php @@ -2,6 +2,7 @@ namespace Upstash\Vector\Iterators; +use Iterator; use Upstash\Vector\Operations\RangeVectorsOperation; use Upstash\Vector\VectorMatch; use Upstash\Vector\VectorRange; @@ -10,7 +11,7 @@ /** * @implements \Iterator */ -class VectorRangeIterator implements \Iterator +class VectorRangeIterator implements Iterator { private string $nextCursor; @@ -46,7 +47,6 @@ public function next(): void $this->results = $rangeResult->getResults(); $this->position = 0; } - } public function key(): string