Skip to content

Commit c0266db

Browse files
committed
minor #142 [Platform] Remove HTTP awareness from Platform (chr-hertel)
This PR was merged into the main branch. Discussion ---------- [Platform] Remove HTTP awareness from Platform | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | Issues | | License | MIT This PR decouples the main Platform implementation from HTTP details, enabling Bedrock and TransformersPHP to drop their own Platform implementation. I get that dealing with those RawXYZ objects is extra burden, but this is just one refactoring after each other - the goal was to decouple more from HTTP and reduce Platform implementations. **Key changes are in:** * HTTP Decoupling: * [src/platform/src/ModelClientInterface.php](https://github.com/symfony/ai/pull/142/files#diff-2fa6af72c0c856e895a407798442305cadab6f7f0d0fa0d84f5c26ff6faaccac) * [src/platform/src/Platform.php](https://github.com/symfony/ai/pull/142/files#diff-660e8336450572154d3c925521d0b91b8e98f1c6bba0d0ca547e163873549a36) * [src/platform/src/Response/ResponsePromise.php](https://github.com/symfony/ai/pull/142/files#diff-ee22066b5a7dfe6e5daccca990feafd609d2afc07412210a9d630e54b057b454) * [src/platform/src/ResponseConverterInterface.php](https://github.com/symfony/ai/pull/142/files#diff-2847909fadca63a47d4156f7aec298b9a67992d198922d1f174fa2b961d7abd4) * Additional Platform Removal * [src/platform/src/Bridge/Bedrock/PlatformFactory.php](https://github.com/symfony/ai/pull/142/files#diff-9d90025703ce4f7a3c795b186355435b8b0925de16859c0b4d52ab2cb88d5d51) * [src/platform/src/Bridge/TransformersPHP/PlatformFactory.php](https://github.com/symfony/ai/pull/142/files#diff-a6e435b3ff56bbcb596d7f241bfe2926467411d9b489a75573cb705e875c8079) Everything else is noise but impossible to avoid. Definitely up for discussing the naming of interfaces and classes involved here. feels a bit odd, but let's move the renaming to follow-up PR, and also please check #141 in that case. Commits ------- f912f43 Remove HTTP awareness from Platform
2 parents 395156d + f912f43 commit c0266db

File tree

69 files changed

+555
-498
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+555
-498
lines changed

examples/bedrock/chat-claude.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
}
2727

2828
$platform = PlatformFactory::create();
29-
$model = new Claude();
29+
$model = new Claude('claude-3-7-sonnet-20250219');
3030

3131
$agent = new Agent($platform, $model);
3232
$messages = new MessageBag(

examples/bedrock/image-claude-binary.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
}
2828

2929
$platform = PlatformFactory::create();
30-
$model = new Claude();
30+
$model = new Claude('claude-3-7-sonnet-20250219');
3131

3232
$agent = new Agent($platform, $model);
3333
$messages = new MessageBag(

examples/bedrock/toolcall-claude.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
}
3131

3232
$platform = PlatformFactory::create();
33-
$model = new Claude();
33+
$model = new Claude('claude-3-7-sonnet-20250219');
3434

3535
$wikipedia = new Wikipedia(HttpClient::create());
3636
$toolbox = Toolbox::create($wikipedia);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
use Symfony\AI\Platform\Exception\InvalidArgumentException;
1616
use Symfony\AI\Platform\Model;
1717
use Symfony\AI\Platform\ModelClientInterface;
18+
use Symfony\AI\Platform\Response\RawHttpResponse;
19+
use Symfony\AI\Platform\Response\RawResponseInterface;
1820
use Symfony\Contracts\HttpClient\HttpClientInterface;
19-
use Symfony\Contracts\HttpClient\ResponseInterface;
2021

