-
-
Notifications
You must be signed in to change notification settings - Fork 102
[Agent] Add Platform tool wrapper for using platforms as tools #722
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
Draft
OskarStark
wants to merge
15
commits into
symfony:main
Choose a base branch
from
OskarStark:OskarStark/platforms-as-tools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−2
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8d8f2ad
[Agent] Add Platform tool wrapper for using platforms as tools
OskarStark 1cbb279
[Agent] Remove README-platform-as-tool.md
OskarStark 98e4760
[AiBundle] Add configuration support for platform tools
OskarStark 8320e3c
[Agent] Simplify Platform tool by using asText() method
OskarStark f4b35ac
-
OskarStark 523d7a2
[Examples] Use MemoryToolFactory pattern in platform-as-tool example
OskarStark 9962349
-
OskarStark 46a1ffc
[Examples] Simplify platform-as-tool to use OpenAI gpt-4o
OskarStark e5412d5
-
OskarStark 11eaf4f
-
OskarStark 983f25f
-
OskarStark c28d51a
-
OskarStark ad6722d
[Examples] Use simple math question in platform-as-tool example
OskarStark a806b22
[Agent] Update Platform tool docblock and fix parameter name
OskarStark ee38c77
-
OskarStark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\AI\Agent\Agent; | ||
use Symfony\AI\Agent\Toolbox\AgentProcessor; | ||
use Symfony\AI\Agent\Toolbox\Tool\Platform as PlatformTool; | ||
use Symfony\AI\Agent\Toolbox\Toolbox; | ||
use Symfony\AI\Agent\Toolbox\ToolFactory\ChainFactory; | ||
use Symfony\AI\Agent\Toolbox\ToolFactory\MemoryToolFactory; | ||
use Symfony\AI\Agent\Toolbox\ToolFactory\ReflectionToolFactory; | ||
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory; | ||
use Symfony\AI\Platform\Message\Message; | ||
use Symfony\AI\Platform\Message\MessageBag; | ||
|
||
require_once dirname(__DIR__).'/bootstrap.php'; | ||
|
||
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client()); | ||
|
||
// Create a specialized OpenAI platform tool using gpt-4o for mathematical calculations | ||
$mathTool = new PlatformTool($platform, 'gpt-4o'); | ||
|
||
// Use MemoryToolFactory to register the tool with metadata | ||
$memoryFactory = new MemoryToolFactory(); | ||
$memoryFactory->addTool( | ||
$mathTool, | ||
'calculate', | ||
'Performs mathematical calculations using GPT-4o. Use this when you need to solve math problems or do arithmetic.', | ||
); | ||
|
||
// Combine with ReflectionToolFactory using ChainFactory | ||
$chainFactory = new ChainFactory([ | ||
$memoryFactory, | ||
new ReflectionToolFactory(), | ||
]); | ||
|
||
// Create the main agent with gpt-4o-mini but with gpt-4o available as a tool | ||
$toolbox = new Toolbox([$mathTool], toolFactory: $chainFactory, logger: logger()); | ||
$processor = new AgentProcessor($toolbox); | ||
$agent = new Agent($platform, 'gpt-4o-mini', [$processor], [$processor], logger: logger()); | ||
|
||
// Ask a question that requires mathematical calculation | ||
$result = $agent->call(new MessageBag(Message::ofUser( | ||
'I have 15 apples and I want to share them equally among 4 friends. How many apples does each friend get and how many are left over?' | ||
))); | ||
|
||
echo $result->getContent().\PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\AI\Agent\Toolbox\Tool; | ||
|
||
use Symfony\AI\Platform\PlatformInterface; | ||
|
||
/** | ||
* Wraps a Platform instance as a tool, allowing agents to use specialized platforms for specific tasks. | ||
* | ||
* This enables scenarios where an agent can leverage different models or platforms | ||
* as tools (e.g., using gpt-4o for complex calculations while using gpt-4o-mini for the main agent). | ||
* | ||
* @author Oskar Stark <[email protected]> | ||
*/ | ||
final readonly class Platform | ||
{ | ||
/** | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function __construct( | ||
private PlatformInterface $platform, | ||
private string $model, | ||
private array $options = [], | ||
) { | ||
} | ||
|
||
/** | ||
* @param string $message the message to pass to the chain | ||
*/ | ||
public function __invoke(string $message): string | ||
{ | ||
return $this->platform->invoke( | ||
$this->model, | ||
$message, | ||
$this->options, | ||
)->asText(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -390,6 +390,12 @@ | |
->children() | ||
->stringNode('service')->cannotBeEmpty()->end() | ||
->stringNode('agent')->cannotBeEmpty()->end() | ||
->stringNode('platform')->cannotBeEmpty()->end() | ||
->stringNode('model')->cannotBeEmpty()->end() | ||
->arrayNode('options') | ||
->info('Options to pass to the platform') | ||
->scalarPrototype()->end() | ||
->end() | ||
Comment on lines
+394
to
+398
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we validate that |
||
->stringNode('name')->end() | ||
->stringNode('description')->end() | ||
->stringNode('method')->end() | ||
|
@@ -401,8 +407,27 @@ | |
}) | ||
->end() | ||
->validate() | ||
->ifTrue(static fn ($v) => !(empty($v['agent']) xor empty($v['service']))) | ||
->thenInvalid('Either "agent" or "service" must be configured, and never both.') | ||
->ifTrue(static function ($v) { | ||
$count = 0; | ||
if (!empty($v['agent'])) { | ||
++$count; | ||
} | ||
if (!empty($v['service'])) { | ||
++$count; | ||
} | ||
if (!empty($v['platform'])) { | ||
++$count; | ||
} | ||
|
||
return 1 !== $count; | ||
Comment on lines
+411
to
+422
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ha nice, that's pragmatic 👍 |
||
}) | ||
->thenInvalid('Exactly one of "agent", "service", or "platform" must be configured.') | ||
->end() | ||
->validate() | ||
->ifTrue(static function ($v) { | ||
return !empty($v['platform']) && empty($v['model']); | ||
}) | ||
->thenInvalid('When "platform" is configured, "model" must also be provided.') | ||
->end() | ||
->end() | ||
->end() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check for instanceof TextResult and handle other cases. at least ObjectResult can be json serialized