@@ -49,7 +49,8 @@ array of options::
49
49
50
50
The structure of the input message bag is flexible, see `Platform Component `_ for more details on how to use it.
51
51
52
- **Options **
52
+ Options
53
+ ~~~~~~~
53
54
54
55
As with the Platform component, you can pass options to the agent when running it. These options configure the agent's
55
56
behavior, for example available tools to execute, or are forwarded to the underlying platform and model.
@@ -88,12 +89,14 @@ Custom tools can basically be any class, but must configure by the ``#[AsTool]``
88
89
}
89
90
}
90
91
91
- **Tool Return Value **
92
+ Tool Return Value
93
+ ~~~~~~~~~~~~~~~~~
92
94
93
95
In the end, the tool's result needs to be a string, but Symfony AI converts arrays and objects, that implement the
94
96
JsonSerializable interface, to JSON strings for you. So you can return arrays or objects directly from your tool.
95
97
96
- **Tool Methods **
98
+ Tool Methods
99
+ ~~~~~~~~~~~~
97
100
98
101
You can configure the method to be called by the LLM with the #[AsTool] attribute and have multiple tools per class::
99
102
@@ -122,13 +125,15 @@ You can configure the method to be called by the LLM with the #[AsTool] attribut
122
125
}
123
126
}
124
127
125
- **Tool Parameters **
128
+ Tool Parameters
129
+ ~~~~~~~~~~~~~~~
126
130
127
131
Symfony AI generates a JSON Schema representation for all tools in the Toolbox based on the #[AsTool] attribute and
128
132
method arguments and param comments in the doc block. Additionally, JSON Schema support validation rules, which are
129
133
partially support by LLMs like GPT.
130
134
131
- **Parameter Validation with #[With] Attribute **
135
+ Parameter Validation with #[With] Attribute
136
+ ...........................................
132
137
133
138
To leverage JSON Schema validation rules, configure the ``#[With] `` attribute on the method arguments of your tool::
134
139
@@ -157,7 +162,8 @@ To leverage JSON Schema validation rules, configure the ``#[With]`` attribute on
157
162
158
163
See attribute class ``Symfony\AI\Platform\Contract\JsonSchema\Attribute\With `` for all available options.
159
164
160
- **Automatic Enum Validation **
165
+ Automatic Enum Validation
166
+ .........................
161
167
162
168
For PHP backed enums, Symfony AI provides automatic validation without requiring any ``#[With] `` attributes::
163
169
@@ -201,7 +207,8 @@ This eliminates the need for manual ``#[With(enum: [...])]`` attributes when usi
201
207
202
208
Please be aware, that this is only converted in a JSON Schema for the LLM to respect, but not validated by Symfony AI.
203
209
204
- **Third-Party Tools **
210
+ Third-Party Tools
211
+ ~~~~~~~~~~~~~~~~~
205
212
206
213
In some cases you might want to use third-party tools, which are not part of your application. Adding the ``#[AsTool] ``
207
214
attribute to the class is not possible in those cases, but you can explicitly register the tool in the MemoryFactory::
@@ -235,7 +242,8 @@ tools in the same chain - which even enables you to overwrite the pre-existing c
235
242
236
243
The order of the factories in the ChainFactory matters, as the first factory has the highest priority.
237
244
238
- **Agent uses Agent 🤯 **
245
+ Agent uses Agent 🤯
246
+ ~~~~~~~~~~~~~~~~~~
239
247
240
248
Similar to third-party tools, an agent can also use an different agent as a tool. This can be useful to encapsulate
241
249
complex logic or to reuse an agent in multiple places or hide sub-agents from the LLM::
@@ -251,7 +259,8 @@ complex logic or to reuse an agent in multiple places or hide sub-agents from th
251
259
->addTool($agentTool, 'research_agent', 'Meaningful description for sub-agent');
252
260
$toolbox = new Toolbox($metadataFactory, [$agentTool]);
253
261
254
- **Fault Tolerance **
262
+ Fault Tolerance
263
+ ~~~~~~~~~~~~~~~
255
264
256
265
To gracefully handle errors that occur during tool calling, e.g. wrong tool names or runtime errors, you can use the
257
266
``FaultTolerantToolbox `` as a decorator for the Toolbox. It will catch the exceptions and return readable error messages
@@ -299,14 +308,16 @@ If you want to expose the underlying error to the LLM, you can throw a custom ex
299
308
}
300
309
}
301
310
302
- **Tool Filtering **
311
+ Tool Filtering
312
+ ~~~~~~~~~~~~~~
303
313
304
314
To limit the tools provided to the LLM in a specific agent call to a subset of the configured tools, you can use the
305
315
tools option with a list of tool names::
306
316
307
317
$this->agent->call($messages, ['tools' => ['tavily_search']]);
308
318
309
- **Tool Result Interception **
319
+ Tool Result Interception
320
+ ~~~~~~~~~~~~~~~~~~~~~~~~
310
321
311
322
To react to the result of a tool, you can implement an EventListener or EventSubscriber, that listens to the
312
323
``ToolCallsExecuted `` event. This event is dispatched after the Toolbox executed all current tool calls and enables you
@@ -320,7 +331,8 @@ to skip the next LLM call by setting a result yourself::
320
331
}
321
332
});
322
333
323
- **Keeping Tool Messages **
334
+ Keeping Tool Messages
335
+ ~~~~~~~~~~~~~~~~~~~~~
324
336
325
337
Sometimes you might wish to keep the tool messages (AssistantMessage containing the toolCalls and ToolCallMessage
326
338
containing the result) in the context. Enable the keepToolMessages flag of the toolbox' AgentProcessor to ensure those
@@ -347,7 +359,8 @@ messages will be added to your MessageBag::
347
359
$result = $agent->call($messages);
348
360
// $messages will now include the tool messages
349
361
350
- **Code Examples (with built-in tools) **
362
+ Code Examples (with built-in tools)
363
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
351
364
352
365
* `Brave Tool `_
353
366
* `Clock Tool `_
@@ -391,7 +404,8 @@ more accurate and context-aware results. Therefore, the component provides a bui
391
404
);
392
405
$result = $agent->call($messages);
393
406
394
- **Code Examples **
407
+ Code Examples
408
+ ~~~~~~~~~~~~~
395
409
396
410
* `RAG with MongoDB `_
397
411
* `RAG with Pinecone `_
@@ -400,9 +414,10 @@ Structured Output
400
414
-----------------
401
415
402
416
A typical use-case of LLMs is to classify and extract data from unstructured sources, which is supported by some models
403
- by features like ** Structured Output ** or providing a ** Response Format ** .
417
+ by features like Structured Output or providing a Response Format.
404
418
405
- **PHP Classes as Output **
419
+ PHP Classes as Output
420
+ ~~~~~~~~~~~~~~~~~~~~~
406
421
407
422
Symfony AI supports that use-case by abstracting the hustle of defining and providing schemas to the LLM and converting
408
423
the result back to PHP objects.
@@ -433,7 +448,8 @@ To achieve this, a specific agent processor needs to be registered::
433
448
434
449
dump($result->getContent()); // returns an instance of `MathReasoning` class
435
450
436
- **Array Structures as Output **
451
+ Array Structures as Output
452
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
437
453
438
454
Also PHP array structures as response_format are supported, which also requires the agent processor mentioned above::
439
455
@@ -462,7 +478,8 @@ Also PHP array structures as response_format are supported, which also requires
462
478
463
479
dump($result->getContent()); // returns an array
464
480
465
- **Code Examples **
481
+ Code Examples
482
+ ~~~~~~~~~~~~~
466
483
467
484
* `Structured Output with PHP class `_
468
485
* `Structured Output with array `_
@@ -479,7 +496,8 @@ They are provided while instantiating the agent instance::
479
496
480
497
$agent = new Agent($platform, $model, $inputProcessors, $outputProcessors);
481
498
482
- **InputProcessor **
499
+ InputProcessor
500
+ ~~~~~~~~~~~~~~
483
501
484
502
InputProcessor instances are called in the agent before handing over the MessageBag and the $options array to the LLM
485
503
and are able to mutate both on top of the Input instance provided::
@@ -502,7 +520,8 @@ and are able to mutate both on top of the Input instance provided::
502
520
}
503
521
}
504
522
505
- **OutputProcessor **
523
+ OutputProcessor
524
+ ~~~~~~~~~~~~~~~
506
525
507
526
OutputProcessor instances are called after the model provided a result and can - on top of options and messages - mutate
508
527
or replace the given result::
@@ -521,7 +540,8 @@ or replace the given result::
521
540
}
522
541
}
523
542
524
- **Agent Awareness **
543
+ Agent Awareness
544
+ ~~~~~~~~~~~~~~~
525
545
526
546
Both, Input and Output instances, provide access to the LLM used by the agent, but the agent itself is only provided,
527
547
in case the processor implemented the AgentAwareInterface interface, which can be combined with using the
@@ -551,7 +571,7 @@ relevant information from different sources. Memory providers inject information
551
571
model with context without changing your application logic.
552
572
553
573
Using Memory
554
- ~~~~~~~~~~~~
574
+ ^^^^^^^^^^^^
555
575
556
576
Memory integration is handled through the ``MemoryInputProcessor `` and one or more ``MemoryProviderInterface `` implementations::
557
577
@@ -575,11 +595,12 @@ Memory integration is handled through the ``MemoryInputProcessor`` and one or mo
575
595
$result = $agent->call($messages);
576
596
577
597
Memory Providers
578
- ~~~~~~~~~~~~~~~~
598
+ ^^^^^^^^^^^^^^^^
579
599
580
600
The library includes several memory provider implementations that are ready to use out of the box.
581
601
582
- **Static Memory **
602
+ Static Memory
603
+ .............
583
604
584
605
Static memory provides fixed information to the agent, such as user preferences, application context, or any other
585
606
information that should be consistently available without being directly added to the system prompt::
@@ -591,7 +612,8 @@ information that should be consistently available without being directly added t
591
612
'The user prefers brief explanations',
592
613
);
593
614
594
- **Embedding Provider **
615
+ Embedding Provider
616
+ ..................
595
617
596
618
This provider leverages vector storage to inject relevant knowledge based on the user's current message. It can be used
597
619
for retrieving general knowledge from a store or recalling past conversation pieces that might be relevant::
@@ -605,7 +627,7 @@ for retrieving general knowledge from a store or recalling past conversation pie
605
627
);
606
628
607
629
Dynamic Memory Control
608
- ~~~~~~~~~~~~~~~~~~~~~~
630
+ ^^^^^^^^^^^^^^^^^^^^^^
609
631
610
632
Memory is globally configured for the agent, but you can selectively disable it for specific calls when needed. This is
611
633
useful when certain interactions shouldn't be influenced by the memory context::
@@ -619,7 +641,7 @@ Testing
619
641
-------
620
642
621
643
MockAgent
622
- ~~~~~~~~~
644
+ ^^^^^^^^^
623
645
624
646
For testing purposes, the Agent component provides a ``MockAgent `` class that behaves like Symfony's ``MockHttpClient ``.
625
647
It provides predictable responses without making external API calls and includes assertion methods for verifying interactions::
@@ -652,7 +674,7 @@ Call Tracking and Assertions::
652
674
$agent->reset();
653
675
654
676
MockResponse Objects
655
- ~~~~~~~~~~~~~~~~~~~~
677
+ ^^^^^^^^^^^^^^^^^^^^
656
678
657
679
Similar to ``MockHttpClient ``, you can use ``MockResponse `` objects for more complex scenarios::
658
680
@@ -665,7 +687,7 @@ Similar to ``MockHttpClient``, you can use ``MockResponse`` objects for more com
665
687
]);
666
688
667
689
Callable Responses
668
- ~~~~~~~~~~~~~~~~~~
690
+ ^^^^^^^^^^^^^^^^^^
669
691
670
692
Like ``MockHttpClient ``, ``MockAgent `` supports callable responses for dynamic behavior::
671
693
@@ -684,7 +706,7 @@ Like ``MockHttpClient``, ``MockAgent`` supports callable responses for dynamic b
684
706
685
707
686
708
Service Testing Example
687
- ~~~~~~~~~~~~~~~~~~~~~~~
709
+ ^^^^^^^^^^^^^^^^^^^^^^^
688
710
689
711
Testing a service that uses an agent::
690
712
@@ -708,7 +730,8 @@ Testing a service that uses an agent::
708
730
The ``MockAgent `` provides all the benefits of traditional mocks while offering a more intuitive API for AI agent testing,
709
731
making your tests more reliable and easier to maintain.
710
732
711
- **Code Examples **
733
+ Code Examples
734
+ ~~~~~~~~~~~~~
712
735
713
736
* `Chat with static memory `_
714
737
* `Chat with embedding search memory `_
0 commit comments