Skip to content

Commit ebce2e8

Browse files
committed
[Platform] Fix Albert API embeddings (#846)
1 parent d15d098 commit ebce2e8

File tree

2 files changed

+47
-60
lines changed

2 files changed

+47
-60
lines changed

src/platform/src/Bridge/Albert/EmbeddingsModelClient.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public function request(Model $model, array|string $payload, array $options = []
3939
{
4040
return new RawHttpResult($this->httpClient->request('POST', \sprintf('%s/embeddings', $this->baseUrl), [
4141
'auth_bearer' => $this->apiKey,
42-
'json' => \is_array($payload) ? array_merge($payload, $options) : $payload,
42+
'json' => array_merge($options, [
43+
'model' => $model->getName(),
44+
'input' => $payload,
45+
]),
4346
]));
4447
}
4548
}

src/platform/tests/Bridge/Albert/EmbeddingsModelClientTest.php

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111

1212
namespace Symfony\AI\Platform\Tests\Bridge\Albert;
1313

14-
use PHPUnit\Framework\Attributes\DataProvider;
1514
use PHPUnit\Framework\TestCase;
1615
use Symfony\AI\Platform\Bridge\Albert\EmbeddingsModelClient;
1716
use Symfony\AI\Platform\Bridge\OpenAi\Embeddings;
1817
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
1918
use Symfony\Component\HttpClient\MockHttpClient;
2019
use Symfony\Component\HttpClient\Response\JsonMockResponse;
20+
use Symfony\Component\HttpClient\Response\MockResponse;
21+
use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse;
2122

2223
final class EmbeddingsModelClientTest extends TestCase
2324
{
@@ -29,7 +30,7 @@ public function testSupportsEmbeddingsModel()
2930
'https://albert.example.com/'
3031
);
3132

32-
$embeddingsModel = new Embeddings('text-embedding-ada-002');
33+
$embeddingsModel = new Embeddings('embedding-small');
3334
$this->assertTrue($client->supports($embeddingsModel));
3435
}
3536

@@ -45,66 +46,49 @@ public function testDoesNotSupportNonEmbeddingsModel()
4546
$this->assertFalse($client->supports($gptModel));
4647
}
4748

48-
#[DataProvider('providePayloadToJson')]
49-
public function testRequestSendsCorrectHttpRequest(array|string $payload, array $options, array|string $expectedJson)
49+
public function testItIsExecutingTheCorrectRequest()
5050
{
51-
$capturedRequest = null;
52-
$httpClient = new MockHttpClient(function ($method, $url, $options) use (&$capturedRequest) {
53-
$capturedRequest = ['method' => $method, 'url' => $url, 'options' => $options];
54-
55-
return new JsonMockResponse(['data' => []]);
56-
});
57-
58-
$client = new EmbeddingsModelClient(
59-
$httpClient,
60-
'test-api-key',
61-
'https://albert.example.com/v1'
62-
);
51+
$resultCallback = static function (string $method, string $url, array $options): HttpResponse {
52+
self::assertSame('POST', $method);
53+
self::assertSame('https://albert.example.com/v1/embeddings', $url);
54+
self::assertSame('Authorization: Bearer api-key', $options['normalized_headers']['authorization'][0]);
55+
self::assertSame('{"model":"embedding-small","input":"test text"}', $options['body']);
56+
57+
return new MockResponse();
58+
};
59+
$httpClient = new MockHttpClient([$resultCallback]);
60+
$modelClient = new EmbeddingsModelClient($httpClient, 'api-key', 'https://albert.example.com/v1');
61+
$modelClient->request(new Embeddings('embedding-small'), 'test text', []);
62+
}
6363

