Skip to content

refactor!: improve types in platform api #73

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

Merged
merged 1 commit into from
Jul 7, 2025
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
2 changes: 1 addition & 1 deletion examples/azure/audio-transcript.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@

$response = $platform->request($model, $file);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
5 changes: 1 addition & 4 deletions examples/azure/embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Symfony\AI\Platform\Bridge\Azure\OpenAI\PlatformFactory;
use Symfony\AI\Platform\Bridge\OpenAI\Embeddings;
use Symfony\AI\Platform\Response\VectorResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__).'/vendor/autoload.php';
Expand All @@ -37,6 +36,4 @@
country was very peaceful and prosperous. The people lived happily ever after.
TEXT);

assert($response instanceof VectorResponse);

echo 'Dimensions: '.$response->getContent()[0]->getDimensions().\PHP_EOL;
echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/huggingface/audio-classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
'task' => Task::AUDIO_CLASSIFICATION,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/automatic-speech-recognition.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
'task' => Task::AUTOMATIC_SPEECH_RECOGNITION,
]);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/huggingface/chat-completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
'task' => Task::CHAT_COMPLETION,
]);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
5 changes: 1 addition & 4 deletions examples/huggingface/feature-extraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Symfony\AI\Platform\Bridge\HuggingFace\PlatformFactory;
use Symfony\AI\Platform\Bridge\HuggingFace\Task;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\Response\VectorResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__).'/vendor/autoload.php';
Expand All @@ -30,6 +29,4 @@
'task' => Task::FEATURE_EXTRACTION,
]);

assert($response instanceof VectorResponse);

echo 'Dimensions: '.$response->getContent()[0]->getDimensions().\PHP_EOL;
echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/huggingface/fill-mask.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
'task' => Task::FILL_MASK,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/image-classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
'task' => Task::IMAGE_CLASSIFICATION,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/image-segmentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
'task' => Task::IMAGE_SEGMENTATION,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/image-to-text.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
'task' => Task::IMAGE_TO_TEXT,
]);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/huggingface/object-detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
'task' => Task::OBJECT_DETECTION,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/question-answering.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
'task' => Task::QUESTION_ANSWERING,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/sentence-similarity.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@
'task' => Task::SENTENCE_SIMILARITY,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/summarization.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
'task' => Task::SUMMARIZATION,
]);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/huggingface/table-question-answering.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@
'task' => Task::TABLE_QUESTION_ANSWERING,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/text-classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
'task' => Task::TEXT_CLASSIFICATION,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/text-generation.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
'task' => Task::TEXT_GENERATION,
]);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
5 changes: 1 addition & 4 deletions examples/huggingface/text-to-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Symfony\AI\Platform\Bridge\HuggingFace\PlatformFactory;
use Symfony\AI\Platform\Bridge\HuggingFace\Task;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\Response\BinaryResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__).'/vendor/autoload.php';
Expand All @@ -30,6 +29,4 @@
'task' => Task::TEXT_TO_IMAGE,
]);

assert($response instanceof BinaryResponse);

echo $response->toBase64().\PHP_EOL;
echo $response->asBase64().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/huggingface/token-classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
'task' => Task::TOKEN_CLASSIFICATION,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
'tgt_lang' => 'en',
]);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/huggingface/zero-shot-classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
'candidate_labels' => ['refund', 'legal', 'faq'],
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/misc/parallel-chat-gpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@

echo 'Waiting for the responses ...'.\PHP_EOL;
foreach ($responses as $response) {
echo 'Next Letter: '.$response->getContent().\PHP_EOL;
echo 'Next Letter: '.$response->asText().\PHP_EOL;
}
4 changes: 1 addition & 3 deletions examples/misc/parallel-embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Symfony\AI\Platform\Bridge\OpenAI\Embeddings;
use Symfony\AI\Platform\Bridge\OpenAI\PlatformFactory;
use Symfony\AI\Platform\Response\VectorResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__).'/vendor/autoload.php';
Expand All @@ -36,6 +35,5 @@

