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
23 changes: 23 additions & 0 deletions src/DenseIndexInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Upstash\Vector;

use Upstash\Vector\Contracts\Arrayable;

final readonly class DenseIndexInfo implements Arrayable
{
public function __construct(
public int $dimension = 0,
public string $similarityFunction = '',
public string $embeddingModel = '',
) {}

public function toArray(): array
{
return [
'dimension' => $this->dimension,
'similarityFunction' => $this->similarityFunction,
'embeddingModel' => $this->embeddingModel,
];
}
}
11 changes: 11 additions & 0 deletions src/Enums/IndexType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Upstash\Vector\Enums;

enum IndexType: string
{
case DENSE = 'DENSE';
case SPARSE = 'SPARSE';
case HYBRID = 'HYBRID';
case UNKNOWN = 'UNKNOWN';
}
7 changes: 7 additions & 0 deletions src/IndexInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Upstash\Vector;

use Upstash\Vector\Contracts\Arrayable;
use Upstash\Vector\Enums\IndexType;

final readonly class IndexInfo implements Arrayable
{
Expand All @@ -16,6 +17,9 @@ public function __construct(
public int $dimension = 0,
public string $similarityFunction = '',
public array $namespaces = [],
public IndexType $indexType = IndexType::UNKNOWN,
public ?DenseIndexInfo $denseIndex = null,
public ?SparseIndexInfo $sparseIndex = null,
) {}

public function namespace(string $namespace): NamespaceInfo
Expand All @@ -38,6 +42,9 @@ public function toArray(): array
'dimension' => $this->dimension,
'similarityFunction' => $this->similarityFunction,
'namespaces' => $this->namespaces,
'indexType' => $this->indexType->value,
'denseIndex' => $this->denseIndex?->toArray(),
'sparseIndex' => $this->sparseIndex?->toArray(),
];
}
}
4 changes: 2 additions & 2 deletions src/Iterators/VectorRangeIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Upstash\Vector\Iterators;

use Iterator;
use Upstash\Vector\Operations\RangeVectorsOperation;
use Upstash\Vector\VectorMatch;
use Upstash\Vector\VectorRange;
Expand All @@ -10,7 +11,7 @@
/**
* @implements \Iterator<string, VectorMatch>
*/
class VectorRangeIterator implements \Iterator
class VectorRangeIterator implements Iterator
{
private string $nextCursor;

Expand Down Expand Up @@ -46,7 +47,6 @@ public function next(): void
$this->results = $rangeResult->getResults();
$this->position = 0;
}

}

public function key(): string
Expand Down
28 changes: 28 additions & 0 deletions src/Operations/GetIndexInfoOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, mixed> $data
*/
private function transformDenseIndex(array $data): DenseIndexInfo
{
return new DenseIndexInfo(
dimension: $data['dimension'] ?? 0,
similarityFunction: $data['similarityFunction'] ?? '',
embeddingModel: $data['embeddingModel'] ?? '',
);
}

/**
* @param array<string, mixed> $data
*/
private function transformSparseIndex(array $data): SparseIndexInfo
{
return new SparseIndexInfo(
embeddingModel: $data['embeddingModel'] ?? '',
);
}
}
19 changes: 19 additions & 0 deletions src/SparseIndexInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Upstash\Vector;

use Upstash\Vector\Contracts\Arrayable;

final readonly class SparseIndexInfo implements Arrayable
{
public function __construct(
public string $embeddingModel = '',
) {}

public function toArray(): array
{
return [
'embeddingModel' => $this->embeddingModel,
];
}
}
6 changes: 6 additions & 0 deletions tests/Dense/Operations/GetIndexInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
23 changes: 23 additions & 0 deletions tests/Hybrid/Operations/GetIndexInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Upstash\Vector\Tests\Hybrid\Operations;

use PHPUnit\Framework\TestCase;
use Upstash\Vector\Enums\IndexType;
use Upstash\Vector\IndexInfo;
use Upstash\Vector\Tests\Concerns\UsesHybridIndex;

class GetIndexInfoTest extends TestCase
{
use UsesHybridIndex;

public function test_get_info(): void
{
$info = $this->index->getInfo();

$this->assertInstanceOf(IndexInfo::class, $info);
$this->assertSame(IndexType::HYBRID, $info->indexType);
$this->assertNotNull($info->sparseIndex);
$this->assertNotNull($info->denseIndex);
}
}
24 changes: 24 additions & 0 deletions tests/Sparse/Operations/GetIndexInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Upstash\Vector\Tests\Sparse\Operations;

use PHPUnit\Framework\TestCase;
use Upstash\Vector\Enums\IndexType;
use Upstash\Vector\IndexInfo;
use Upstash\Vector\Tests\Concerns\UsesSparseIndex;

class GetIndexInfoTest extends TestCase
{
use UsesSparseIndex;

public function test_get_info(): void
{
$info = $this->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);
}
}