Skip to content

Commit f11f245

Browse files
authored
fix: consolidate tool config and make it work again (#56)
1 parent d4e2ac3 commit f11f245

File tree

3 files changed

+63
-23
lines changed

3 files changed

+63
-23
lines changed

README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ llm_chain:
5151
platform: 'llm_chain.platform.anthropic'
5252
model:
5353
name: 'Claude'
54-
tools: # If undefined, all tools are injected into the chain, use "[]" or "false" to have no tools.
54+
tools: # If undefined, all tools are injected into the chain, use "tools: false" to disable tools.
5555
- 'PhpLlm\LlmChain\Chain\ToolBox\Tool\Wikipedia'
5656
store:
5757
# also azure_search, mongodb and pinecone are supported as store type
@@ -129,6 +129,25 @@ final class CompanyName
129129
}
130130
```
131131

132+
The chain configuration by default will inject all known tools into the chain.
133+
134+
To disable this behavior, set the `tools` option to `false`:
135+
```yaml
136+
llm_chain:
137+
chain:
138+
my_chain:
139+
tools: false
140+
```
141+
142+
To inject only specific tools, list them in the configuration:
143+
```yaml
144+
llm_chain:
145+
chain:
146+
my_chain:
147+
tools:
148+
- 'PhpLlm\LlmChain\Chain\ToolBox\Tool\SimilaritySearch'
149+
```
150+
132151
### Profiler
133152

134153
The profiler panel provides insights into the chain's execution:

src/DependencyInjection/Configuration.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,25 @@ public function getConfigTreeBuilder(): TreeBuilder
6565
->end()
6666
->booleanNode('structured_output')->defaultTrue()->end()
6767
->arrayNode('tools')
68+
->addDefaultsIfNotSet()
69+
->treatFalseLike(['enabled' => false])
70+
->treatTrueLike(['enabled' => true])
71+
->treatNullLike(['enabled' => true])
6872
->beforeNormalization()
69-
->ifTrue(fn ($v) => false === $v)
70-
->then(fn () => [])
73+
->ifArray()
74+
->then(function (array $v) {
75+
return [
76+
'enabled' => $v['enabled'] ?? true,
77+
'services' => $v['services'] ?? $v,
78+
];
79+
})
80+
->end()
81+
->children()
82+
->booleanNode('enabled')->defaultTrue()->end()
83+
->arrayNode('services')
84+
->scalarPrototype()->end()
85+
->end()
7186
->end()
72-
->scalarPrototype()->end()
7387
->end()
7488
->end()
7589
->end()

src/DependencyInjection/LlmChainExtension.php

+26-19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PhpLlm\LlmChain\Chain\OutputProcessor;
2222
use PhpLlm\LlmChain\Chain\StructuredOutput\ChainProcessor as StructureOutputProcessor;
2323
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\AsTool;
24+
use PhpLlm\LlmChain\Chain\ToolBox\ChainProcessor as ToolProcessor;
2425
use PhpLlm\LlmChain\ChainInterface;
2526
use PhpLlm\LlmChain\Embedder;
2627
use PhpLlm\LlmChain\Model\EmbeddingsModel;
@@ -224,27 +225,33 @@ private function processChainConfig(string $name, array $config, ContainerBuilde
224225
$outputProcessor = [];
225226

226227
// TOOL & PROCESSOR
227-
if (0 !== count($config['tools'])) {
228-
$tools = array_map(static fn (string $tool) => new Reference($tool), $config['tools']);
229-
$toolboxDefinition = (new ChildDefinition('llm_chain.toolbox.abstract'))
230-
->replaceArgument('$tools', $tools);
231-
$container->setDefinition('llm_chain.toolbox.'.$name, $toolboxDefinition);
232-
233-
if ($container->getParameter('kernel.debug')) {
234-
$traceableToolboxDefinition = (new Definition('llm_chain.traceable_toolbox.'.$name))
235-
->setClass(TraceableToolBox::class)
236-
->setAutowired(true)
237-
->setDecoratedService('llm_chain.toolbox.'.$name)
238-
->addTag('llm_chain.traceable_toolbox');
239-
$container->setDefinition('llm_chain.traceable_toolbox.'.$name, $traceableToolboxDefinition);
240-
}
228+
if ($config['tools']['enabled']) {
229+
// Create specific tool box and process if tools are explicitly defined
230+
if (0 !== count($config['tools']['services'])) {
231+
$tools = array_map(static fn (string $tool) => new Reference($tool), $config['tools']['services']);
232+
$toolboxDefinition = (new ChildDefinition('llm_chain.toolbox.abstract'))
233+
->replaceArgument('$tools', $tools);
234+
$container->setDefinition('llm_chain.toolbox.'.$name, $toolboxDefinition);
235+
236+
if ($container->getParameter('kernel.debug')) {
237+
$traceableToolboxDefinition = (new Definition('llm_chain.traceable_toolbox.'.$name))
238+
->setClass(TraceableToolBox::class)
239+
->setAutowired(true)
240+
->setDecoratedService('llm_chain.toolbox.'.$name)
241+
->addTag('llm_chain.traceable_toolbox');
242+
$container->setDefinition('llm_chain.traceable_toolbox.'.$name, $traceableToolboxDefinition);
243+
}
241244

242-
$toolProcessorDefinition = (new ChildDefinition('llm_chain.tool.chain_processor.abstract'))
243-
->replaceArgument('$toolBox', new Reference('llm_chain.toolbox.'.$name));
244-
$container->setDefinition('llm_chain.tool.chain_processor.'.$name, $toolProcessorDefinition);
245+
$toolProcessorDefinition = (new ChildDefinition('llm_chain.tool.chain_processor.abstract'))
246+
->replaceArgument('$toolBox', new Reference('llm_chain.toolbox.'.$name));
247+
$container->setDefinition('llm_chain.tool.chain_processor.'.$name, $toolProcessorDefinition);
245248

246-
$inputProcessor[] = new Reference('llm_chain.tool.chain_processor.'.$name);
247-
$outputProcessor[] = new Reference('llm_chain.tool.chain_processor.'.$name);
249+
$inputProcessor[] = new Reference('llm_chain.tool.chain_processor.'.$name);
250+
$outputProcessor[] = new Reference('llm_chain.tool.chain_processor.'.$name);
251+
} else {
252+
$inputProcessor[] = new Reference(ToolProcessor::class);
253+
$outputProcessor[] = new Reference(ToolProcessor::class);
254+
}
248255
}
249256

250257
// STRUCTURED OUTPUT

0 commit comments

Comments
 (0)