|
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,8 +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 | + } |
| 277 | + |
253 | 278 | $toolboxDefinition = (new ChildDefinition('llm_chain.toolbox.abstract'))
|
| 279 | + ->replaceArgument('$metadataFactory', new Reference('llm_chain.toolbox.'.$name.'.chain_factory')) |
254 | 280 | ->replaceArgument('$tools', $tools);
|
255 | 281 | $container->setDefinition('llm_chain.toolbox.'.$name, $toolboxDefinition);
|
256 | 282 |
|
@@ -424,10 +450,11 @@ private function processEmbedderConfig(int|string $name, array $config, Containe
|
424 | 450 | $modelDefinition->addTag('llm_chain.model.embeddings_model');
|
425 | 451 | $container->setDefinition('llm_chain.embedder.'.$name.'.embeddings', $modelDefinition);
|
426 | 452 |
|
427 |
| - $definition = (new ChildDefinition('llm_chain.embedder.abstract')) |
428 |
| - ->replaceArgument('$platform', new Reference($config['platform'])) |
429 |
| - ->replaceArgument('$store', new Reference($config['store'])) |
430 |
| - ->replaceArgument('$embeddings', new Reference('llm_chain.embedder.'.$name.'.embeddings')); |
| 453 | + $definition = new Definition(Embedder::class, [ |
| 454 | + '$embeddings' => new Reference('llm_chain.embedder.'.$name.'.embeddings'), |
| 455 | + '$platform' => new Reference($config['platform']), |
| 456 | + '$store' => new Reference($config['store']), |
| 457 | + ]); |
431 | 458 |
|
432 | 459 | $container->setDefinition('llm_chain.embedder.'.$name, $definition);
|
433 | 460 | }
|
|
0 commit comments