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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As is mentioned elsewhere, I'd move this into isEmbeddingList so we aren't iterating over everything twice. Those iterations add up when dealing with 3072 dimension vector, for instance

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if this should be RuntimeException instead of InvalidArgumentException? The realistic trigger here is someone generates an embedding result and it gets passed in here where it errors out. So it's not necessarily an argument the user is passing in, it's an invalid return from the LLM. We use RuntimeException elsewhere for those cases but likely splitting hairs here.

May be nice to return the index and value so a user can track down where things went wrong though

}
}

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could likely have better test coverage here. For instance, no new tests for EmbeddingResult are here, which is the most likely path someone will use in production. May also want tests to verify things like PHP_FLOAT_MAX, -0.0, 0.0, and plain ints still pass.

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