Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
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
33 changes: 32 additions & 1 deletion src/Chain/InputProcessor/SystemPromptInputProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@

use PhpLlm\LlmChain\Chain\Input;
use PhpLlm\LlmChain\Chain\InputProcessor;
use PhpLlm\LlmChain\Chain\ToolBox\Metadata;
use PhpLlm\LlmChain\Chain\ToolBox\ToolBoxInterface;
use PhpLlm\LlmChain\Model\Message\Message;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

final readonly class SystemPromptInputProcessor implements InputProcessor
{
/**
* @param 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 ?ToolBoxInterface $toolBox = null,
private LoggerInterface $logger = new NullLogger(),
) {
}
Expand All @@ -28,6 +35,30 @@ public function processInput(Input $input): void
return;
}

$input->messages = $messages->prepend(Message::forSystem($this->systemPrompt));
$message = $this->systemPrompt;

if ($this->toolBox instanceof ToolBoxInterface
&& [] !== $this->toolBox->getMap()
) {
$this->logger->debug('Append tool definitions to system prompt.');

$tools = implode(PHP_EOL.PHP_EOL, array_map(
fn (Metadata $tool) => <<<TOOL
## {$tool->name}
{$tool->description}
TOOL,
$this->toolBox->getMap()
));

$message = <<<PROMPT
{$this->systemPrompt}

# Available tools

{$tools}
PROMPT;
}

$input->messages = $messages->prepend(Message::forSystem($message));
}
}
84 changes: 84 additions & 0 deletions tests/Chain/InputProcessor/SystemPromptInputProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
use PhpLlm\LlmChain\Bridge\OpenAI\GPT;
use PhpLlm\LlmChain\Chain\Input;
use PhpLlm\LlmChain\Chain\InputProcessor\SystemPromptInputProcessor;
use PhpLlm\LlmChain\Chain\ToolBox\Metadata;
use PhpLlm\LlmChain\Chain\ToolBox\ToolBoxInterface;
use PhpLlm\LlmChain\Model\Message\Content\Text;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Message\MessageBag;
use PhpLlm\LlmChain\Model\Message\SystemMessage;
use PhpLlm\LlmChain\Model\Message\UserMessage;
use PhpLlm\LlmChain\Model\Response\ToolCall;
use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolNoParams;
use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolRequiredParams;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
Expand Down Expand Up @@ -62,4 +67,83 @@ public function processInputDoesNotAddSystemMessageWhenOneExists(): void
self::assertInstanceOf(UserMessage::class, $messages[1]);
self::assertSame('This is already a system prompt', $messages[0]->content);
}

#[Test]
public function doesNotIncludeToolsIfToolboxIsEmpty(): void
{
$processor = new SystemPromptInputProcessor(
'This is a system prompt',
new class implements ToolBoxInterface {
public function getMap(): array
{
return [];
}

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('This is a system prompt', $messages[0]->content);
}

#[Test]
public function includeToolDefinitions(): void
{
$processor = new SystemPromptInputProcessor(
'This is a system prompt',
new class implements ToolBoxInterface {
public function getMap(): array
{
return [
new Metadata(ToolNoParams::class, 'tool_no_params', 'A tool without parameters', '__invoke', null),
new Metadata(
ToolRequiredParams::class,
'tool_required_params',
<<<DESCRIPTION
A tool with required parameters
or not
DESCRIPTION,
'bar',
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
This is a system prompt

# Available tools

## tool_no_params
A tool without parameters

## tool_required_params
A tool with required parameters
or not
PROMPT, $messages[0]->content);
}
}