echo 'Waiting for the responses ...'.\PHP_EOL;
foreach ($responses as $response) {
assert($response instanceof VectorResponse);
echo 'Dimensions: '.$response->getContent()[0]->getDimensions().\PHP_EOL;
echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
}
5 changes: 1 addition & 4 deletions examples/mistral/embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Symfony\AI\Platform\Bridge\Mistral\Embeddings;
use Symfony\AI\Platform\Bridge\Mistral\PlatformFactory;
use Symfony\AI\Platform\Response\VectorResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__).'/vendor/autoload.php';
Expand All @@ -32,6 +31,4 @@
salt. The goal was to prevent deficiencies and promote better health in the population.
TEXT);

assert($response instanceof VectorResponse);

echo 'Dimensions: '.$response->getContent()[0]->getDimensions().\PHP_EOL;
echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/openai/audio-transcript.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@

$response = $platform->request($model, $file);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
5 changes: 1 addition & 4 deletions examples/openai/embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Symfony\AI\Platform\Bridge\OpenAI\Embeddings;
use Symfony\AI\Platform\Bridge\OpenAI\PlatformFactory;
use Symfony\AI\Platform\Response\VectorResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__).'/vendor/autoload.php';
Expand All @@ -31,6 +30,4 @@
country was very peaceful and prosperous. The people lived happily ever after.
TEXT);

assert($response instanceof VectorResponse);

echo 'Dimensions: '.$response->getContent()[0]->getDimensions().\PHP_EOL;
echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/openai/image-output-dall-e-2.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
],
);

foreach ($response->getContent() as $index => $image) {
foreach ($response->getResponse()->getContent() as $index => $image) {
echo 'Image '.$index.': '.$image->url.\PHP_EOL;
}
2 changes: 1 addition & 1 deletion examples/openai/image-output-dall-e-3.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
options: [
'response_format' => 'url', // Generate response as URL
],
);
)->getResponse();

assert($response instanceof ImageResponse);

Expand Down
2 changes: 1 addition & 1 deletion examples/transformers/text-generation.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
'task' => Task::Text2TextGeneration,
]);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
5 changes: 1 addition & 4 deletions examples/voyage/embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Symfony\AI\Platform\Bridge\Voyage\PlatformFactory;
use Symfony\AI\Platform\Bridge\Voyage\Voyage;
use Symfony\AI\Platform\Response\VectorResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__).'/vendor/autoload.php';
Expand All @@ -31,6 +30,4 @@
country was very peaceful and prosperous. The people lived happily ever after.
TEXT);

assert($response instanceof VectorResponse);

echo 'Dimensions: '.$response->getContent()[0]->getDimensions().\PHP_EOL;
echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
7 changes: 1 addition & 6 deletions src/agent/src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Symfony\AI\Platform\Message\MessageBagInterface;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\PlatformInterface;
use Symfony\AI\Platform\Response\AsyncResponse;
use Symfony\AI\Platform\Response\ResponseInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
Expand Down Expand Up @@ -76,11 +75,7 @@ public function call(MessageBagInterface $messages, array $options = []): Respon
}

