Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce NullVector and VectorInterface #114

Merged
merged 5 commits into from
Oct 4, 2024
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
20 changes: 20 additions & 0 deletions src/Document/NullVector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Document;

use PhpLlm\LlmChain\Exception\RuntimeException;

final class NullVector implements VectorInterface
{
public function getData(): array
{
throw new RuntimeException('getData() method cannot be called on a NullVector.');
}

public function getDimensions(): int
{
throw new RuntimeException('getDimensions() method cannot be called on a NullVector.');
}
}
2 changes: 1 addition & 1 deletion src/Document/Vector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PhpLlm\LlmChain\Exception\InvalidArgumentException;

final class Vector
final class Vector implements VectorInterface
{
/**
* @param list<float> $data
Expand Down
2 changes: 1 addition & 1 deletion src/Document/VectorDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{
public function __construct(
public Uuid $id,
public Vector $vector,
public VectorInterface $vector,
public Metadata $metadata = new Metadata(),
) {
}
Expand Down
15 changes: 15 additions & 0 deletions src/Document/VectorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Document;

interface VectorInterface
{
/**
* @return list<float>
*/
public function getData(): array;

public function getDimensions(): int;
}
5 changes: 4 additions & 1 deletion src/Store/Azure/SearchStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpLlm\LlmChain\Store\Azure;

use PhpLlm\LlmChain\Document\Metadata;
use PhpLlm\LlmChain\Document\NullVector;
use PhpLlm\LlmChain\Document\Vector;
use PhpLlm\LlmChain\Document\VectorDocument;
use PhpLlm\LlmChain\Store\VectorStoreInterface;
Expand Down Expand Up @@ -80,7 +81,9 @@ private function convertToVectorDocument(array $data): VectorDocument
{
return new VectorDocument(
id: Uuid::fromString($data['id']),
vector: $data[$this->vectorFieldName] ? new Vector($data[$this->vectorFieldName]) : null,
vector: !array_key_exists($this->vectorFieldName, $data) || null === $data[$this->vectorFieldName]
? new NullVector()
: new Vector($data[$this->vectorFieldName]),
metadata: new Metadata($data),
);
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Document/NullVectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\Document;

use PhpLlm\LlmChain\Document\NullVector;
use PhpLlm\LlmChain\Document\VectorInterface;
use PhpLlm\LlmChain\Exception\RuntimeException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(NullVector::class)]
final class NullVectorTest extends TestCase
{
#[Test]
public function implementsInterface(): void
{
self::assertInstanceOf(VectorInterface::class, new NullVector());
}

#[Test]
public function getDataThrowsOnAccess(): void
{
$this->expectException(RuntimeException::class);

(new NullVector())->getData();
}

#[Test]
public function getDimensionsThrowsOnAccess(): void
{
$this->expectException(RuntimeException::class);

(new NullVector())->getDimensions();
}
}
10 changes: 10 additions & 0 deletions tests/Document/VectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@
namespace PhpLlm\LlmChain\Tests\Document;

use PhpLlm\LlmChain\Document\Vector;
use PhpLlm\LlmChain\Document\VectorInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(Vector::class)]
final class VectorTest extends TestCase
{
#[Test]
public function implementsInterface(): void
{
self::assertInstanceOf(
VectorInterface::class,
new Vector([1.0, 2.0, 3.0])
);
}

#[Test]
public function withDimensionNull(): void
{
Expand Down