2122
/**
2223
* @author Oskar Stark <[email protected]>
@@ -37,11 +38,11 @@ public function supports(Model $model): bool
3738
return $model instanceof Embeddings;
3839
}
3940

40-
public function request(Model $model, array|string $payload, array $options = []): ResponseInterface
41+
public function request(Model $model, array|string $payload, array $options = []): RawResponseInterface
4142
{
42-
return $this->httpClient->request('POST', \sprintf('%s/embeddings', $this->baseUrl), [
43+
return new RawHttpResponse($this->httpClient->request('POST', \sprintf('%s/embeddings', $this->baseUrl), [
4344
'auth_bearer' => $this->apiKey,
4445
'json' => \is_array($payload) ? array_merge($payload, $options) : $payload,
45-
]);
46+
]));
4647
}
4748
}

src/platform/src/Bridge/Albert/GPTModelClient.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
use Symfony\AI\Platform\Exception\InvalidArgumentException;
1616
use Symfony\AI\Platform\Model;
1717
use Symfony\AI\Platform\ModelClientInterface;
18+
use Symfony\AI\Platform\Response\RawHttpResponse;
19+
use Symfony\AI\Platform\Response\RawResponseInterface;
1820
use Symfony\Component\HttpClient\EventSourceHttpClient;
1921
use Symfony\Contracts\HttpClient\HttpClientInterface;
20-
use Symfony\Contracts\HttpClient\ResponseInterface;
2122

2223
/**
2324
* @author Oskar Stark <[email protected]>
@@ -42,11 +43,11 @@ public function supports(Model $model): bool
4243
return $model instanceof GPT;
4344
}
4445

45-
public function request(Model $model, array|string $payload, array $options = []): ResponseInterface
46+
public function request(Model $model, array|string $payload, array $options = []): RawResponseInterface
4647
{
47-
return $this->httpClient->request('POST', \sprintf('%s/chat/completions', $this->baseUrl), [
48+
return new RawHttpResponse($this->httpClient->request('POST', \sprintf('%s/chat/completions', $this->baseUrl), [
4849
'auth_bearer' => $this->apiKey,
4950
'json' => \is_array($payload) ? array_merge($payload, $options) : $payload,
50-
]);
51+
]));
5152
}
5253
}

src/platform/src/Bridge/Anthropic/ModelClient.php

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

1414
use Symfony\AI\Platform\Model;
1515
use Symfony\AI\Platform\ModelClientInterface;
16+
use Symfony\AI\Platform\Response\RawHttpResponse;
1617
use Symfony\Component\HttpClient\EventSourceHttpClient;
1718
use Symfony\Contracts\HttpClient\HttpClientInterface;
18-
use Symfony\Contracts\HttpClient\ResponseInterface;
1919

2020
/**
2121
* @author Christopher Hertel <[email protected]>
@@ -37,18 +37,18 @@ public function supports(Model $model): bool
3737
return $model instanceof Claude;
3838
}
3939

40-
public function request(Model $model, array|string $payload, array $options = []): ResponseInterface
40+
public function request(Model $model, array|string $payload, array $options = []): RawHttpResponse
4141
{
4242
if (isset($options['tools'])) {
4343
$options['tool_choice'] = ['type' => 'auto'];
4444
}
4545

46-
return $this->httpClient->request('POST', 'https://api.anthropic.com/v1/messages', [
46+
return new RawHttpResponse($this->httpClient->request('POST', 'https://api.anthropic.com/v1/messages', [
4747
'headers' => [
4848
'x-api-key' => $this->apiKey,
4949
'anthropic-version' => $this->version,
5050
],
5151
'json' => array_merge($options, $payload),
52-
]);
52+
]));
5353
}
5454
}

src/platform/src/Bridge/Anthropic/ResponseConverter.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
use Symfony\AI\Platform\Exception\RuntimeException;
1515
use Symfony\AI\Platform\Model;
16-
use Symfony\AI\Platform\Response\ResponseInterface as LlmResponse;
16+
use Symfony\AI\Platform\Response\RawHttpResponse;
17+
use Symfony\AI\Platform\Response\RawResponseInterface;
18+
use Symfony\AI\Platform\Response\ResponseInterface;
1719
use Symfony\AI\Platform\Response\StreamResponse;
1820
use Symfony\AI\Platform\Response\TextResponse;
1921
use Symfony\AI\Platform\Response\ToolCall;
@@ -22,7 +24,7 @@
2224
use Symfony\Component\HttpClient\Chunk\ServerSentEvent;
2325
use Symfony\Component\HttpClient\EventSourceHttpClient;
2426
use Symfony\Component\HttpClient\Exception\JsonException;
25-
use Symfony\Contracts\HttpClient\ResponseInterface;
27+
use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse;
2628

2729
/**
2830
* @author Christopher Hertel <[email protected]>
@@ -34,13 +36,13 @@ public function supports(Model $model): bool
3436
return $model instanceof Claude;
3537
}
3638

37-
public function convert(ResponseInterface $response, array $options = []): LlmResponse
39+
public function convert(RawHttpResponse|RawResponseInterface $response, array $options = []): ResponseInterface
3840
{
3941
if ($options['stream'] ?? false) {
40-
return new StreamResponse($this->convertStream($response));
42+
return new StreamResponse($this->convertStream($response->getRawObject()));
4143
}
4244

43-
$data = $response->toArray();
45+
$data = $response->getRawData();
4446

4547
if (!isset($data['content']) || [] === $data['content']) {
4648
throw new RuntimeException('Response does not contain any content');
@@ -64,7 +66,7 @@ public function convert(ResponseInterface $response, array $options = []): LlmRe
6466
return new TextResponse($data['content'][0]['text']);
6567
}
6668

67-
private function convertStream(ResponseInterface $response): \Generator
69+
private function convertStream(HttpResponse $response): \Generator
6870
{
6971
foreach ((new EventSourceHttpClient())->stream($response) as $chunk) {
7072
if (!$chunk instanceof ServerSentEvent || '[DONE]' === $chunk->getData()) {

src/platform/src/Bridge/Azure/Meta/LlamaModelClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use Symfony\AI\Platform\Bridge\Meta\Llama;
1515
use Symfony\AI\Platform\Model;
1616
use Symfony\AI\Platform\ModelClientInterface;
17+
use Symfony\AI\Platform\Response\RawHttpResponse;
1718
use Symfony\Contracts\HttpClient\HttpClientInterface;
18-
use Symfony\Contracts\HttpClient\ResponseInterface;
1919

2020
/**
2121
* @author Christopher Hertel <[email protected]>
@@ -34,16 +34,16 @@ public function supports(Model $model): bool
3434
return $model instanceof Llama;
3535
}
3636

37-
public function request(Model $model, array|string $payload, array $options = []): ResponseInterface
37+
public function request(Model $model, array|string $payload, array $options = []): RawHttpResponse
3838
{
3939
$url = \sprintf('https://%s/chat/completions', $this->baseUrl);
4040

41-
return $this->httpClient->request('POST', $url, [
41+
return new RawHttpResponse($this->httpClient->request('POST', $url, [
4242
'headers' => [
4343
'Content-Type' => 'application/json',
4444
'Authorization' => $this->apiKey,
4545
],
4646
'json' => array_merge($options, $payload),
47-
]);
47+
]));
4848
}
4949
}

src/platform/src/Bridge/Azure/Meta/LlamaResponseConverter.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
use Symfony\AI\Platform\Bridge\Meta\Llama;
1515
use Symfony\AI\Platform\Exception\RuntimeException;
1616
use Symfony\AI\Platform\Model;
17-
use Symfony\AI\Platform\Response\ResponseInterface as LlmResponse;
17+
use Symfony\AI\Platform\Response\RawResponseInterface;
1818
use Symfony\AI\Platform\Response\TextResponse;
1919
use Symfony\AI\Platform\ResponseConverterInterface;
20-
use Symfony\Contracts\HttpClient\ResponseInterface;
2120

2221
/**
2322
* @author Christopher Hertel <[email protected]>
@@ -29,9 +28,9 @@ public function supports(Model $model): bool
2928
return $model instanceof Llama;
3029
}
3130

32-
public function convert(ResponseInterface $response, array $options = []): LlmResponse
31+
public function convert(RawResponseInterface $response, array $options = []): TextResponse
3332
{
34-
$data = $response->toArray();
33+
$data = $response->getRawData();
3534

3635
if (!isset($data['choices'][0]['message']['content'])) {
3736
throw new RuntimeException('Response does not contain output');

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
use Symfony\AI\Platform\Exception\InvalidArgumentException;
1616
use Symfony\AI\Platform\Model;
1717
use Symfony\AI\Platform\ModelClientInterface;
18+
use Symfony\AI\Platform\Response\RawHttpResponse;
1819
use Symfony\Component\HttpClient\EventSourceHttpClient;
1920
use Symfony\Contracts\HttpClient\HttpClientInterface;
20-
use Symfony\Contracts\HttpClient\ResponseInterface;
2121

2222
/**
2323
* @author Christopher Hertel <[email protected]>
@@ -46,11 +46,11 @@ public function supports(Model $model): bool
4646
return $model instanceof Embeddings;
4747
}
4848

49-
public function request(Model $model, array|string $payload, array $options = []): ResponseInterface
49+
public function request(Model $model, array|string $payload, array $options = []): RawHttpResponse
5050
{
5151
$url = \sprintf('https://%s/openai/deployments/%s/embeddings', $this->baseUrl, $this->deployment);
5252

53-
return $this->httpClient->request('POST', $url, [
53+
return new RawHttpResponse($this->httpClient->request('POST', $url, [
5454
'headers' => [
5555
'api-key' => $this->apiKey,
5656
],
@@ -59,6 +59,6 @@ public function request(Model $model, array|string $payload, array $options = []
5959
'model' => $model->getName(),
6060
'input' => $payload,
6161
]),
62-
]);
62+
]));
6363
}
6464
}

0 commit comments

Comments
 (0)