Skip to content

[AIBundle] Simplify agent-as-tool configuration #175

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 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
3 changes: 1 addition & 2 deletions demo/config/packages/ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ ai:
system_prompt: 'You are a friendly chatbot that likes to have a conversation with users and asks them some questions.'
tools:
# Agent in agent 🤯
- service: 'ai.agent.blog'
- agent: 'blog'
name: 'symfony_blog'
description: 'Can answer questions based on the Symfony blog.'
is_agent: true
store:
chroma_db:
symfonycon:
Expand Down
8 changes: 6 additions & 2 deletions src/ai-bundle/config/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,22 @@
->arrayNode('services')
->arrayPrototype()
->children()
->scalarNode('service')->isRequired()->end()
->scalarNode('service')->cannotBeEmpty()->end()
->scalarNode('agent')->cannotBeEmpty()->end()
->scalarNode('name')->end()
->scalarNode('description')->end()
->scalarNode('method')->end()
->booleanNode('is_agent')->defaultFalse()->end()
->end()
->beforeNormalization()
->ifString()
->then(function (string $v) {
return ['service' => $v];
})
->end()
->validate()
->ifTrue(static fn ($v) => !(empty($v['agent']) xor empty($v['service'])))
->thenInvalid('Either "agent" or "service" must be configured, and never both.')
->end()
->end()
->end()
->end()
Expand Down
3 changes: 1 addition & 2 deletions src/ai-bundle/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ Configuration
method: 'foo' # Optional with default value '__invoke'

# Referencing a agent => agent in agent 🤯
- service: 'ai.agent.research'
- agent: 'research'
name: 'wikipedia_research'
description: 'Can research on Wikipedia'
is_agent: true
research:
platform: 'ai.platform.anthropic'
model:
Expand Down
6 changes: 5 additions & 1 deletion src/ai-bundle/src/AIBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,14 @@ private function processAgentConfig(string $name, array $config, ContainerBuilde

$tools = [];
foreach ($config['tools']['services'] as $tool) {
if (isset($tool['agent'])) {
$tool['name'] ??= $tool['agent'];
$tool['service'] = \sprintf('ai.agent.%s', $tool['agent']);
}
$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_agent']) {
if (isset($tool['agent'])) {
$agentWrapperDefinition = new Definition(AgentTool::class, [$reference]);
$container->setDefinition('ai.toolbox.'.$name.'.agent_wrapper.'.$tool['name'], $agentWrapperDefinition);
$reference = new Reference('ai.toolbox.'.$name.'.agent_wrapper.'.$tool['name']);
Expand Down
52 changes: 49 additions & 3 deletions src/ai-bundle/tests/DependencyInjection/AIBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,72 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use Symfony\AI\AIBundle\AIBundle;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\ContainerBuilder;

#[CoversClass(AIBundle::class)]
#[UsesClass(ContainerBuilder::class)]
class AIBundleTest extends TestCase
{
#[DoesNotPerformAssertions]
public function testExtensionLoad(): void
#[Test]
public function extensionLoadDoesNotThrow(): void
{
$this->buildContainer($this->getFullConfig());
}

#[Test]
public function agentsCanBeRegisteredAsTools(): void
{
$container = $this->buildContainer([
'ai' => [
'agent' => [
'main_agent' => [
'model' => ['class' => 'Symfony\AI\Platform\Bridge\OpenAI\GPT'],
'tools' => [
['agent' => 'another_agent', 'description' => 'Agent tool with implicit name'],
['agent' => 'another_agent', 'name' => 'another_agent_instance', 'description' => 'Agent tool with explicit name'],
],
],
],
],
]);

$this->assertTrue($container->hasDefinition('ai.toolbox.main_agent.agent_wrapper.another_agent'));
$this->assertTrue($container->hasDefinition('ai.toolbox.main_agent.agent_wrapper.another_agent_instance'));
}

#[Test]
public function agentsAsToolsCannotDefineService(): void
{
$this->expectException(InvalidConfigurationException::class);
$this->buildContainer([
'ai' => [
'agent' => [
'main_agent' => [
'model' => ['class' => 'Symfony\AI\Platform\Bridge\OpenAI\GPT'],
'tools' => [['agent' => 'another_agent', 'service' => 'foo_bar', 'description' => 'Agent with service']],
],
],
],
]);
}

private function buildContainer(array $configuration): ContainerBuilder
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', true);
$container->setParameter('kernel.environment', 'dev');
$container->setParameter('kernel.build_dir', 'public');

$extension = (new AIBundle())->getContainerExtension();
$extension->load($configuration, $container);

$configs = $this->getFullConfig();
$extension->load($configs, $container);
return $container;
}

/**
Expand Down