64-
$model = new Embeddings('text-embedding-ada-002');
65-
$result = $client->request($model, $payload, $options);
66-
67-
$this->assertNotNull($capturedRequest);
68-
$this->assertSame('POST', $capturedRequest['method']);
69-
$this->assertSame('https://albert.example.com/v1/embeddings', $capturedRequest['url']);
70-
$this->assertArrayHasKey('normalized_headers', $capturedRequest['options']);
71-
$this->assertArrayHasKey('authorization', $capturedRequest['options']['normalized_headers']);
72-
$this->assertStringContainsString('Bearer test-api-key', (string) $capturedRequest['options']['normalized_headers']['authorization'][0]);
73-
74-
// Check JSON body - it might be in 'body' after processing
75-
if (isset($capturedRequest['options']['body'])) {
76-
$actualJson = json_decode($capturedRequest['options']['body'], true);
77-
$this->assertEquals($expectedJson, $actualJson);
78-
} else {
79-
$this->assertSame($expectedJson, $capturedRequest['options']['json']);
80-
}
64+
public function testItIsExecutingTheCorrectRequestWithCustomOptions()
65+
{
66+
$resultCallback = static function (string $method, string $url, array $options): HttpResponse {
67+
self::assertSame('POST', $method);
68+
self::assertSame('https://albert.example.com/v1/embeddings', $url);
69+
self::assertSame('Authorization: Bearer api-key', $options['normalized_headers']['authorization'][0]);
70+
self::assertSame('{"dimensions":256,"model":"embedding-small","input":"test text"}', $options['body']);
71+
72+
return new MockResponse();
73+
};
74+
$httpClient = new MockHttpClient([$resultCallback]);
75+
$modelClient = new EmbeddingsModelClient($httpClient, 'api-key', 'https://albert.example.com/v1');
76+
$modelClient->request(new Embeddings('embedding-small'), 'test text', ['dimensions' => 256]);
8177
}
8278

83-
public static function providePayloadToJson(): iterable
79+
public function testItIsExecutingTheCorrectRequestWithArrayInput()
8480
{
85-
yield 'with array payload and no options' => [
86-
['input' => 'test text', 'model' => 'text-embedding-ada-002'],
87-
[],
88-
['input' => 'test text', 'model' => 'text-embedding-ada-002'],
89-
];
90-
91-
yield 'with string payload and no options' => [
92-
'test text',
93-
[],
94-
'test text',
95-
];
96-
97-
yield 'with array payload and options' => [
98-
['input' => 'test text', 'model' => 'text-embedding-ada-002'],
99-
['dimensions' => 1536],
100-
['dimensions' => 1536, 'input' => 'test text', 'model' => 'text-embedding-ada-002'],
101-
];
102-
103-
yield 'options override payload values' => [
104-
['input' => 'test text', 'model' => 'text-embedding-ada-002'],
105-
['model' => 'text-embedding-3-small'],
106-
['model' => 'text-embedding-3-small', 'input' => 'test text'],
107-
];
81+
$resultCallback = static function (string $method, string $url, array $options): HttpResponse {
82+
self::assertSame('POST', $method);
83+
self::assertSame('https://albert.example.com/v1/embeddings', $url);
84+
self::assertSame('Authorization: Bearer api-key', $options['normalized_headers']['authorization'][0]);
85+
self::assertSame('{"model":"embedding-small","input":["text1","text2","text3"]}', $options['body']);
86+
87+
return new MockResponse();
88+
};
89+
$httpClient = new MockHttpClient([$resultCallback]);
90+
$modelClient = new EmbeddingsModelClient($httpClient, 'api-key', 'https://albert.example.com/v1');
91+
$modelClient->request(new Embeddings('embedding-small'), ['text1', 'text2', 'text3'], []);
10892
}
10993

11094
public function testRequestHandlesBaseUrlWithoutTrailingSlash()
@@ -122,7 +106,7 @@ public function testRequestHandlesBaseUrlWithoutTrailingSlash()
122106
'https://albert.example.com/v1'
123107
);
124108

125-
$model = new Embeddings('text-embedding-ada-002');
109+
$model = new Embeddings('embedding-small');
126110
$client->request($model, ['input' => 'test']);
127111

128112
$this->assertSame('https://albert.example.com/v1/embeddings', $capturedUrl);
@@ -143,7 +127,7 @@ public function testRequestHandlesBaseUrlWithTrailingSlash()
143127
'https://albert.example.com/v1'
144128
);
145129

146-
$model = new Embeddings('text-embedding-ada-002');
130+
$model = new Embeddings('embedding-small');
147131
$client->request($model, ['input' => 'test']);
148132

149133
$this->assertSame('https://albert.example.com/v1/embeddings', $capturedUrl);

0 commit comments

Comments
 (0)