Skip to content

Commit af174b9

Browse files
authored
feat: support stringable system prompt (#259)
1 parent 5b663ee commit af174b9

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

src/Chain/InputProcessor/SystemPromptInputProcessor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
final readonly class SystemPromptInputProcessor implements InputProcessor
1616
{
1717
/**
18-
* @param string $systemPrompt the system prompt to prepend to the input messages
18+
* @param \Stringable|string $systemPrompt the system prompt to prepend to the input messages
1919
* @param ToolBoxInterface|null $toolBox the tool box to be used to append the tool definitions to the system prompt
2020
*/
2121
public function __construct(
22-
private string $systemPrompt,
22+
private \Stringable|string $systemPrompt,
2323
private ?ToolBoxInterface $toolBox = null,
2424
private LoggerInterface $logger = new NullLogger(),
2525
) {
@@ -35,7 +35,7 @@ public function processInput(Input $input): void
3535
return;
3636
}
3737

38-
$message = $this->systemPrompt;
38+
$message = (string) $this->systemPrompt;
3939

4040
if ($this->toolBox instanceof ToolBoxInterface
4141
&& [] !== $this->toolBox->getMap()

tests/Chain/InputProcessor/SystemPromptInputProcessorTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,41 @@ public function execute(ToolCall $toolCall): mixed
147147
or not
148148
PROMPT, $messages[0]->content);
149149
}
150+
151+
#[Test]
152+
public function withStringableSystemPrompt(): void
153+
{
154+
$processor = new SystemPromptInputProcessor(
155+
new SystemPromptService(),
156+
new class implements ToolBoxInterface {
157+
public function getMap(): array
158+
{
159+
return [
160+
new Metadata(ToolNoParams::class, 'tool_no_params', 'A tool without parameters', '__invoke', null),
161+
];
162+
}
163+
164+
public function execute(ToolCall $toolCall): mixed
165+
{
166+
return null;
167+
}
168+
}
169+
);
170+
171+
$input = new Input(new GPT(), new MessageBag(Message::ofUser('This is a user message')), []);
172+
$processor->processInput($input);
173+
174+
$messages = $input->messages->getMessages();
175+
self::assertCount(2, $messages);
176+
self::assertInstanceOf(SystemMessage::class, $messages[0]);
177+
self::assertInstanceOf(UserMessage::class, $messages[1]);
178+
self::assertSame(<<<PROMPT
179+
My dynamic system prompt.
180+
181+
# Available tools
182+
183+
## tool_no_params
184+
A tool without parameters
185+
PROMPT, $messages[0]->content);
186+
}
150187
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\LlmChain\Tests\Chain\InputProcessor;
6+
7+
final class SystemPromptService implements \Stringable
8+
{
9+
public function __toString(): string
10+
{
11+
return 'My dynamic system prompt.';
12+
}
13+
}

0 commit comments

Comments
 (0)