Skip to content
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 1 commit into from
Dec 6, 2024
Merged
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
7 changes: 6 additions & 1 deletion src/Bridge/OpenAI/Embeddings/ResponseConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public function convert(ResponseInterface $response, array $options = []): Vecto
{
$data = $response->toArray();

return new VectorResponse(new Vector($data['data'][0]['embedding']));
return new VectorResponse(
...\array_map(
static fn (array $item): Vector => new Vector($item['embedding']),
$data['data']
),
);
}
}
56 changes: 56 additions & 0 deletions tests/Bridge/OpenAI/Embeddings/ResponseConverterTest.php
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);
Copy link
Contributor

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?

Copy link
Contributor Author

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 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

CleanShot 2024-12-06 at 12 49 02@2x

Copy link
Contributor Author

@DZunke DZunke Dec 6, 2024

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.

Copy link
Member

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

$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;
}
}
Loading