Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/Results/DTO/Embedding.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public function __construct(array $values, int $dimensions)
throw new InvalidArgumentException('Embedding values must be integers or floats.');
}

foreach ($values as $value) {
if (is_float($value) && !is_finite($value)) {
throw new InvalidArgumentException('Embedding values must be finite numbers.');
}
}

if (count($values) !== $dimensions) {
throw new InvalidArgumentException('Embedding vector length must match dimensions.');
}
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/Results/DTO/EmbeddingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,27 @@ public function testValuesMustBeNumeric(): void

new Embedding([0.1, '0.2'], 2);
}

/**
* @dataProvider nonFiniteValueProvider
*/
public function testValuesMustBeFinite(float $value): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Embedding values must be finite numbers.');

new Embedding([0.1, $value], 2);
}

/**
* @return array<string, array{float}>
*/
public static function nonFiniteValueProvider(): array
{
return [
'NAN' => [NAN],
'INF' => [INF],
'-INF' => [-INF],
];
}
}
Loading