-
-
Notifications
You must be signed in to change notification settings - Fork 172
[Platform] Add support for object instances in structured output #1550
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
Open
wachterjohannes
wants to merge
3
commits into
symfony:main
Choose a base branch
from
wachterjohannes:feature/structured-output-object-instances
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -489,11 +489,85 @@ top this example uses the feature through the agent to leverage tool calling:: | |
|
|
||
| dump($result->getContent()); // returns an array | ||
|
|
||
| Populating Existing Objects | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| Instead of creating new object instances, you can pass existing object instances to have the AI populate missing data. | ||
| This is useful when you have partially filled objects and want the AI to complete them:: | ||
|
|
||
| use Symfony\AI\Platform\Message\Message; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
|
|
||
| // Create a partially populated object | ||
| class City | ||
| { | ||
| public function __construct( | ||
| public ?string $name = null, | ||
| public ?int $population = null, | ||
| public ?string $country = null, | ||
| public ?string $mayor = null, | ||
| ) {} | ||
| } | ||
|
|
||
| $city = new City(name: 'Berlin'); | ||
|
|
||
| // Pass the object instance to response_format | ||
| $messages = new MessageBag( | ||
| Message::forSystem('You are a helpful assistant that provides information about cities.'), | ||
| Message::ofUser('Please provide the population, country, and current mayor for Berlin.'), | ||
| ); | ||
|
|
||
| $result = $platform->invoke('gpt-4o-mini', $messages, [ | ||
| 'response_format' => $city, // Pass the instance instead of the class | ||
| ]); | ||
|
|
||
| // The same object instance is returned with populated fields | ||
| $populatedCity = $result->asObject(); | ||
| assert($city === $populatedCity); // Same object! | ||
|
|
||
| echo $city->population; // 3500000 | ||
| echo $city->country; // Germany | ||
| echo $city->mayor; // Kai Wegner | ||
|
|
||
| The AI will populate the missing fields while preserving any existing values. This is particularly useful for: | ||
|
|
||
| - Enriching partial data from databases | ||
| - Updating incomplete records | ||
| - Progressive data collection workflows | ||
|
Comment on lines
+542
to
+546
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i feel stuff like that would rather belong in a cookbook article |
||
|
|
||
| Object Context in Messages | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| You can also pass objects directly to user messages for automatic serialization as context. | ||
| The object will be JSON-serialized and included in the message content:: | ||
|
|
||
| use Symfony\AI\Platform\Message\Message; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
|
|
||
| $city = new City(name: 'Berlin'); // Partial object | ||
|
|
||
| // Pass object as last parameter to Message::ofUser() | ||
| $messages = new MessageBag( | ||
| Message::forSystem('You are a helpful assistant that provides information about cities.'), | ||
| Message::ofUser('Research missing data for this city', $city), // Object as context | ||
wachterjohannes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| $result = $platform->invoke('gpt-4o-mini', $messages, [ | ||
wachterjohannes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 'response_format' => $city, // Populate the same object | ||
| ]); | ||
|
|
||
| // The AI sees the serialized object state and fills in missing data | ||
| $populatedCity = $result->asObject(); | ||
|
|
||
| The object is serialized to JSON and appended to the message, providing the AI with the current state of the object. | ||
| This makes it clear what data is already present and what needs to be filled. | ||
|
|
||
| Code Examples | ||
| ~~~~~~~~~~~~~ | ||
|
|
||
| * `Structured Output with PHP class`_ | ||
| * `Structured Output with array`_ | ||
| * `Populating existing objects`_ | ||
|
|
||
| Server Tools | ||
| ------------ | ||
|
|
@@ -748,6 +822,7 @@ Code Examples | |
| .. _`Embeddings with Mistral`: https://github.com/symfony/ai/blob/main/examples/mistral/embeddings.php | ||
| .. _`Structured Output with PHP class`: https://github.com/symfony/ai/blob/main/examples/openai/structured-output-math.php | ||
| .. _`Structured Output with array`: https://github.com/symfony/ai/blob/main/examples/openai/structured-output-clock.php | ||
| .. _`Populating existing objects`: https://github.com/symfony/ai/blob/main/examples/platform/structured-output-populate-object.php | ||
| .. _`Parallel GPT Calls`: https://github.com/symfony/ai/blob/main/examples/misc/parallel-chat-gpt.php | ||
| .. _`Parallel Embeddings Calls`: https://github.com/symfony/ai/blob/main/examples/misc/parallel-embeddings.php | ||
| .. _`LM Studio`: https://lmstudio.ai/ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory; | ||
| use Symfony\AI\Platform\Message\Message; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
| use Symfony\AI\Platform\StructuredOutput\PlatformSubscriber; | ||
| use Symfony\AI\Platform\Tests\Fixtures\StructuredOutput\City; | ||
| use Symfony\Component\EventDispatcher\EventDispatcher; | ||
|
|
||
| require_once dirname(__DIR__).'/bootstrap.php'; | ||
|
|
||
| $dispatcher = new EventDispatcher(); | ||
| $dispatcher->addSubscriber(new PlatformSubscriber()); | ||
|
|
||
| $platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client(), eventDispatcher: $dispatcher); | ||
|
|
||
| // Create a partially populated object with only the city name | ||
| $city = new City(name: 'Berlin'); | ||
|
|
||
| echo "Initial object state:\n"; | ||
| dump($city); | ||
|
|
||
| $messages = new MessageBag( | ||
| Message::forSystem('You are a helpful assistant that provides information about cities.'), | ||
| Message::ofUser('Please research the missing data attributes for this city', $city), | ||
| ); | ||
|
|
||
| $result = $platform->invoke('gpt-5-mini', $messages, [ | ||
| 'response_format' => $city, | ||
| ]); | ||
|
|
||
| echo "\nPopulated object state:\n"; | ||
| dump($result->asObject()); | ||
|
|
||
| // Verify that the same object instance was populated | ||
| echo "\nObject identity preserved: "; | ||
| dump($city === $result->asObject()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\AI\Platform\Tests\Fixtures\StructuredOutput; | ||
|
|
||
| final class City | ||
| { | ||
| public function __construct( | ||
| public ?string $name = null, | ||
| public ?int $population = null, | ||
| public ?string $country = null, | ||
| public ?string $mayor = null, | ||
| ) { | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.