|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\StructuredOutput\AgentProcessor as StructuredOutputProcessor; |
| 14 | +use Symfony\AI\Agent\Toolbox\AgentProcessor as ToolProcessor; |
| 15 | +use Symfony\AI\Agent\Toolbox\Tool\Clock; |
| 16 | +use Symfony\AI\Agent\Toolbox\Toolbox; |
| 17 | +use Symfony\AI\Platform\Bridge\Google\Gemini; |
| 18 | +use Symfony\AI\Platform\Bridge\Google\PlatformFactory; |
| 19 | +use Symfony\AI\Platform\Message\Message; |
| 20 | +use Symfony\AI\Platform\Message\MessageBag; |
| 21 | +use Symfony\Component\Clock\Clock as SymfonyClock; |
| 22 | +use Symfony\Component\Dotenv\Dotenv; |
| 23 | + |
| 24 | +require_once dirname(__DIR__, 2).'/vendor/autoload.php'; |
| 25 | +(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env'); |
| 26 | + |
| 27 | +if (empty($_ENV['GOOGLE_API_KEY'])) { |
| 28 | + echo 'Please set the GOOGLE_API_KEY environment variable.'.\PHP_EOL; |
| 29 | + exit(1); |
| 30 | +} |
| 31 | + |
| 32 | +$platform = PlatformFactory::create($_ENV['GOOGLE_API_KEY']); |
| 33 | +$model = new Gemini(Gemini::GEMINI_1_5_FLASH); |
| 34 | + |
| 35 | +$clock = new Clock(new SymfonyClock()); |
| 36 | +$toolbox = Toolbox::create($clock); |
| 37 | +$toolProcessor = new ToolProcessor($toolbox); |
| 38 | +$structuredOutputProcessor = new StructuredOutputProcessor(); |
| 39 | +$agent = new Agent($platform, $model, [$toolProcessor, $structuredOutputProcessor], [$toolProcessor, $structuredOutputProcessor]); |
| 40 | + |
| 41 | +$messages = new MessageBag(Message::ofUser('What date and time is it?')); |
| 42 | +$response = $agent->call($messages, ['response_format' => [ |
| 43 | + 'type' => 'json_schema', |
| 44 | + 'json_schema' => [ |
| 45 | + 'name' => 'clock', |
| 46 | + 'strict' => true, |
| 47 | + 'schema' => [ |
| 48 | + 'type' => 'object', |
| 49 | + 'properties' => [ |
| 50 | + 'date' => ['type' => 'string', 'description' => 'The current date in the format YYYY-MM-DD.'], |
| 51 | + 'time' => ['type' => 'string', 'description' => 'The current time in the format HH:MM:SS.'], |
| 52 | + ], |
| 53 | + 'required' => ['date', 'time'], |
| 54 | + 'additionalProperties' => false, |
| 55 | + ], |
| 56 | + ], |
| 57 | +]]); |
| 58 | + |
| 59 | +dump($response->getContent()); |
0 commit comments