|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +use Symfony\AI\Agent\Agent; |
| 13 | +use Symfony\AI\Agent\Toolbox\AgentProcessor; |
| 14 | +use Symfony\AI\Agent\Toolbox\Tool\Clock; |
| 15 | +use Symfony\AI\Agent\Toolbox\Toolbox; |
| 16 | +use Symfony\AI\Platform\Bridge\Anthropic\PlatformFactory; |
| 17 | +use Symfony\AI\Platform\Message\Message; |
| 18 | +use Symfony\AI\Platform\Message\MessageBag; |
| 19 | +use Symfony\AI\Platform\StructuredOutput\PlatformSubscriber; |
| 20 | +use Symfony\Component\Clock\Clock as SymfonyClock; |
| 21 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 22 | + |
| 23 | +require_once dirname(__DIR__).'/bootstrap.php'; |
| 24 | + |
| 25 | +$dispatcher = new EventDispatcher(); |
| 26 | +$dispatcher->addSubscriber(new PlatformSubscriber()); |
| 27 | + |
| 28 | +$platform = PlatformFactory::create(env('ANTHROPIC_API_KEY'), http_client(), eventDispatcher: $dispatcher); |
| 29 | + |
| 30 | +$clock = new Clock(new SymfonyClock()); |
| 31 | +$toolbox = new Toolbox([$clock], logger: logger()); |
| 32 | +$toolProcessor = new AgentProcessor($toolbox); |
| 33 | +$agent = new Agent($platform, 'claude-sonnet-4-5-20250929', [$toolProcessor], [$toolProcessor]); |
| 34 | + |
| 35 | +$messages = new MessageBag(Message::ofUser('What date and time is it?')); |
| 36 | +$result = $agent->call($messages, ['response_format' => [ |
| 37 | + 'type' => 'json_schema', |
| 38 | + 'json_schema' => [ |
| 39 | + 'name' => 'clock', |
| 40 | + 'strict' => true, |
| 41 | + 'schema' => [ |
| 42 | + 'type' => 'object', |
| 43 | + 'properties' => [ |
| 44 | + 'date' => ['type' => 'string', 'description' => 'The current date in the format YYYY-MM-DD.'], |
| 45 | + 'time' => ['type' => 'string', 'description' => 'The current time in the format HH:MM:SS.'], |
| 46 | + ], |
| 47 | + 'required' => ['date', 'time'], |
| 48 | + 'additionalProperties' => false, |
| 49 | + ], |
| 50 | + ], |
| 51 | +]]); |
| 52 | + |
| 53 | +dump($result->getContent()); |
0 commit comments