Skip to content

Commit 1648643

Browse files
fix code review
1 parent 88f5d17 commit 1648643

File tree

15 files changed

+237
-151
lines changed

15 files changed

+237
-151
lines changed

examples/platform/message-templates.php

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
13+
use Symfony\AI\Platform\EventListener\TemplateRendererListener;
14+
use Symfony\AI\Platform\Message\Message;
15+
use Symfony\AI\Platform\Message\MessageBag;
16+
use Symfony\AI\Platform\Message\Template;
17+
use Symfony\AI\Platform\Message\TemplateRenderer\ChainTemplateRenderer;
18+
use Symfony\AI\Platform\Message\TemplateRenderer\StringTemplateRenderer;
19+
use Symfony\Component\EventDispatcher\EventDispatcher;
20+
21+
require_once dirname(__DIR__, 2).'/bootstrap.php';
22+
23+
$eventDispatcher = new EventDispatcher();
24+
$rendererRegistry = new ChainTemplateRenderer([
25+
new StringTemplateRenderer(),
26+
]);
27+
$templateListener = new TemplateRendererListener($rendererRegistry);
28+
$eventDispatcher->addSubscriber($templateListener);
29+
30+
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client(), eventDispatcher: $eventDispatcher);
31+
32+
echo "SystemMessage with template\n";
33+
echo "===========================\n\n";
34+
35+
$template = Template::string('You are a {domain} expert assistant.');
36+
$messages = new MessageBag(
37+
Message::forSystem($template),
38+
Message::ofUser('What is PHP?')
39+
);
40+
41+
$result = $platform->invoke('gpt-4o-mini', $messages, [
42+
'template_vars' => ['domain' => 'programming'],
43+
]);
44+
45+
echo "SystemMessage template: You are a {domain} expert assistant.\n";
46+
echo "Variables: ['domain' => 'programming']\n";
47+
echo 'Response: '.$result->asText()."\n";
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
13+
use Symfony\AI\Platform\EventListener\TemplateRendererListener;
14+
use Symfony\AI\Platform\Message\Message;
15+
use Symfony\AI\Platform\Message\MessageBag;
16+
use Symfony\AI\Platform\Message\Template;
17+
use Symfony\AI\Platform\Message\TemplateRenderer\ChainTemplateRenderer;
18+
use Symfony\AI\Platform\Message\TemplateRenderer\StringTemplateRenderer;
19+
use Symfony\Component\EventDispatcher\EventDispatcher;
20+
21+
require_once dirname(__DIR__, 2).'/bootstrap.php';
22+
23+
$eventDispatcher = new EventDispatcher();
24+
$rendererRegistry = new ChainTemplateRenderer([
25+
new StringTemplateRenderer(),
26+
]);
27+
$templateListener = new TemplateRendererListener($rendererRegistry);
28+
$eventDispatcher->addSubscriber($templateListener);
29+
30+
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client(), eventDispatcher: $eventDispatcher);
31+
32+
echo "UserMessage with template\n";
33+
echo "=========================\n\n";
34+
35+
$messages = new MessageBag(
36+
Message::forSystem('You are a helpful assistant.'),
37+
Message::ofUser(Template::string('Tell me about {topic}'))
38+
);
39+
40+
$result = $platform->invoke('gpt-4o-mini', $messages, [
41+
'template_vars' => ['topic' => 'PHP'],
42+
]);
43+
44+
echo "UserMessage template: Tell me about {topic}\n";
45+
echo "Variables: ['topic' => 'PHP']\n";
46+
echo 'Response: '.$result->asText()."\n";
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
13+
use Symfony\AI\Platform\EventListener\TemplateRendererListener;
14+
use Symfony\AI\Platform\Message\Message;
15+
use Symfony\AI\Platform\Message\MessageBag;
16+
use Symfony\AI\Platform\Message\Template;
17+
use Symfony\AI\Platform\Message\TemplateRenderer\ChainTemplateRenderer;
18+
use Symfony\AI\Platform\Message\TemplateRenderer\StringTemplateRenderer;
19+
use Symfony\Component\EventDispatcher\EventDispatcher;
20+
21+
require_once dirname(__DIR__, 2).'/bootstrap.php';
22+
23+
$eventDispatcher = new EventDispatcher();
24+
$rendererRegistry = new ChainTemplateRenderer([
25+
new StringTemplateRenderer(),
26+
]);
27+
$templateListener = new TemplateRendererListener($rendererRegistry);
28+
$eventDispatcher->addSubscriber($templateListener);
29+
30+
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client(), eventDispatcher: $eventDispatcher);
31+
32+
echo "Multiple messages with templates\n";
33+
echo "=================================\n\n";
34+
35+
$systemTemplate = Template::string('You are a {domain} assistant.');
36+
$userTemplate = Template::string('Calculate {operation}');
37+
38+
$messages = new MessageBag(
39+
Message::forSystem($systemTemplate),
40+
Message::ofUser($userTemplate)
41+
);
42+
43+
$result = $platform->invoke('gpt-4o-mini', $messages, [
44+
'template_vars' => [
45+
'domain' => 'math',
46+
'operation' => '2 + 2',
47+
],
48+
]);
49+
50+
echo "System template: You are a {domain} assistant.\n";
51+
echo "User template: Calculate {operation}\n";
52+
echo "Variables: ['domain' => 'math', 'operation' => '2 + 2']\n";
53+
echo 'Response: '.$result->asText()."\n";
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
13+
use Symfony\AI\Platform\EventListener\TemplateRendererListener;
14+
use Symfony\AI\Platform\Message\Message;
15+
use Symfony\AI\Platform\Message\MessageBag;
16+
use Symfony\AI\Platform\Message\Template;
17+
use Symfony\AI\Platform\Message\TemplateRenderer\ChainTemplateRenderer;
18+
use Symfony\AI\Platform\Message\TemplateRenderer\StringTemplateRenderer;
19+
use Symfony\Component\EventDispatcher\EventDispatcher;
20+
21+
require_once dirname(__DIR__, 2).'/bootstrap.php';
22+
23+
$eventDispatcher = new EventDispatcher();
24+
$rendererRegistry = new ChainTemplateRenderer([
25+
new StringTemplateRenderer(),
26+
]);
27+
$templateListener = new TemplateRendererListener($rendererRegistry);
28+
$eventDispatcher->addSubscriber($templateListener);
29+
30+
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client(), eventDispatcher: $eventDispatcher);
31+
32+
echo "UserMessage with mixed content\n";
33+
echo "==============================\n\n";
34+
35+
$messages = new MessageBag(
36+
Message::forSystem('You are a helpful assistant.'),
37+
Message::ofUser('I need help with', Template::string(' {task}'))
38+
);
39+
40+
$result = $platform->invoke('gpt-4o-mini', $messages, [
41+
'template_vars' => ['task' => 'debugging'],
42+
]);
43+
44+
echo "UserMessage: 'Plain text' + Template('{task}')\n";
45+
echo "Variables: ['task' => 'debugging']\n";
46+
echo 'Response: '.$result->asText()."\n";

