Skip to content

Commit 07e31b1

Browse files
committed
feat: expose more information about the index
1 parent 99357ea commit 07e31b1

File tree

6 files changed

+94
-0
lines changed

6 files changed

+94
-0
lines changed

src/DenseIndexInfo.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Upstash\Vector;
4+
5+
use Upstash\Vector\Contracts\Arrayable;
6+
7+
final readonly class DenseIndexInfo implements Arrayable
8+
{
9+
public function __construct(
10+
public int $dimension = 0,
11+
public string $similarityFunction = '',
12+
public string $embeddingModel = '',
13+
) {}
14+
15+
public function toArray(): array
16+
{
17+
return [
18+
'dimension' => $this->dimension,
19+
'similarityFunction' => $this->similarityFunction,
20+
'embeddingModel' => $this->embeddingModel,
21+
];
22+
}
23+
}

src/Enums/IndexType.php

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

src/IndexInfo.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Upstash\Vector;
44

55
use Upstash\Vector\Contracts\Arrayable;
6+
use Upstash\Vector\Enums\IndexType;
67

78
final readonly class IndexInfo implements Arrayable
89
{
@@ -16,6 +17,9 @@ public function __construct(
1617
public int $dimension = 0,
1718
public string $similarityFunction = '',
1819
public array $namespaces = [],
20+
public IndexType $indexType = IndexType::UNKNOWN,
21+
public ?DenseIndexInfo $denseIndex = null,
22+
public ?SparseIndexInfo $sparseIndex = null,
1923
) {}
2024

2125
public function namespace(string $namespace): NamespaceInfo
@@ -38,6 +42,9 @@ public function toArray(): array
3842
'dimension' => $this->dimension,
3943
'similarityFunction' => $this->similarityFunction,
4044
'namespaces' => $this->namespaces,
45+
'indexType' => $this->indexType->value,
46+
'denseIndex' => $this->denseIndex?->toArray(),
47+
'sparseIndex' => $this->sparseIndex?->toArray(),
4148
];
4249
}
4350
}

src/Operations/GetIndexInfoOperation.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace Upstash\Vector\Operations;
44

55
use Upstash\Vector\Contracts\TransporterInterface;
6+
use Upstash\Vector\DenseIndexInfo;
7+
use Upstash\Vector\Enums\IndexType;
68
use Upstash\Vector\IndexInfo;
79
use Upstash\Vector\NamespaceInfo;
810
use Upstash\Vector\Operations\Concerns\AssertsApiResponseErrors;
11+
use Upstash\Vector\SparseIndexInfo;
912
use Upstash\Vector\Transporter\ContentType;
1013
use Upstash\Vector\Transporter\Method;
1114
use Upstash\Vector\Transporter\TransporterRequest;
@@ -55,6 +58,31 @@ private function transformResponse(TransporterResponse $response): IndexInfo
5558
dimension: $result['dimension'],
5659
similarityFunction: $result['similarityFunction'],
5760
namespaces: $namespaces,
61+
indexType: IndexType::tryFrom($result['indexType'] ?? '') ?? IndexType::UNKNOWN,
62+
denseIndex: isset($result['denseIndex']) ? $this->transformDenseIndex($result['denseIndex']) : null,
63+
sparseIndex: isset($result['sparseIndex']) ? $this->transformSparseIndex($result['sparseIndex']) : null,
64+
);
65+
}
66+
67+
/**
68+
* @param array<string, mixed> $data
69+
*/
70+
private function transformDenseIndex(array $data): DenseIndexInfo
71+
{
72+
return new DenseIndexInfo(
73+
dimension: $data['dimension'] ?? 0,
74+
similarityFunction: $data['similarityFunction'] ?? '',
75+
embeddingModel: $data['embeddingModel'] ?? '',
76+
);
77+
}
78+
79+
/**
80+
* @param array<string, mixed> $data
81+
*/
82+
private function transformSparseIndex(array $data): SparseIndexInfo
83+
{
84+
return new SparseIndexInfo(
85+
embeddingModel: $data['embeddingModel'] ?? '',
5886
);
5987
}
6088
}

src/SparseIndexInfo.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Upstash\Vector;
4+
5+
use Upstash\Vector\Contracts\Arrayable;
6+
7+
final readonly class SparseIndexInfo implements Arrayable
8+
{
9+
public function __construct(
10+
public string $embeddingModel = '',
11+
) {}
12+
13+
public function toArray(): array
14+
{
15+
return [
16+
'embeddingModel' => $this->embeddingModel,
17+
];
18+
}
19+
}

tests/Dense/Operations/GetIndexInfoTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Upstash\Vector\Tests\Dense\Operations;
44

55
use PHPUnit\Framework\TestCase;
6+
use Upstash\Vector\Enums\IndexType;
67
use Upstash\Vector\IndexInfo;
78
use Upstash\Vector\Tests\Concerns\UsesDenseIndex;
89

@@ -15,5 +16,10 @@ public function test_get_info(): void
1516
$info = $this->index->getInfo();
1617

1718
$this->assertInstanceOf(IndexInfo::class, $info);
19+
$this->assertSame(IndexType::DENSE, $info->indexType);
20+
$this->assertNull($info->sparseIndex);
21+
$this->assertNotNull($info->denseIndex);
22+
$this->assertSame(2, $info->denseIndex->dimension);
23+
$this->assertSame('COSINE', $info->denseIndex->similarityFunction);
1824
}
1925
}

0 commit comments

Comments
 (0)