Skip to content

Commit 2910133

Browse files
authored
fix: re-enable filtering of tools (#223)
1 parent e27a6e1 commit 2910133

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ $toolProcessor = new ChainProcessor($toolBox);
245245
$chain = new Chain($platform, $llm, inputProcessor: [$toolProcessor], outputProcessor: [$toolProcessor]);
246246
```
247247

248+
#### Tool Filtering
249+
250+
To limit the tools provided to the LLM in a specific chain call to a subset of the configured tools, you can use the
251+
`tools` option with a list of tool names:
252+
253+
```php
254+
$this->chain->call($messages, ['tools' => ['tavily_search']]);
255+
```
256+
248257
#### Tool Result Interception
249258

250259
To react to the result of a tool, you can implement an EventListener or EventSubscriber, that listens to the

src/Chain/ToolBox/ChainProcessor.php

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ 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);
48+
}
49+
4650
$options['tools'] = $toolMap;
4751
$input->setOptions($options);
4852
}

tests/Chain/ToolBox/ChainProcessorTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ public function processInputWithRegisteredToolsWillResultInOptionChange(): void
5555
self::assertSame(['tools' => ['tool1' => 'tool1', 'tool2' => 'tool2']], $input->getOptions());
5656
}
5757

58+
#[Test]
59+
public function processInputWithRegisteredToolsButToolOverride(): void
60+
{
61+
$toolBox = $this->createStub(ToolBoxInterface::class);
62+
$toolBox->method('getMap')->willReturn(['tool1' => 'tool1-metadata', 'tool2' => 'tool2-metadata']);
63+
64+
$llm = $this->createMock(LanguageModel::class);
65+
$llm->method('supportsToolCalling')->willReturn(true);
66+
67+
$chainProcessor = new ChainProcessor($toolBox);
68+
$input = new Input($llm, new MessageBag(), ['tools' => ['tool2']]);
69+
70+
$chainProcessor->processInput($input);
71+
72+
self::assertSame(['tools' => ['tool2' => 'tool2-metadata']], $input->getOptions());
73+
}
74+
5875
#[Test]
5976
public function processInputWithUnsupportedToolCallingWillThrowException(): void
6077
{

0 commit comments

Comments
 (0)