|
26 | 26 | use PhpLlm\LlmChain\Chain\Toolbox\Attribute\AsTool; |
27 | 27 | use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor as ToolProcessor; |
28 | 28 | use PhpLlm\LlmChain\Chain\Toolbox\FaultTolerantToolbox; |
| 29 | +use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\ChainFactory; |
| 30 | +use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\MemoryFactory; |
| 31 | +use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\ReflectionFactory; |
| 32 | +use PhpLlm\LlmChain\Chain\Toolbox\Tool\Chain as ChainTool; |
29 | 33 | use PhpLlm\LlmChain\ChainInterface; |
30 | 34 | use PhpLlm\LlmChain\Embedder; |
31 | 35 | use PhpLlm\LlmChain\Model\EmbeddingsModel; |
@@ -249,9 +253,30 @@ private function processChainConfig(string $name, array $config, ContainerBuilde |
249 | 253 | if ($config['tools']['enabled']) { |
250 | 254 | // Create specific toolbox and process if tools are explicitly defined |
251 | 255 | if (0 !== count($config['tools']['services'])) { |
252 | | - $tools = array_map(static fn (string $tool) => new Reference($tool), $config['tools']['services']); |
| 256 | + $memoryFactoryDefinition = new Definition(MemoryFactory::class); |
| 257 | + $container->setDefinition('llm_chain.toolbox.'.$name.'.memory_factory', $memoryFactoryDefinition); |
| 258 | + $chainFactoryDefinition = new Definition(ChainFactory::class, [ |
| 259 | + '$factories' => [new Reference('llm_chain.toolbox.'.$name.'.memory_factory'), new Reference(ReflectionFactory::class)], |
| 260 | + ]); |
| 261 | + $container->setDefinition('llm_chain.toolbox.'.$name.'.chain_factory', $chainFactoryDefinition); |
| 262 | + |
| 263 | + $tools = []; |
| 264 | + foreach ($config['tools']['services'] as $tool) { |
| 265 | + $reference = new Reference($tool['service']); |
| 266 | + // We use the memory factory in case method, description and name are set |
| 267 | + if (isset($tool['name'], $tool['description'])) { |
| 268 | + if ($tool['is_chain']) { |
| 269 | + $chainWrapperDefinition = new Definition(ChainTool::class, ['$chain' => $reference]); |
| 270 | + $container->setDefinition('llm_chain.toolbox.'.$name.'.chain_wrapper.'.$tool['name'], $chainWrapperDefinition); |
| 271 | + $reference = new Reference('llm_chain.toolbox.'.$name.'.chain_wrapper.'.$tool['name']); |
| 272 | + } |
| 273 | + $memoryFactoryDefinition->addMethodCall('addTool', [$reference, $tool['name'], $tool['description'], $tool['method'] ?? '__invoke']); |
| 274 | + } |
| 275 | + $tools[] = $reference; |
| 276 | + } |
253 | 277 |
|
254 | 278 | $toolboxDefinition = (new ChildDefinition('llm_chain.toolbox.abstract')) |
| 279 | + ->replaceArgument('$metadataFactory', new Reference('llm_chain.toolbox.'.$name.'.chain_factory')) |
255 | 280 | ->replaceArgument('$tools', $tools); |
256 | 281 | $container->setDefinition('llm_chain.toolbox.'.$name, $toolboxDefinition); |
257 | 282 |
|
|
0 commit comments