|
4 | 4 |
|
5 | 5 | namespace PhpLlm\LlmChain\Tests\Chain\Toolbox;
|
6 | 6 |
|
| 7 | +use PhpLlm\LlmChain\Chain; |
7 | 8 | use PhpLlm\LlmChain\Chain\Input;
|
| 9 | +use PhpLlm\LlmChain\Chain\Output; |
8 | 10 | use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor;
|
9 | 11 | use PhpLlm\LlmChain\Chain\Toolbox\ExecutionReference;
|
10 | 12 | use PhpLlm\LlmChain\Chain\Toolbox\Metadata;
|
11 | 13 | use PhpLlm\LlmChain\Chain\Toolbox\ToolboxInterface;
|
12 | 14 | use PhpLlm\LlmChain\Exception\MissingModelSupport;
|
13 | 15 | use PhpLlm\LlmChain\Model\LanguageModel;
|
| 16 | +use PhpLlm\LlmChain\Model\Message\AssistantMessage; |
14 | 17 | use PhpLlm\LlmChain\Model\Message\MessageBag;
|
| 18 | +use PhpLlm\LlmChain\Model\Message\ToolCallMessage; |
| 19 | +use PhpLlm\LlmChain\Model\Response\ToolCall; |
| 20 | +use PhpLlm\LlmChain\Model\Response\ToolCallResponse; |
| 21 | +use PhpLlm\LlmChain\PlatformInterface; |
15 | 22 | use PHPUnit\Framework\Attributes\CoversClass;
|
16 | 23 | use PHPUnit\Framework\Attributes\Test;
|
17 | 24 | use PHPUnit\Framework\Attributes\UsesClass;
|
18 | 25 | use PHPUnit\Framework\TestCase;
|
| 26 | +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
19 | 27 |
|
20 | 28 | #[CoversClass(ChainProcessor::class)]
|
21 | 29 | #[UsesClass(Input::class)]
|
@@ -93,4 +101,60 @@ public function processInputWithUnsupportedToolCallingWillThrowException(): void
|
93 | 101 |
|
94 | 102 | $chainProcessor->processInput($input);
|
95 | 103 | }
|
| 104 | + |
| 105 | + #[Test] |
| 106 | + public function processOutputWithToolCallResponseKeepingMessages(): void |
| 107 | + { |
| 108 | + $toolbox = $this->createMock(ToolboxInterface::class); |
| 109 | + $toolbox->expects($this->once())->method('execute')->willReturn('Test response'); |
| 110 | + |
| 111 | + $llm = $this->createStub(LanguageModel::class); |
| 112 | + |
| 113 | + $eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
| 114 | + $eventDispatcher->expects($this->once())->method('dispatch'); |
| 115 | + |
| 116 | + $messageBag = new MessageBag(); |
| 117 | + |
| 118 | + $response = new ToolCallResponse(new ToolCall('id1', 'tool1', ['arg1' => 'value1'])); |
| 119 | + |
| 120 | + $chain = new Chain($this->createStub(PlatformInterface::class), $llm); |
| 121 | + |
| 122 | + $chainProcessor = new ChainProcessor($toolbox, eventDispatcher: $eventDispatcher, keepToolMessages: true); |
| 123 | + $chainProcessor->setChain($chain); |
| 124 | + |
| 125 | + $output = new Output($llm, $response, $messageBag, []); |
| 126 | + |
| 127 | + $chainProcessor->processOutput($output); |
| 128 | + |
| 129 | + self::assertCount(2, $messageBag); |
| 130 | + self::assertInstanceOf(AssistantMessage::class, $messageBag->getMessages()[0]); |
| 131 | + self::assertInstanceOf(ToolCallMessage::class, $messageBag->getMessages()[1]); |
| 132 | + } |
| 133 | + |
| 134 | + #[Test] |
| 135 | + public function processOutputWithToolCallResponseForgettingMessages(): void |
| 136 | + { |
| 137 | + $toolbox = $this->createMock(ToolboxInterface::class); |
| 138 | + $toolbox->expects($this->once())->method('execute')->willReturn('Test response'); |
| 139 | + |
| 140 | + $llm = $this->createStub(LanguageModel::class); |
| 141 | + |
| 142 | + $eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
| 143 | + $eventDispatcher->expects($this->once())->method('dispatch'); |
| 144 | + |
| 145 | + $messageBag = new MessageBag(); |
| 146 | + |
| 147 | + $response = new ToolCallResponse(new ToolCall('id1', 'tool1', ['arg1' => 'value1'])); |
| 148 | + |
| 149 | + $chain = new Chain($this->createStub(PlatformInterface::class), $llm); |
| 150 | + |
| 151 | + $chainProcessor = new ChainProcessor($toolbox, eventDispatcher: $eventDispatcher, keepToolMessages: false); |
| 152 | + $chainProcessor->setChain($chain); |
| 153 | + |
| 154 | + $output = new Output($llm, $response, $messageBag, []); |
| 155 | + |
| 156 | + $chainProcessor->processOutput($output); |
| 157 | + |
| 158 | + self::assertCount(0, $messageBag); |
| 159 | + } |
96 | 160 | }
|
0 commit comments