Skip to content

Commit 7580100

Browse files
committed
Add optional translation to system prompts
So that system prompts can be written in any locale
1 parent ff08204 commit 7580100

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

src/agent/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ CHANGELOG
5454
* Add model capability detection before processing
5555
* Add comprehensive type safety with full PHP type hints
5656
* Add clear exception hierarchy for different error scenarios
57+
* Add translation support for system prompts

src/agent/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"symfony/property-access": "^6.4 || ^7.1",
3131
"symfony/property-info": "^6.4 || ^7.1",
3232
"symfony/serializer": "^6.4 || ^7.1",
33+
"symfony/translation": "^7.3",
3334
"symfony/type-info": "^7.2.3"
3435
},
3536
"require-dev": {

src/agent/src/InputProcessor/SystemPromptInputProcessor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\AI\Agent\Toolbox\ToolboxInterface;
1919
use Symfony\AI\Platform\Message\Message;
2020
use Symfony\AI\Platform\Tool\Tool;
21+
use Symfony\Contracts\Translation\TranslatorInterface;
2122

2223
/**
2324
* @author Christopher Hertel <mail@christopher-hertel.de>
@@ -31,6 +32,7 @@
3132
public function __construct(
3233
private \Stringable|string $systemPrompt,
3334
private ?ToolboxInterface $toolbox = null,
35+
private ?TranslatorInterface $translator = null,
3436
private LoggerInterface $logger = new NullLogger(),
3537
) {
3638
}
@@ -45,7 +47,9 @@ public function processInput(Input $input): void
4547
return;
4648
}
4749

48-
$message = (string) $this->systemPrompt;
50+
$message = $this->translator
51+
? $this->translator->trans((string) $this->systemPrompt)
52+
: (string) $this->systemPrompt;
4953

5054
if ($this->toolbox instanceof ToolboxInterface
5155
&& [] !== $this->toolbox->getTools()

src/agent/tests/InputProcessor/SystemPromptInputProcessorTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\AI\Platform\Result\ToolCall;
3030
use Symfony\AI\Platform\Tool\ExecutionReference;
3131
use Symfony\AI\Platform\Tool\Tool;
32+
use Symfony\Contracts\Translation\TranslatorInterface;
3233

3334
#[CoversClass(SystemPromptInputProcessor::class)]
3435
#[UsesClass(Gpt::class)]
@@ -186,4 +187,37 @@ public function execute(ToolCall $toolCall): mixed
186187
A tool without parameters
187188
PROMPT, $messages[0]->content);
188189
}
190+
191+
public function testWithTranslatedSystemPrompt()
192+
{
193+
$processor = new SystemPromptInputProcessor(
194+
systemPrompt: 'This is a',
195+
toolbox: null,
196+
translator: $this->getTranslator()
197+
);
198+
199+
$input = new Input(new Gpt(), new MessageBag(Message::ofUser('This is a user message')), []);
200+
$processor->processInput($input);
201+
202+
$messages = $input->messages->getMessages();
203+
$this->assertCount(2, $messages);
204+
$this->assertInstanceOf(SystemMessage::class, $messages[0]);
205+
$this->assertInstanceOf(UserMessage::class, $messages[1]);
206+
$this->assertSame('This is a cool translated system prompt', $messages[0]->content);
207+
}
208+
209+
public function getTranslator(): TranslatorInterface
210+
{
211+
return new class implements TranslatorInterface {
212+
public function trans(string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
213+
{
214+
return \sprintf('%s cool translated system prompt', $id);
215+
}
216+
217+
public function getLocale(): string
218+
{
219+
return 'en';
220+
}
221+
};
222+
}
189223
}

0 commit comments

Comments
 (0)