-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parse OpenAI response embedding to contain all embeddings #150
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Tests\Bridge\OpenAI\Embeddings; | ||
|
||
use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings\ResponseConverter; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Small; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
#[CoversClass(ResponseConverter::class)] | ||
#[Small] | ||
class ResponseConverterTest extends TestCase | ||
{ | ||
#[Test] | ||
public function itConvertsAResponseToAVectorResponse(): void | ||
{ | ||
$response = $this->createStub(ResponseInterface::class); | ||
$response | ||
->method('toArray') | ||
->willReturn(\json_decode($this->getEmbeddingStub(), true)); | ||
|
||
$vectorResponse = (new ResponseConverter())->convert($response); | ||
$convertedContent = $vectorResponse->getContent(); | ||
|
||
self::assertIsArray($convertedContent); | ||
self::assertCount(2, $convertedContent); | ||
|
||
self::assertSame([0.3, 0.4, 0.4], $convertedContent[0]->getData()); | ||
self::assertSame([0.0, 0.0, 0.2], $convertedContent[1]->getData()); | ||
} | ||
|
||
private function getEmbeddingStub(): string | ||
{ | ||
return <<<'JSON' | ||
{ | ||
"object": "list", | ||
"data": [ | ||
{ | ||
"object": "embedding", | ||
"index": 0, | ||
"embedding": [0.3, 0.4, 0.4] | ||
}, | ||
{ | ||
"object": "embedding", | ||
"index": 1, | ||
"embedding": [0.0, 0.0, 0.2] | ||
} | ||
] | ||
} | ||
JSON; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not using a JsonMockResponse directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you maybe have more information to get me on your level here? I am a bit stuck with that you could mean with a
JsonMockResponse
🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! Ok. Nice one. But what would be the benefit from not mocking the
ResponseInterface
here? The only thing i get from the documentation is that it would improve testing timeouts and chunked responses, which i do not want to do.But nethertheless i have tried it and got the following exception that hints me to utilize the mock http client but i do not utilize any http client for my test as i am testing the response converter itself.
Symfony\Component\HttpClient\Exception\InvalidArgumentException: MockResponse instances must be issued by MockHttpClient before processing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think in this case the benefit is not really there yet - more like a question of style, e.g. not using mock frameworks but implementations.
in general i can think of some infrastructure for MockResponse from file where also processing is done already - when oop design is done, it could be a quite common scenario to test those response converters and contract is
ResponseInterface
yup