Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

feat: support stringable system prompt #259

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Chain/InputProcessor/SystemPromptInputProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
final readonly class SystemPromptInputProcessor implements InputProcessor
{
/**
* @param string $systemPrompt the system prompt to prepend to the input messages
* @param \Stringable|string $systemPrompt the system prompt to prepend to the input messages
* @param ToolBoxInterface|null $toolBox the tool box to be used to append the tool definitions to the system prompt
*/
public function __construct(
private string $systemPrompt,
private \Stringable|string $systemPrompt,
private ?ToolBoxInterface $toolBox = null,
private LoggerInterface $logger = new NullLogger(),
) {
Expand All @@ -35,7 +35,7 @@ public function processInput(Input $input): void
return;
}

$message = $this->systemPrompt;
$message = (string) $this->systemPrompt;

if ($this->toolBox instanceof ToolBoxInterface
&& [] !== $this->toolBox->getMap()
Expand Down
37 changes: 37 additions & 0 deletions tests/Chain/InputProcessor/SystemPromptInputProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,41 @@ public function execute(ToolCall $toolCall): mixed
or not
PROMPT, $messages[0]->content);
}

#[Test]
public function withStringableSystemPrompt(): void
{
$processor = new SystemPromptInputProcessor(
new SystemPromptService(),
new class implements ToolBoxInterface {
public function getMap(): array
{
return [
new Metadata(ToolNoParams::class, 'tool_no_params', 'A tool without parameters', '__invoke', null),
];
}

public function execute(ToolCall $toolCall): mixed
{
return null;
}
}
);

$input = new Input(new GPT(), new MessageBag(Message::ofUser('This is a user message')), []);
$processor->processInput($input);

$messages = $input->messages->getMessages();
self::assertCount(2, $messages);
self::assertInstanceOf(SystemMessage::class, $messages[0]);
self::assertInstanceOf(UserMessage::class, $messages[1]);
self::assertSame(<<<PROMPT
My dynamic system prompt.

# Available tools

## tool_no_params
A tool without parameters
PROMPT, $messages[0]->content);
}
}
13 changes: 13 additions & 0 deletions tests/Chain/InputProcessor/SystemPromptService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\Chain\InputProcessor;

final class SystemPromptService implements \Stringable
{
public function __toString(): string
{
return 'My dynamic system prompt.';
}
}