Skip to content

Commit 1a8fa84

Browse files
committed
Add docs for message templates
1 parent 7538f9f commit 1a8fa84

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

docs/bundles/ai-bundle.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,14 @@ Then configure the prompt with translation enabled:
424424
The system prompt text will be automatically translated using the configured translator service.
425425
If no translation domain is specified, the default domain will be used.
426426

427+
Message Template Support
428+
~~~~~~~~~~~~~~~~~~~~~~~~
429+
430+
The Platform's feature for using message templates is set up by the bundle, and conditionally also registers the
431+
expression language support if the `symfony/expression-language` package is installed.
432+
433+
More about message templates can be found in the :doc:`Platform documentation <components/platform>`.
434+
427435
Memory Provider Configuration
428436
-----------------------------
429437

docs/components/platform.rst

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,89 @@ This provides several benefits:
229229
// Get string representation
230230
echo $id->toRfc4122(); // e.g., "01928d1f-6f2e-7123-a456-123456789abc"
231231

232+
Message Templates
233+
~~~~~~~~~~~~~~~~~
234+
235+
Message templates allow dynamic variable substitution in messages. Both system and user messages support templates, enabling reusable message patterns with runtime variables.
236+
237+
String Templates
238+
................
239+
240+
String templates use curly braces for variable placeholders::
241+
242+
use Symfony\AI\Platform\Message\Message;
243+
use Symfony\AI\Platform\Message\MessageBag;
244+
use Symfony\AI\Platform\Message\Template;
245+
246+
// System message with template
247+
$messages = new MessageBag(
248+
Message::forSystem(Template::string('You are a {role} assistant.')),
249+
Message::ofUser('What is PHP?')
250+
);
251+
252+
$result = $platform->invoke('gpt-4o-mini', $messages, [
253+
'template_vars' => ['role' => 'programming'],
254+
]);
255+
256+
User messages also support templates::
257+
258+
$messages = new MessageBag(
259+
Message::forSystem('You are a helpful assistant.'),
260+
Message::ofUser(Template::string('Tell me about {topic}'))
261+
);
262+
263+
$result = $platform->invoke('gpt-4o-mini', $messages, [
264+
'template_vars' => ['topic' => 'PHP'],
265+
]);
266+
267+
Multiple messages can use the same variable set::
268+
269+
$messages = new MessageBag(
270+
Message::forSystem(Template::string('You are a {domain} assistant.')),
271+
Message::ofUser(Template::string('Calculate {operation}'))
272+
);
273+
274+
$result = $platform->invoke('gpt-4o-mini', $messages, [
275+
'template_vars' => [
276+
'domain' => 'math',
277+
'operation' => '2 + 2',
278+
],
279+
]);
280+
281+
Expression Templates
282+
....................
283+
284+
For advanced use cases, expression templates provide dynamic evaluation using Symfony's Expression Language::
285+
286+
$template = Template::expression('price * quantity');
287+
288+
.. note::
289+
290+
Expression templates require the ``symfony/expression-language`` component to be installed.
291+
292+
Setup
293+
.....
294+
295+
To use templates, register the ``TemplateRendererListener`` with your platform's event dispatcher::
296+
297+
use Symfony\AI\Platform\EventListener\TemplateRendererListener;
298+
use Symfony\AI\Platform\Message\TemplateRenderer\StringTemplateRenderer;
299+
use Symfony\AI\Platform\Message\TemplateRenderer\TemplateRendererRegistry;
300+
use Symfony\Component\EventDispatcher\EventDispatcher;
301+
302+
$eventDispatcher = new EventDispatcher();
303+
$rendererRegistry = new TemplateRendererRegistry([
304+
new StringTemplateRenderer(),
305+
]);
306+
$templateListener = new TemplateRendererListener($rendererRegistry);
307+
$eventDispatcher->addSubscriber($templateListener);
308+
309+
$platform = PlatformFactory::create($apiKey, eventDispatcher: $eventDispatcher);
310+
311+
.. note::
312+
313+
When using the AI Bundle, template rendering is automatically configured and available without manual setup.
314+
232315
Result Streaming
233316
----------------
234317

0 commit comments

Comments
 (0)