try {
$response = $this->platform->request($model, $messages, $options);

if ($response instanceof AsyncResponse) {
$response = $response->unwrap();
}
$response = $this->platform->request($model, $messages, $options)->getResponse();
} catch (ClientExceptionInterface $e) {
$message = $e->getMessage();
$content = $e->getResponse()->toArray(false);
Expand Down
4 changes: 1 addition & 3 deletions src/agent/src/Toolbox/Tool/SimilaritySearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\AI\Agent\Toolbox\Attribute\AsTool;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\PlatformInterface;
use Symfony\AI\Platform\Vector\Vector;
use Symfony\AI\Store\Document\VectorDocument;
use Symfony\AI\Store\VectorStoreInterface;

Expand All @@ -41,8 +40,7 @@ public function __construct(
*/
public function __invoke(string $searchTerm): string
{
/** @var Vector[] $vectors */
$vectors = $this->platform->request($this->model, $searchTerm)->getContent();
$vectors = $this->platform->request($this->model, $searchTerm)->asVectors();
$this->usedDocuments = $this->vectorStore->query($vectors[0]);

if (0 === \count($this->usedDocuments)) {
Expand Down
8 changes: 4 additions & 4 deletions src/ai-bundle/src/Profiler/TraceablePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Symfony\AI\Platform\Message\Content\File;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\PlatformInterface;
use Symfony\AI\Platform\Response\ResponseInterface;
use Symfony\AI\Platform\Response\ResponsePromise;

/**
* @author Christopher Hertel <[email protected]>
Expand All @@ -23,7 +23,7 @@
* model: Model,
* input: array<mixed>|string|object,
* options: array<string, mixed>,
* response: ResponseInterface,
* response: ResponsePromise,
* }
*/
final class TraceablePlatform implements PlatformInterface
Expand All @@ -38,7 +38,7 @@ public function __construct(
) {
}

public function request(Model $model, array|string|object $input, array $options = []): ResponseInterface
public function request(Model $model, array|string|object $input, array $options = []): ResponsePromise
{
$response = $this->platform->request($model, $input, $options);

Expand All @@ -50,7 +50,7 @@ public function request(Model $model, array|string|object $input, array $options
'model' => $model,
'input' => \is_object($input) ? clone $input : $input,
'options' => $options,
'response' => $response->getContent(),
'response' => $response,
];

return $response;
Expand Down
4 changes: 2 additions & 2 deletions src/platform/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ The standalone usage results in an ``Vector`` instance::

$embeddings = new Embeddings($platform, Embeddings::TEXT_3_SMALL);

$vectors = $platform->request($embeddings, $textInput)->getContent();
$vectors = $platform->request($embeddings, $textInput)->asVectors();

dump($vectors[0]->getData()); // returns something like: [0.123, -0.456, 0.789, ...]

Expand Down Expand Up @@ -276,7 +276,7 @@ which can be useful to speed up the processing::
}

foreach ($responses as $response) {
echo $response->getContent().PHP_EOL;
echo $response->asText().PHP_EOL;
}

.. note::
Expand Down
9 changes: 3 additions & 6 deletions src/platform/src/Bridge/Bedrock/Anthropic/ClaudeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Symfony\AI\Platform\Bridge\Bedrock\BedrockModelClient;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\Response\ResponseInterface as LlmResponse;
use Symfony\AI\Platform\Response\TextResponse;
use Symfony\AI\Platform\Response\ToolCall;
use Symfony\AI\Platform\Response\ToolCallResponse;
Expand All @@ -39,7 +38,7 @@ public function supports(Model $model): bool
return $model instanceof Claude;
}

public function request(Model $model, array|string $payload, array $options = []): LlmResponse
public function request(Model $model, array|string $payload, array $options = []): InvokeModelResponse
{
unset($payload['model']);

Expand All @@ -57,12 +56,10 @@ public function request(Model $model, array|string $payload, array $options = []
'body' => json_encode(array_merge($options, $payload), \JSON_THROW_ON_ERROR),
];

$invokeModelResponse = $this->bedrockRuntimeClient->invokeModel(new InvokeModelRequest($request));

return $this->convert($invokeModelResponse);
return $this->bedrockRuntimeClient->invokeModel(new InvokeModelRequest($request));
}

public function convert(InvokeModelResponse $bedrockResponse): LlmResponse
public function convert(InvokeModelResponse $bedrockResponse): ToolCallResponse|TextResponse
{
$data = json_decode($bedrockResponse->getBody(), true, 512, \JSON_THROW_ON_ERROR);

Expand Down
Loading