|
| 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\Chat; |
| 14 | +use Symfony\AI\Agent\Chat\MessageStore\InMemoryStore; |
| 15 | +use Symfony\AI\Platform\Bridge\OpenAI\GPT; |
| 16 | +use Symfony\AI\Platform\Bridge\OpenAI\PlatformFactory; |
| 17 | +use Symfony\AI\Platform\Message\Message; |
| 18 | +use Symfony\AI\Platform\Message\MessageBag; |
| 19 | +use Symfony\Component\Dotenv\Dotenv; |
| 20 | + |
| 21 | +require_once dirname(__DIR__).'/vendor/autoload.php'; |
| 22 | +(new Dotenv())->loadEnv(dirname(__DIR__).'/.env'); |
| 23 | + |
| 24 | +if (empty($_ENV['OPENAI_API_KEY'])) { |
| 25 | + echo 'Please set the OPENAI_API_KEY environment variable.'.\PHP_EOL; |
| 26 | + exit(1); |
| 27 | +} |
| 28 | + |
| 29 | +$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']); |
| 30 | +$llm = new GPT(GPT::GPT_4O_MINI); |
| 31 | + |
| 32 | +$agent = new Agent($platform, $llm); |
| 33 | +$chat = new Chat($agent, new InMemoryStore()); |
| 34 | + |
| 35 | +$messages = new MessageBag( |
| 36 | + Message::forSystem('You are a helpful assistant. You only answer with short sentences.'), |
| 37 | +); |
| 38 | + |
| 39 | +$chat->initiate($messages); |
| 40 | +$chat->submit(Message::ofUser('My name is Christopher.')); |
| 41 | +$message = $chat->submit(Message::ofUser('What is my name?')); |
| 42 | + |
| 43 | +echo $message->content.\PHP_EOL; |
0 commit comments