@@ -483,80 +483,6 @@ Code Examples
483483* `RAG with MongoDB `_
484484* `RAG with Pinecone `_
485485
486- Structured Output
487- -----------------
488-
489- A typical use-case of LLMs is to classify and extract data from unstructured sources, which is supported by some models
490- by features like Structured Output or providing a Response Format.
491-
492- PHP Classes as Output
493- ~~~~~~~~~~~~~~~~~~~~~
494-
495- Symfony AI supports that use-case by abstracting the hustle of defining and providing schemas to the LLM and converting
496- the result back to PHP objects.
497-
498- To achieve this, a specific agent processor needs to be registered::
499-
500- use Symfony\AI\Agent\Agent;
501- use Symfony\AI\Agent\StructuredOutput\AgentProcessor;
502- use Symfony\AI\Agent\StructuredOutput\ResponseFormatFactory;
503- use Symfony\AI\Fixtures\StructuredOutput\MathReasoning;
504- use Symfony\AI\Platform\Message\Message;
505- use Symfony\AI\Platform\Message\MessageBag;
506- use Symfony\Component\Serializer\Encoder\JsonEncoder;
507- use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
508- use Symfony\Component\Serializer\Serializer;
509-
510- // Initialize Platform and LLM
511-
512- $serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
513- $processor = new AgentProcessor(new ResponseFormatFactory(), $serializer);
514- $agent = new Agent($platform, $model, [$processor], [$processor]);
515-
516- $messages = new MessageBag(
517- Message::forSystem('You are a helpful math tutor. Guide the user through the solution step by step.'),
518- Message::ofUser('how can I solve 8x + 7 = -23'),
519- );
520- $result = $agent->call($messages, ['output_structure' => MathReasoning::class]);
521-
522- dump($result->getContent()); // returns an instance of `MathReasoning` class
523-
524- Array Structures as Output
525- ~~~~~~~~~~~~~~~~~~~~~~~~~~
526-
527- Also PHP array structures as response_format are supported, which also requires the agent processor mentioned above::
528-
529- use Symfony\AI\Platform\Message\Message;
530- use Symfony\AI\Platform\Message\MessageBag;
531-
532- // Initialize Platform, LLM and agent with processors and Clock tool
533-
534- $messages = new MessageBag(Message::ofUser('What date and time is it?'));
535- $result = $agent->call($messages, ['response_format' => [
536- 'type' => 'json_schema',
537- 'json_schema' => [
538- 'name' => 'clock',
539- 'strict' => true,
540- 'schema' => [
541- 'type' => 'object',
542- 'properties' => [
543- 'date' => ['type' => 'string', 'description' => 'The current date in the format YYYY-MM-DD.'],
544- 'time' => ['type' => 'string', 'description' => 'The current time in the format HH:MM:SS.'],
545- ],
546- 'required' => ['date', 'time'],
547- 'additionalProperties' => false,
548- ],
549- ],
550- ]]);
551-
552- dump($result->getContent()); // returns an array
553-
554- Code Examples
555- ~~~~~~~~~~~~~
556-
557- * `Structured Output with PHP class `_
558- * `Structured Output with array `_
559-
560486Input & Output Processing
561487-------------------------
562488
@@ -825,7 +751,5 @@ Code Examples
825751.. _`Store Component` : https://github.com/symfony/ai-store
826752.. _`RAG with MongoDB` : https://github.com/symfony/ai/blob/main/examples/rag/mongodb.php
827753.. _`RAG with Pinecone` : https://github.com/symfony/ai/blob/main/examples/rag/pinecone.php
828- .. _`Structured Output with PHP class` : https://github.com/symfony/ai/blob/main/examples/openai/structured-output-math.php
829- .. _`Structured Output with array` : https://github.com/symfony/ai/blob/main/examples/openai/structured-output-clock.php
830754.. _`Chat with static memory` : https://github.com/symfony/ai/blob/main/examples/memory/static.php
831755.. _`Chat with embedding search memory` : https://github.com/symfony/ai/blob/main/examples/memory/mariadb.php
0 commit comments