From a26054aff6303fc1853848f8863cdbf9b776ffd6 Mon Sep 17 00:00:00 2001 From: Denis Zunke Date: Tue, 12 Nov 2024 17:59:02 +0100 Subject: [PATCH] fix empty tool box chain processor is setting invalid tools option to input --- src/ToolBox/ChainProcessor.php | 7 ++- tests/ToolBox/ChainProcessorTest.php | 71 ++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 tests/ToolBox/ChainProcessorTest.php diff --git a/src/ToolBox/ChainProcessor.php b/src/ToolBox/ChainProcessor.php index 8d29da9c..3faf91cf 100644 --- a/src/ToolBox/ChainProcessor.php +++ b/src/ToolBox/ChainProcessor.php @@ -29,8 +29,13 @@ public function processInput(Input $input): void throw MissingModelSupport::forToolCalling($input->llm::class); } + $toolMap = $this->toolBox->getMap(); + if ([] === $toolMap) { + return; + } + $options = $input->getOptions(); - $options['tools'] = $this->toolBox->getMap(); + $options['tools'] = $toolMap; $input->setOptions($options); } diff --git a/tests/ToolBox/ChainProcessorTest.php b/tests/ToolBox/ChainProcessorTest.php new file mode 100644 index 00000000..d88cf9a0 --- /dev/null +++ b/tests/ToolBox/ChainProcessorTest.php @@ -0,0 +1,71 @@ +createStub(ToolBoxInterface::class); + $toolBox->method('getMap')->willReturn([]); + + $llm = $this->createMock(LanguageModel::class); + $llm->method('supportsToolCalling')->willReturn(true); + + $chainProcessor = new ChainProcessor($toolBox); + $input = new Input($llm, new MessageBag(), []); + + $chainProcessor->processInput($input); + + self::assertSame([], $input->getOptions()); + } + + #[Test] + public function processInputWithRegisteredToolsWillResultInOptionChange(): void + { + $toolBox = $this->createStub(ToolBoxInterface::class); + $toolBox->method('getMap')->willReturn(['tool1' => 'tool1', 'tool2' => 'tool2']); + + $llm = $this->createMock(LanguageModel::class); + $llm->method('supportsToolCalling')->willReturn(true); + + $chainProcessor = new ChainProcessor($toolBox); + $input = new Input($llm, new MessageBag(), []); + + $chainProcessor->processInput($input); + + self::assertSame(['tools' => ['tool1' => 'tool1', 'tool2' => 'tool2']], $input->getOptions()); + } + + #[Test] + public function processInputWithUnsupportedToolCallingWillThrowException(): void + { + $this->expectException(MissingModelSupport::class); + + $llm = $this->createMock(LanguageModel::class); + $llm->method('supportsToolCalling')->willReturn(false); + + $chainProcessor = new ChainProcessor($this->createStub(ToolBoxInterface::class)); + $input = new Input($llm, new MessageBag(), []); + + $chainProcessor->processInput($input); + } +}