This repository was archived by the owner on Jul 16, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 7 files changed +89
-3
lines changed Expand file tree Collapse file tree 7 files changed +89
-3
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace PhpLlm \LlmChain \Document ;
6+
7+ use PhpLlm \LlmChain \Exception \RuntimeException ;
8+
9+ final class NullVector implements VectorInterface
10+ {
11+ public function getData (): array
12+ {
13+ throw new RuntimeException ('getData() method cannot be called on a NullVector. ' );
14+ }
15+
16+ public function getDimensions (): int
17+ {
18+ throw new RuntimeException ('getDimensions() method cannot be called on a NullVector. ' );
19+ }
20+ }
Original file line number Diff line number Diff line change 66
77use PhpLlm \LlmChain \Exception \InvalidArgumentException ;
88
9- final class Vector
9+ final class Vector implements VectorInterface
1010{
1111 /**
1212 * @param list<float> $data
Original file line number Diff line number Diff line change 1010{
1111 public function __construct (
1212 public Uuid $ id ,
13- public Vector $ vector ,
13+ public VectorInterface $ vector ,
1414 public Metadata $ metadata = new Metadata (),
1515 ) {
1616 }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace PhpLlm \LlmChain \Document ;
6+
7+ interface VectorInterface
8+ {
9+ /**
10+ * @return list<float>
11+ */
12+ public function getData (): array ;
13+
14+ public function getDimensions (): int ;
15+ }
Original file line number Diff line number Diff line change 55namespace PhpLlm \LlmChain \Store \Azure ;
66
77use PhpLlm \LlmChain \Document \Metadata ;
8+ use PhpLlm \LlmChain \Document \NullVector ;
89use PhpLlm \LlmChain \Document \Vector ;
910use PhpLlm \LlmChain \Document \VectorDocument ;
1011use PhpLlm \LlmChain \Store \VectorStoreInterface ;
@@ -80,7 +81,9 @@ private function convertToVectorDocument(array $data): VectorDocument
8081 {
8182 return new VectorDocument (
8283 id: Uuid::fromString ($ data ['id ' ]),
83- vector: $ data [$ this ->vectorFieldName ] ? new Vector ($ data [$ this ->vectorFieldName ]) : null ,
84+ vector: !array_key_exists ($ this ->vectorFieldName , $ data ) || null === $ data [$ this ->vectorFieldName ]
85+ ? new NullVector ()
86+ : new Vector ($ data [$ this ->vectorFieldName ]),
8487 metadata: new Metadata ($ data ),
8588 );
8689 }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace PhpLlm \LlmChain \Tests \Document ;
6+
7+ use PhpLlm \LlmChain \Document \NullVector ;
8+ use PhpLlm \LlmChain \Document \VectorInterface ;
9+ use PhpLlm \LlmChain \Exception \RuntimeException ;
10+ use PHPUnit \Framework \Attributes \CoversClass ;
11+ use PHPUnit \Framework \Attributes \Test ;
12+ use PHPUnit \Framework \TestCase ;
13+
14+ #[CoversClass(NullVector::class)]
15+ final class NullVectorTest extends TestCase
16+ {
17+ #[Test]
18+ public function implementsInterface (): void
19+ {
20+ self ::assertInstanceOf (VectorInterface::class, new NullVector ());
21+ }
22+
23+ #[Test]
24+ public function getDataThrowsOnAccess (): void
25+ {
26+ $ this ->expectException (RuntimeException::class);
27+
28+ (new NullVector ())->getData ();
29+ }
30+
31+ #[Test]
32+ public function getDimensionsThrowsOnAccess (): void
33+ {
34+ $ this ->expectException (RuntimeException::class);
35+
36+ (new NullVector ())->getDimensions ();
37+ }
38+ }
Original file line number Diff line number Diff line change 55namespace PhpLlm \LlmChain \Tests \Document ;
66
77use PhpLlm \LlmChain \Document \Vector ;
8+ use PhpLlm \LlmChain \Document \VectorInterface ;
89use PHPUnit \Framework \Attributes \CoversClass ;
910use PHPUnit \Framework \Attributes \Test ;
1011use PHPUnit \Framework \TestCase ;
1112
1213#[CoversClass(Vector::class)]
1314final class VectorTest extends TestCase
1415{
16+ #[Test]
17+ public function implementsInterface (): void
18+ {
19+ self ::assertInstanceOf (
20+ VectorInterface::class,
21+ new Vector ([1.0 , 2.0 , 3.0 ])
22+ );
23+ }
24+
1525 #[Test]
1626 public function withDimensionNull (): void
1727 {
You can’t perform that action at this time.
0 commit comments