Skip to content

Commit 8da55bb

Browse files
authored
fix: actually fix the fix of tool filter (#224)
1 parent 2910133 commit 8da55bb

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/Chain/ToolBox/ChainProcessor.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ public function processInput(Input $input): void
4343
}
4444

4545
$options = $input->getOptions();
46-
if (isset($options['tools'])) {
47-
$toolMap = array_filter($toolMap, fn (string $tool) => in_array($tool, $options['tools'], true), ARRAY_FILTER_USE_KEY);
46+
// only filter tool map if list of strings is provided as option
47+
if (isset($options['tools']) && is_array($options['tools']) && is_string($options['tools'][0])) {
48+
$toolMap = array_values(array_filter($toolMap, fn (Metadata $tool) => in_array($tool->name, $options['tools'], true)));
4849
}
4950

5051
$options['tools'] = $toolMap;

tests/Chain/ToolBox/ChainProcessorTest.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpLlm\LlmChain\Chain\Input;
88
use PhpLlm\LlmChain\Chain\ToolBox\ChainProcessor;
9+
use PhpLlm\LlmChain\Chain\ToolBox\Metadata;
910
use PhpLlm\LlmChain\Chain\ToolBox\ToolBoxInterface;
1011
use PhpLlm\LlmChain\Exception\MissingModelSupport;
1112
use PhpLlm\LlmChain\Model\LanguageModel;
@@ -42,7 +43,9 @@ public function processInputWithoutRegisteredToolsWillResultInNoOptionChange():
4243
public function processInputWithRegisteredToolsWillResultInOptionChange(): void
4344
{
4445
$toolBox = $this->createStub(ToolBoxInterface::class);
45-
$toolBox->method('getMap')->willReturn(['tool1' => 'tool1', 'tool2' => 'tool2']);
46+
$tool1 = new Metadata('ClassTool1', 'tool1', 'description1', 'method1', null);
47+
$tool2 = new Metadata('ClassTool2', 'tool2', 'description2', 'method2', null);
48+
$toolBox->method('getMap')->willReturn([$tool1, $tool2]);
4649

4750
$llm = $this->createMock(LanguageModel::class);
4851
$llm->method('supportsToolCalling')->willReturn(true);
@@ -52,14 +55,16 @@ public function processInputWithRegisteredToolsWillResultInOptionChange(): void
5255

5356
$chainProcessor->processInput($input);
5457

55-
self::assertSame(['tools' => ['tool1' => 'tool1', 'tool2' => 'tool2']], $input->getOptions());
58+
self::assertSame(['tools' => [$tool1, $tool2]], $input->getOptions());
5659
}
5760

5861
#[Test]
5962
public function processInputWithRegisteredToolsButToolOverride(): void
6063
{
6164
$toolBox = $this->createStub(ToolBoxInterface::class);
62-
$toolBox->method('getMap')->willReturn(['tool1' => 'tool1-metadata', 'tool2' => 'tool2-metadata']);
65+
$tool1 = new Metadata('ClassTool1', 'tool1', 'description1', 'method1', null);
66+
$tool2 = new Metadata('ClassTool2', 'tool2', 'description2', 'method2', null);
67+
$toolBox->method('getMap')->willReturn([$tool1, $tool2]);
6368

6469
$llm = $this->createMock(LanguageModel::class);
6570
$llm->method('supportsToolCalling')->willReturn(true);
@@ -69,7 +74,7 @@ public function processInputWithRegisteredToolsButToolOverride(): void
6974

7075
$chainProcessor->processInput($input);
7176

72-
self::assertSame(['tools' => ['tool2' => 'tool2-metadata']], $input->getOptions());
77+
self::assertSame(['tools' => [$tool2]], $input->getOptions());
7378
}
7479

7580
#[Test]

0 commit comments

Comments
 (0)