Skip to content

feat: add inline tool definition and chain in chain 🤯 #75

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,20 @@ llm_chain:
system_prompt: 'You are a helpful assistant that can answer questions.' # The default system prompt of the chain
include_tools: true # Include tool definitions at the end of the system prompt
tools:
- 'PhpLlm\LlmChain\Chain\ToolBox\Tool\SimilaritySearch'
# Referencing a service with #[AsTool] attribute
- 'PhpLlm\LlmChain\Chain\Toolbox\Tool\SimilaritySearch'

# Referencing a service without #[AsTool] attribute
- service: 'App\Chain\Tool\CompanyName'
name: 'company_name'
description: 'Provides the name of your company'
method: 'foo' # Optional with default value '__invoke'

# Referencing a chain => chain in chain 🤯
- service: 'llm_chain.chain.research'
name: 'wikipedia_research'
description: 'Can research on Wikipedia'
is_chain: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While thinking about this, do we really need this? We know service is a ChainInterface 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another thing of writing could be:

- service: 'llm_chain.chain.research'
+ chain: 'llm_chain.chain.research'
name: 'wikipedia_research'
description: 'Can research on Wikipedia'
- is_chain: true

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but I think I would just remove the is_chain

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i tried to omit that thing, but failed 😢

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but why, don't we know at that time, that the service is an instance of ChainInterface?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm with you on this, but i just don't know how 😆

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright then, but I can also set is_chain true and add anything else in service then, but we can communicate that with a clear error I guess

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait a sec

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waiting again ... ⏳ 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quite in your face, yes

image

research:
platform: 'llm_chain.platform.anthropic'
model:
Expand Down
16 changes: 15 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,21 @@ public function getConfigTreeBuilder(): TreeBuilder
->children()
->booleanNode('enabled')->defaultTrue()->end()
->arrayNode('services')
->scalarPrototype()->end()
->arrayPrototype()
->children()
->scalarNode('service')->isRequired()->end()
->scalarNode('name')->end()
->scalarNode('description')->end()
->scalarNode('method')->end()
->booleanNode('is_chain')->defaultFalse()->end()
->end()
->beforeNormalization()
->ifString()
->then(function (string $v) {
return ['service' => $v];
})
->end()
->end()
->end()
->end()
->end()
Expand Down
27 changes: 26 additions & 1 deletion src/DependencyInjection/LlmChainExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
use PhpLlm\LlmChain\Chain\Toolbox\Attribute\AsTool;
use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor as ToolProcessor;
use PhpLlm\LlmChain\Chain\Toolbox\FaultTolerantToolbox;
use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\ChainFactory;
use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\MemoryFactory;
use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\ReflectionFactory;
use PhpLlm\LlmChain\Chain\Toolbox\Tool\Chain as ChainTool;
use PhpLlm\LlmChain\ChainInterface;
use PhpLlm\LlmChain\Embedder;
use PhpLlm\LlmChain\Model\EmbeddingsModel;
Expand Down Expand Up @@ -249,9 +253,30 @@ private function processChainConfig(string $name, array $config, ContainerBuilde
if ($config['tools']['enabled']) {
// Create specific toolbox and process if tools are explicitly defined
if (0 !== count($config['tools']['services'])) {
$tools = array_map(static fn (string $tool) => new Reference($tool), $config['tools']['services']);
$memoryFactoryDefinition = new Definition(MemoryFactory::class);
$container->setDefinition('llm_chain.toolbox.'.$name.'.memory_factory', $memoryFactoryDefinition);
$chainFactoryDefinition = new Definition(ChainFactory::class, [
'$factories' => [new Reference('llm_chain.toolbox.'.$name.'.memory_factory'), new Reference(ReflectionFactory::class)],
]);
$container->setDefinition('llm_chain.toolbox.'.$name.'.chain_factory', $chainFactoryDefinition);

$tools = [];
foreach ($config['tools']['services'] as $tool) {
$reference = new Reference($tool['service']);
// We use the memory factory in case method, description and name are set
if (isset($tool['name'], $tool['description'])) {
if ($tool['is_chain']) {
$chainWrapperDefinition = new Definition(ChainTool::class, ['$chain' => $reference]);
$container->setDefinition('llm_chain.toolbox.'.$name.'.chain_wrapper.'.$tool['name'], $chainWrapperDefinition);
$reference = new Reference('llm_chain.toolbox.'.$name.'.chain_wrapper.'.$tool['name']);
}
$memoryFactoryDefinition->addMethodCall('addTool', [$reference, $tool['name'], $tool['description'], $tool['method'] ?? '__invoke']);
}
$tools[] = $reference;
}

$toolboxDefinition = (new ChildDefinition('llm_chain.toolbox.abstract'))
->replaceArgument('$metadataFactory', new Reference('llm_chain.toolbox.'.$name.'.chain_factory'))
->replaceArgument('$tools', $tools);
$container->setDefinition('llm_chain.toolbox.'.$name, $toolboxDefinition);

Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
->autowire()
->abstract()
->args([
'$metadataFactory' => service(MetadataFactory::class),
'$tools' => abstract_arg('Collection of tools'),
])
->set(Toolbox::class)
Expand Down