Skip to content

Commit 3992a80

Browse files
committed
bug #1088 [Platform][Azure] Patch Azure OpenAI after responses API migration (chr-hertel)
This PR was merged into the main branch. Discussion ---------- [Platform][Azure] Patch Azure OpenAI after responses API migration | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Docs? | no | Issues | | License | MIT Follows #871 Commits ------- fc53587 Patch Azure OpenAI after responses API migration
2 parents e7c67d4 + fc53587 commit 3992a80

File tree

7 files changed

+83
-30
lines changed

7 files changed

+83
-30
lines changed

demo/config/reference.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<?php
22

3-
// This file is auto-generated and is for apps only. Bundles SHOULD NOT rely on its content.
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
411

512
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
613

src/platform/src/Bridge/Azure/OpenAi/GptModelClient.php renamed to src/platform/src/Bridge/Azure/OpenAi/CompletionsModelClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\AI\Platform\Bridge\Azure\OpenAi;
1313

14-
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
14+
use Symfony\AI\Platform\Bridge\Generic\CompletionsModel;
1515
use Symfony\AI\Platform\Exception\InvalidArgumentException;
1616
use Symfony\AI\Platform\Model;
1717
use Symfony\AI\Platform\ModelClientInterface;
@@ -22,7 +22,7 @@
2222
/**
2323
* @author Christopher Hertel <mail@christopher-hertel.de>
2424
*/
25-
final class GptModelClient implements ModelClientInterface
25+
final class CompletionsModelClient implements ModelClientInterface
2626
{
2727
private readonly EventSourceHttpClient $httpClient;
2828

@@ -53,7 +53,7 @@ public function __construct(
5353

5454
public function supports(Model $model): bool
5555
{
56-
return $model instanceof Gpt;
56+
return $model instanceof CompletionsModel;
5757
}
5858

5959
public function request(Model $model, object|array|string $payload, array $options = []): RawHttpResult

src/platform/src/Bridge/Azure/OpenAi/EmbeddingsModelClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\AI\Platform\Bridge\Azure\OpenAi;
1313

14-
use Symfony\AI\Platform\Bridge\OpenAi\Embeddings;
14+
use Symfony\AI\Platform\Bridge\Generic\EmbeddingsModel;
1515
use Symfony\AI\Platform\Exception\InvalidArgumentException;
1616
use Symfony\AI\Platform\Model;
1717
use Symfony\AI\Platform\ModelClientInterface;
@@ -53,7 +53,7 @@ public function __construct(
5353

5454
public function supports(Model $model): bool
5555
{
56-
return $model instanceof Embeddings;
56+
return $model instanceof EmbeddingsModel;
5757
}
5858

5959
public function request(Model $model, array|string $payload, array $options = []): RawHttpResult
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Platform\Bridge\Azure\OpenAi;
13+
14+
use Symfony\AI\Platform\Bridge\Generic\CompletionsModel;
15+
use Symfony\AI\Platform\Bridge\Generic\EmbeddingsModel;
16+
use Symfony\AI\Platform\Bridge\OpenAi\Embeddings;
17+
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
18+
use Symfony\AI\Platform\Bridge\OpenAi\ModelCatalog as OpenAiModelCatalog;
19+
use Symfony\AI\Platform\Capability;
20+
use Symfony\AI\Platform\ModelCatalog\AbstractModelCatalog;
21+
22+
/**
23+
* @author Oskar Stark <oskarstark@googlemail.com>
24+
*/
25+
final class ModelCatalog extends AbstractModelCatalog
26+
{
27+
/**
28+
* @param array<string, array{class: string, capabilities: list<Capability>}> $additionalModels
29+
*/
30+
public function __construct(array $additionalModels = [])
31+
{
32+
$defaultModels = (new OpenAiModelCatalog())->getModels();
33+
foreach ($defaultModels as $modelName => $modelData) {
34+
$defaultModels[$modelName] = $modelData;
35+
36+
if (Gpt::class === $modelData['class']) {
37+
$defaultModels[$modelName]['class'] = CompletionsModel::class;
38+
}
39+
if (Embeddings::class === $modelData['class']) {
40+
$defaultModels[$modelName]['class'] = EmbeddingsModel::class;
41+
}
42+
}
43+
44+
$this->models = array_merge($defaultModels, $additionalModels);
45+
}
46+
}

src/platform/src/Bridge/Azure/OpenAi/PlatformFactory.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
namespace Symfony\AI\Platform\Bridge\Azure\OpenAi;
1313

1414
use Psr\EventDispatcher\EventDispatcherInterface;
15-
use Symfony\AI\Platform\Bridge\OpenAi\Embeddings;
16-
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
17-
use Symfony\AI\Platform\Bridge\OpenAi\ModelCatalog;
15+
use Symfony\AI\Platform\Bridge\Generic\Completions;
16+
use Symfony\AI\Platform\Bridge\Generic\Embeddings;
1817
use Symfony\AI\Platform\Bridge\OpenAi\Whisper;
1918
use Symfony\AI\Platform\Bridge\OpenAi\Whisper\AudioNormalizer;
2019
use Symfony\AI\Platform\Contract;
@@ -39,13 +38,14 @@ public static function create(
3938
?EventDispatcherInterface $eventDispatcher = null,
4039
): Platform {
4140
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
42-
$embeddingsModelClient = new EmbeddingsModelClient($httpClient, $baseUrl, $deployment, $apiVersion, $apiKey);
43-
$gptModelClient = new GptModelClient($httpClient, $baseUrl, $deployment, $apiVersion, $apiKey);
44-
$whisperModelClient = new WhisperModelClient($httpClient, $baseUrl, $deployment, $apiVersion, $apiKey);
4541

4642
return new Platform(
47-
[$gptModelClient, $embeddingsModelClient, $whisperModelClient],
48-
[new Gpt\ResultConverter(), new Embeddings\ResultConverter(), new Whisper\ResultConverter()],
43+
[
44+
new EmbeddingsModelClient($httpClient, $baseUrl, $deployment, $apiVersion, $apiKey),
45+
new CompletionsModelClient($httpClient, $baseUrl, $deployment, $apiVersion, $apiKey),
46+
new WhisperModelClient($httpClient, $baseUrl, $deployment, $apiVersion, $apiKey),
47+
],
48+
[new Completions\ResultConverter(), new Embeddings\ResultConverter(), new Whisper\ResultConverter()],
4949
$modelCatalog,
5050
$contract ?? Contract::create(new AudioNormalizer()),
5151
$eventDispatcher,

src/platform/tests/Bridge/Azure/OpenAi/GptModelClientTest.php renamed to src/platform/tests/Bridge/Azure/OpenAi/CompletionsModelClientTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313

1414
use PHPUnit\Framework\Attributes\TestWith;
1515
use PHPUnit\Framework\TestCase;
16-
use Symfony\AI\Platform\Bridge\Azure\OpenAi\GptModelClient;
17-
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
16+
use Symfony\AI\Platform\Bridge\Azure\OpenAi\CompletionsModelClient;
17+
use Symfony\AI\Platform\Bridge\Generic\CompletionsModel;
1818
use Symfony\AI\Platform\Exception\InvalidArgumentException;
1919
use Symfony\Component\HttpClient\MockHttpClient;
2020
use Symfony\Component\HttpClient\Response\MockResponse;
2121

2222
/**
2323
* @author Oskar Stark <oskarstark@googlemail.com>
2424
*/
25-
final class GptModelClientTest extends TestCase
25+
final class CompletionsModelClientTest extends TestCase
2626
{
2727
#[TestWith(['http://test.azure.com', 'The base URL must not contain the protocol.'])]
2828
#[TestWith(['https://test.azure.com', 'The base URL must not contain the protocol.'])]
@@ -33,45 +33,45 @@ public function testItThrowsExceptionWhenBaseUrlContainsProtocol(string $invalid
3333
$this->expectException(InvalidArgumentException::class);
3434
$this->expectExceptionMessage($expectedMessage);
3535

36-
new GptModelClient(new MockHttpClient(), $invalidUrl, 'deployment', 'api-version', 'api-key');
36+
new CompletionsModelClient(new MockHttpClient(), $invalidUrl, 'deployment', 'api-version', 'api-key');
3737
}
3838

3939
public function testItThrowsExceptionWhenDeploymentIsEmpty()
4040
{
4141
$this->expectException(InvalidArgumentException::class);
4242
$this->expectExceptionMessage('The deployment must not be empty.');
4343

44-
new GptModelClient(new MockHttpClient(), 'test.azure.com', '', 'api-version', 'api-key');
44+
new CompletionsModelClient(new MockHttpClient(), 'test.azure.com', '', 'api-version', 'api-key');
4545
}
4646

4747
public function testItThrowsExceptionWhenApiVersionIsEmpty()
4848
{
4949
$this->expectException(InvalidArgumentException::class);
5050
$this->expectExceptionMessage('The API version must not be empty.');
5151

52-
new GptModelClient(new MockHttpClient(), 'test.azure.com', 'deployment', '', 'api-key');
52+
new CompletionsModelClient(new MockHttpClient(), 'test.azure.com', 'deployment', '', 'api-key');
5353
}
5454

5555
public function testItThrowsExceptionWhenApiKeyIsEmpty()
5656
{
5757
$this->expectException(InvalidArgumentException::class);
5858
$this->expectExceptionMessage('The API key must not be empty.');
5959

60-
new GptModelClient(new MockHttpClient(), 'test.azure.com', 'deployment', 'api-version', '');
60+
new CompletionsModelClient(new MockHttpClient(), 'test.azure.com', 'deployment', 'api-version', '');
6161
}
6262

6363
public function testItAcceptsValidParameters()
6464
{
65-
$client = new GptModelClient(new MockHttpClient(), 'test.azure.com', 'gpt-35-turbo', '2023-12-01-preview', 'valid-api-key');
65+
$client = new CompletionsModelClient(new MockHttpClient(), 'test.azure.com', 'gpt-35-turbo', '2023-12-01-preview', 'valid-api-key');
6666

67-
$this->assertInstanceOf(GptModelClient::class, $client);
67+
$this->assertInstanceOf(CompletionsModelClient::class, $client);
6868
}
6969

7070
public function testItIsSupportingTheCorrectModel()
7171
{
72-
$client = new GptModelClient(new MockHttpClient(), 'test.azure.com', 'deployment', '2023-12-01', 'api-key');
72+
$client = new CompletionsModelClient(new MockHttpClient(), 'test.azure.com', 'deployment', '2023-12-01', 'api-key');
7373

74-
$this->assertTrue($client->supports(new Gpt('gpt-4o')));
74+
$this->assertTrue($client->supports(new CompletionsModel('gpt-4o')));
7575
}
7676

7777
public function testItIsExecutingTheCorrectRequest()
@@ -86,7 +86,7 @@ public function testItIsExecutingTheCorrectRequest()
8686
};
8787

8888
$httpClient = new MockHttpClient([$resultCallback]);
89-
$client = new GptModelClient($httpClient, 'test.azure.com', 'gpt-deployment', '2023-12-01', 'test-api-key');
90-
$client->request(new Gpt('gpt-4o'), ['messages' => [['role' => 'user', 'content' => 'Hello']]]);
89+
$client = new CompletionsModelClient($httpClient, 'test.azure.com', 'gpt-deployment', '2023-12-01', 'test-api-key');
90+
$client->request(new CompletionsModel('gpt-4o'), ['messages' => [['role' => 'user', 'content' => 'Hello']]]);
9191
}
9292
}

src/platform/tests/Bridge/Azure/OpenAi/EmbeddingsModelClientTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use PHPUnit\Framework\Attributes\TestWith;
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\AI\Platform\Bridge\Azure\OpenAi\EmbeddingsModelClient;
17-
use Symfony\AI\Platform\Bridge\OpenAi\Embeddings;
17+
use Symfony\AI\Platform\Bridge\Generic\EmbeddingsModel;
1818
use Symfony\AI\Platform\Exception\InvalidArgumentException;
1919
use Symfony\Component\HttpClient\MockHttpClient;
2020
use Symfony\Component\HttpClient\Response\MockResponse;
@@ -71,7 +71,7 @@ public function testItIsSupportingTheCorrectModel()
7171
{
7272
$client = new EmbeddingsModelClient(new MockHttpClient(), 'test.azure.com', 'deployment', '2023-12-01', 'api-key');
7373

74-
$this->assertTrue($client->supports(new Embeddings('text-embedding-3-small')));
74+
$this->assertTrue($client->supports(new EmbeddingsModel('text-embedding-3-small')));
7575
}
7676

7777
public function testItIsExecutingTheCorrectRequest()
@@ -87,6 +87,6 @@ public function testItIsExecutingTheCorrectRequest()
8787

8888
$httpClient = new MockHttpClient([$resultCallback]);
8989
$client = new EmbeddingsModelClient($httpClient, 'test.azure.com', 'embeddings-deployment', '2023-12-01', 'test-api-key');
90-
$client->request(new Embeddings('text-embedding-3-small'), 'Hello, world!');
90+
$client->request(new EmbeddingsModel('text-embedding-3-small'), 'Hello, world!');
9191
}
9292
}

0 commit comments

Comments
 (0)