|
7 | 7 | use PhpLlm\LlmChain\Bridge\OpenAI\GPT; |
8 | 8 | use PhpLlm\LlmChain\Chain\Input; |
9 | 9 | use PhpLlm\LlmChain\Chain\InputProcessor\SystemPromptInputProcessor; |
| 10 | +use PhpLlm\LlmChain\Chain\ToolBox\Metadata; |
| 11 | +use PhpLlm\LlmChain\Chain\ToolBox\ToolBoxInterface; |
10 | 12 | use PhpLlm\LlmChain\Model\Message\Content\Text; |
11 | 13 | use PhpLlm\LlmChain\Model\Message\Message; |
12 | 14 | use PhpLlm\LlmChain\Model\Message\MessageBag; |
13 | 15 | use PhpLlm\LlmChain\Model\Message\SystemMessage; |
14 | 16 | use PhpLlm\LlmChain\Model\Message\UserMessage; |
| 17 | +use PhpLlm\LlmChain\Model\Response\ToolCall; |
| 18 | +use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolNoParams; |
| 19 | +use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolRequiredParams; |
15 | 20 | use PHPUnit\Framework\Attributes\CoversClass; |
16 | 21 | use PHPUnit\Framework\Attributes\Small; |
17 | 22 | use PHPUnit\Framework\Attributes\Test; |
@@ -62,4 +67,94 @@ public function processInputDoesNotAddSystemMessageWhenOneExists(): void |
62 | 67 | self::assertInstanceOf(UserMessage::class, $messages[1]); |
63 | 68 | self::assertSame('This is already a system prompt', $messages[0]->content); |
64 | 69 | } |
| 70 | + |
| 71 | + #[Test] |
| 72 | + public function needsToolboxToBeAbleToIncludeToolDefinitions(): void |
| 73 | + { |
| 74 | + $this->expectException(\InvalidArgumentException::class); |
| 75 | + $this->expectExceptionMessage('Tool definitions cannot be included without a ToolBox.'); |
| 76 | + |
| 77 | + new SystemPromptInputProcessor( |
| 78 | + 'This is a system prompt', |
| 79 | + toolBox: null, |
| 80 | + includeToolDefinitions: true, |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + #[Test] |
| 85 | + public function includeToolDefinitions(): void |
| 86 | + { |
| 87 | + $processor = new SystemPromptInputProcessor( |
| 88 | + 'This is a system prompt', |
| 89 | + new class implements ToolBoxInterface { |
| 90 | + public function getMap(): array |
| 91 | + { |
| 92 | + return [ |
| 93 | + new Metadata(ToolNoParams::class, 'tool_no_params', 'A tool without parameters', '__invoke', null), |
| 94 | + new Metadata( |
| 95 | + ToolRequiredParams::class, |
| 96 | + 'tool_required_params', |
| 97 | + <<<DESCRIPTION |
| 98 | +A tool with required parameters |
| 99 | +or not |
| 100 | +DESCRIPTION, |
| 101 | + 'bar', |
| 102 | + null |
| 103 | + ), |
| 104 | + ]; |
| 105 | + } |
| 106 | + |
| 107 | + public function execute(ToolCall $toolCall): mixed |
| 108 | + { |
| 109 | + return null; |
| 110 | + } |
| 111 | + }, |
| 112 | + includeToolDefinitions: true, |
| 113 | + ); |
| 114 | + |
| 115 | + $input = new Input(new GPT(), new MessageBag(Message::ofUser('This is a user message')), []); |
| 116 | + $processor->processInput($input); |
| 117 | + |
| 118 | + $messages = $input->messages->getMessages(); |
| 119 | + self::assertCount(2, $messages); |
| 120 | + self::assertInstanceOf(SystemMessage::class, $messages[0]); |
| 121 | + self::assertInstanceOf(UserMessage::class, $messages[1]); |
| 122 | + self::assertSame(<<<PROMPT |
| 123 | +This is a system prompt |
| 124 | +
|
| 125 | +# Available tools |
| 126 | +
|
| 127 | +## tool_no_params |
| 128 | +A tool without parameters |
| 129 | +
|
| 130 | +## tool_required_params |
| 131 | +A tool with required parameters |
| 132 | +or not |
| 133 | +PROMPT, $messages[0]->content); |
| 134 | + } |
| 135 | + |
| 136 | + private function createFaultyToolBox(\Closure $exceptionFactory): ToolBoxInterface |
| 137 | + { |
| 138 | + return new class($exceptionFactory) implements ToolBoxInterface { |
| 139 | + public function __construct(private readonly \Closure $exceptionFactory) |
| 140 | + { |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @return Metadata[] |
| 145 | + */ |
| 146 | + public function getMap(): array |
| 147 | + { |
| 148 | + return [ |
| 149 | + new Metadata(ToolNoParams::class, 'tool_no_params', 'A tool without parameters', '__invoke', null), |
| 150 | + new Metadata(ToolRequiredParams::class, 'tool_required_params', 'A tool with required parameters', 'bar', null), |
| 151 | + ]; |
| 152 | + } |
| 153 | + |
| 154 | + public function execute(ToolCall $toolCall): mixed |
| 155 | + { |
| 156 | + throw ($this->exceptionFactory)($toolCall); |
| 157 | + } |
| 158 | + }; |
| 159 | + } |
65 | 160 | } |
0 commit comments