diff --git a/src/Model/Prompt/PromptTemplate.php b/src/Model/Prompt/PromptTemplate.php index e3ad65d..e198555 100644 --- a/src/Model/Prompt/PromptTemplate.php +++ b/src/Model/Prompt/PromptTemplate.php @@ -23,7 +23,7 @@ */ final class PromptTemplate implements \Stringable { - private readonly string $template; + private string $template; private ?string $prompt = null; diff --git a/src/Retrieval/Loader/Directory/DirectoryLoader.php b/src/Retrieval/Loader/Directory/DirectoryLoader.php index b70e91c..f4604c5 100644 --- a/src/Retrieval/Loader/Directory/DirectoryLoader.php +++ b/src/Retrieval/Loader/Directory/DirectoryLoader.php @@ -64,7 +64,7 @@ public function load(AbstractReader $reader = new FileReader()): iterable public function loadAndSplit(SplitterInterface $splitter, AbstractReader $reader = new FileReader()): iterable { foreach ($this->load($reader) as $document) { - yield from $splitter->splitDocuments($document); + yield from $splitter->splitDocument($document); } } } diff --git a/src/Retrieval/Splitter/SplitterInterface.php b/src/Retrieval/Splitter/SplitterInterface.php index da1cdf0..5f7e62e 100644 --- a/src/Retrieval/Splitter/SplitterInterface.php +++ b/src/Retrieval/Splitter/SplitterInterface.php @@ -28,7 +28,8 @@ interface SplitterInterface public function splitText(string $text): iterable; /** - * @return iterable + * @param iterable $splits + * @return iterable */ public function createDocuments(iterable $splits): iterable; diff --git a/src/Retrieval/Splitter/TextSplitter.php b/src/Retrieval/Splitter/TextSplitter.php index b2f14d5..8357167 100644 --- a/src/Retrieval/Splitter/TextSplitter.php +++ b/src/Retrieval/Splitter/TextSplitter.php @@ -92,7 +92,7 @@ public function createDocuments(iterable $splits): iterable { foreach ($splits as $index => $chunk) { yield new Document($chunk, metadata: new Metadata( - hash: md5((string) $chunk), + hash: md5($chunk), chunkNumber: $index )); } diff --git a/tests/Retrieval/VectorStore/DistanceTest.php b/tests/Retrieval/VectorStore/DistanceTest.php new file mode 100644 index 0000000..a0142e6 --- /dev/null +++ b/tests/Retrieval/VectorStore/DistanceTest.php @@ -0,0 +1,61 @@ + + */ +final class DistanceTest extends TestCase +{ + public function testVectorWithDifferentDimensions(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Vectors must have the same dimension.'); + + $distance = Distance::COSINE; + $a = [2, 4, 1, 3]; + $b = [3, 5, 2]; + + $distance->compute($a, $b); + } + + public function testDifferentVectorDistances(): void + { + $a = [2, 4, 1, 3]; + $b = [3, 5, 2, 1]; + + $this->assertEquals(0.0937, round(Distance::COSINE->compute($a, $b), 4)); + $this->assertEquals(2.6458, round(Distance::L2->compute($a, $b), 4)); + $this->assertEquals(5.0, round(Distance::L1->compute($a, $b), 4)); + $this->assertEquals(31.0, round(Distance::INNER_PRODUCT->compute($a, $b), 4)); + } + + public function testSimilarVectorDistances(): void + { + $a = [2, 4, 1, 3]; + $b = [2, 4, 1, 2]; + + $this->assertEquals(0.0141, round(Distance::COSINE->compute($a, $b), 4)); + $this->assertEquals(1.0, round(Distance::L2->compute($a, $b), 4)); + $this->assertEquals(1.0, round(Distance::L1->compute($a, $b), 4)); + $this->assertEquals(27.0, round(Distance::INNER_PRODUCT->compute($a, $b), 4)); + } + + public function testSameVectorDistances(): void + { + $a = [2, 4, 1, 3]; + $b = [2, 4, 1, 3]; + + $this->assertEquals(0.0, round(Distance::COSINE->compute($a, $b), 4)); + $this->assertEquals(0.0, round(Distance::L2->compute($a, $b), 4)); + $this->assertEquals(0.0, round(Distance::L1->compute($a, $b), 4)); + $this->assertEquals(30.0, round(Distance::INNER_PRODUCT->compute($a, $b), 4)); + } +}