|
4 | 4 |
|
5 | 5 | namespace PhpLlm\LlmChain\Tests\Chain\Toolbox;
|
6 | 6 |
|
| 7 | +use PhpLlm\LlmChain\Chain\ChainInterface; |
7 | 8 | use PhpLlm\LlmChain\Chain\Exception\MissingModelSupportException;
|
8 | 9 | use PhpLlm\LlmChain\Chain\Input;
|
| 10 | +use PhpLlm\LlmChain\Chain\Output; |
9 | 11 | use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor;
|
10 | 12 | use PhpLlm\LlmChain\Chain\Toolbox\ToolboxInterface;
|
11 | 13 | use PhpLlm\LlmChain\Platform\Capability;
|
| 14 | +use PhpLlm\LlmChain\Platform\Message\AssistantMessage; |
12 | 15 | use PhpLlm\LlmChain\Platform\Message\MessageBag;
|
| 16 | +use PhpLlm\LlmChain\Platform\Message\ToolCallMessage; |
13 | 17 | use PhpLlm\LlmChain\Platform\Model;
|
| 18 | +use PhpLlm\LlmChain\Platform\Response\ToolCall; |
| 19 | +use PhpLlm\LlmChain\Platform\Response\ToolCallResponse; |
14 | 20 | use PhpLlm\LlmChain\Platform\Tool\ExecutionReference;
|
15 | 21 | use PhpLlm\LlmChain\Platform\Tool\Tool;
|
16 | 22 | use PHPUnit\Framework\Attributes\CoversClass;
|
|
20 | 26 |
|
21 | 27 | #[CoversClass(ChainProcessor::class)]
|
22 | 28 | #[UsesClass(Input::class)]
|
| 29 | +#[UsesClass(Output::class)] |
23 | 30 | #[UsesClass(Tool::class)]
|
| 31 | +#[UsesClass(ToolCall::class)] |
| 32 | +#[UsesClass(ToolCallResponse::class)] |
24 | 33 | #[UsesClass(ExecutionReference::class)]
|
25 | 34 | #[UsesClass(MessageBag::class)]
|
26 | 35 | #[UsesClass(MissingModelSupportException::class)]
|
@@ -87,4 +96,54 @@ public function processInputWithUnsupportedToolCallingWillThrowException(): void
|
87 | 96 |
|
88 | 97 | $chainProcessor->processInput($input);
|
89 | 98 | }
|
| 99 | + |
| 100 | + #[Test] |
| 101 | + public function processOutputWithToolCallResponseKeepingMessages(): void |
| 102 | + { |
| 103 | + $toolbox = $this->createMock(ToolboxInterface::class); |
| 104 | + $toolbox->expects($this->once())->method('execute')->willReturn('Test response'); |
| 105 | + |
| 106 | + $model = new Model('gpt-4', [Capability::TOOL_CALLING]); |
| 107 | + |
| 108 | + $messageBag = new MessageBag(); |
| 109 | + |
| 110 | + $response = new ToolCallResponse(new ToolCall('id1', 'tool1', ['arg1' => 'value1'])); |
| 111 | + |
| 112 | + $chain = $this->createStub(ChainInterface::class); |
| 113 | + |
| 114 | + $chainProcessor = new ChainProcessor($toolbox, keepToolMessages: true); |
| 115 | + $chainProcessor->setChain($chain); |
| 116 | + |
| 117 | + $output = new Output($model, $response, $messageBag, []); |
| 118 | + |
| 119 | + $chainProcessor->processOutput($output); |
| 120 | + |
| 121 | + self::assertCount(2, $messageBag); |
| 122 | + self::assertInstanceOf(AssistantMessage::class, $messageBag->getMessages()[0]); |
| 123 | + self::assertInstanceOf(ToolCallMessage::class, $messageBag->getMessages()[1]); |
| 124 | + } |
| 125 | + |
| 126 | + #[Test] |
| 127 | + public function processOutputWithToolCallResponseForgettingMessages(): void |
| 128 | + { |
| 129 | + $toolbox = $this->createMock(ToolboxInterface::class); |
| 130 | + $toolbox->expects($this->once())->method('execute')->willReturn('Test response'); |
| 131 | + |
| 132 | + $model = new Model('gpt-4', [Capability::TOOL_CALLING]); |
| 133 | + |
| 134 | + $messageBag = new MessageBag(); |
| 135 | + |
| 136 | + $response = new ToolCallResponse(new ToolCall('id1', 'tool1', ['arg1' => 'value1'])); |
| 137 | + |
| 138 | + $chain = $this->createStub(ChainInterface::class); |
| 139 | + |
| 140 | + $chainProcessor = new ChainProcessor($toolbox, keepToolMessages: false); |
| 141 | + $chainProcessor->setChain($chain); |
| 142 | + |
| 143 | + $output = new Output($model, $response, $messageBag, []); |
| 144 | + |
| 145 | + $chainProcessor->processOutput($output); |
| 146 | + |
| 147 | + self::assertCount(0, $messageBag); |
| 148 | + } |
90 | 149 | }
|
0 commit comments