File tree 7 files changed +89
-3
lines changed
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 6
6
7
7
use PhpLlm \LlmChain \Exception \InvalidArgumentException ;
8
8
9
- final class Vector
9
+ final class Vector implements VectorInterface
10
10
{
11
11
/**
12
12
* @param list<float> $data
Original file line number Diff line number Diff line change 10
10
{
11
11
public function __construct (
12
12
public Uuid $ id ,
13
- public Vector $ vector ,
13
+ public VectorInterface $ vector ,
14
14
public Metadata $ metadata = new Metadata (),
15
15
) {
16
16
}
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 5
5
namespace PhpLlm \LlmChain \Store \Azure ;
6
6
7
7
use PhpLlm \LlmChain \Document \Metadata ;
8
+ use PhpLlm \LlmChain \Document \NullVector ;
8
9
use PhpLlm \LlmChain \Document \Vector ;
9
10
use PhpLlm \LlmChain \Document \VectorDocument ;
10
11
use PhpLlm \LlmChain \Store \VectorStoreInterface ;
@@ -80,7 +81,9 @@ private function convertToVectorDocument(array $data): VectorDocument
80
81
{
81
82
return new VectorDocument (
82
83
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 ]),
84
87
metadata: new Metadata ($ data ),
85
88
);
86
89
}
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 5
5
namespace PhpLlm \LlmChain \Tests \Document ;
6
6
7
7
use PhpLlm \LlmChain \Document \Vector ;
8
+ use PhpLlm \LlmChain \Document \VectorInterface ;
8
9
use PHPUnit \Framework \Attributes \CoversClass ;
9
10
use PHPUnit \Framework \Attributes \Test ;
10
11
use PHPUnit \Framework \TestCase ;
11
12
12
13
#[CoversClass(Vector::class)]
13
14
final class VectorTest extends TestCase
14
15
{
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
+
15
25
#[Test]
16
26
public function withDimensionNull (): void
17
27
{
You can’t perform that action at this time.
0 commit comments