src/ai-bundle/tests/DependencyInjection/AiBundleTest.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6876,15 +6876,11 @@ public function testTemplateRendererServicesAreRegistered()
68766876
$this->assertSame(StringTemplateRenderer::class, $stringRendererDefinition->getClass());
68776877
$this->assertTrue($stringRendererDefinition->hasTag('ai.platform.template_renderer'));
68786878

6879-
// Verify expression template renderer is registered only when ExpressionLanguage is available
6880-
if (class_exists(ExpressionLanguage::class)) {
6881-
$this->assertTrue($container->hasDefinition('ai.platform.template_renderer.expression'));
6882-
$expressionRendererDefinition = $container->getDefinition('ai.platform.template_renderer.expression');
6883-
$this->assertSame(ExpressionLanguageTemplateRenderer::class, $expressionRendererDefinition->getClass());
6884-
$this->assertTrue($expressionRendererDefinition->hasTag('ai.platform.template_renderer'));
6885-
} else {
6886-
$this->assertFalse($container->hasDefinition('ai.platform.template_renderer.expression'));
6887-
}
6879+
// Verify expression template renderer is registered
6880+
$this->assertTrue($container->hasDefinition('ai.platform.template_renderer.expression'));
6881+
$expressionRendererDefinition = $container->getDefinition('ai.platform.template_renderer.expression');
6882+
$this->assertSame(ExpressionLanguageTemplateRenderer::class, $expressionRendererDefinition->getClass());
6883+
$this->assertTrue($expressionRendererDefinition->hasTag('ai.platform.template_renderer'));
68886884

68896885
// Verify template renderer registry is registered
68906886
$this->assertTrue($container->hasDefinition('ai.platform.template_renderer_registry'));

src/platform/src/Message/AssistantMessage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class AssistantMessage implements MessageInterface
3030
* @param ?ToolCall[] $toolCalls
3131
*/
3232
public function __construct(
33-
private string|Template|null $content = null,
33+
private ?string $content = null,
3434
private ?array $toolCalls = null,
3535
) {
3636
$this->id = Uuid::v7();
@@ -59,7 +59,7 @@ public function getToolCalls(): ?array
5959
return $this->toolCalls;
6060
}
6161

62-
public function getContent(): string|Template|null
62+
public function getContent(): ?string
6363
{
6464
return $this->content;
6565
}

src/platform/src/Message/Message.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static function forSystem(\Stringable|string|Template $content): SystemMe
3838
/**
3939
* @param ?ToolCall[] $toolCalls
4040
*/
41-
public static function ofAssistant(string|Template|null $content = null, ?array $toolCalls = null): AssistantMessage
41+
public static function ofAssistant(?string $content = null, ?array $toolCalls = null): AssistantMessage
4242
{
4343
return new AssistantMessage($content, $toolCalls);
4444
}

src/platform/src/Message/TemplateRenderer/ExpressionLanguageTemplateRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* @author Johannes Wachter <[email protected]>
2020
*/
21-
final readonly class ExpressionLanguageTemplateRenderer implements TemplateRendererInterface
21+
final class ExpressionLanguageTemplateRenderer implements TemplateRendererInterface
2222
{
2323
private ExpressionLanguage $expressionLanguage;
2424

src/platform/src/Message/TemplateRenderer/StringTemplateRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* @author Johannes Wachter <[email protected]>
2424
*/
25-
final readonly class StringTemplateRenderer implements TemplateRendererInterface
25+
final class StringTemplateRenderer implements TemplateRendererInterface
2626
{
2727
public function supports(string $type): bool
2828
{

0 commit comments

Comments
 (0)