|
| 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\Chat\Bridge\Redis\MessageStore; |
| 14 | +use Symfony\AI\Chat\Chat; |
| 15 | +use Symfony\AI\Chat\MessageNormalizer; |
| 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\Serializer\Encoder\JsonEncoder; |
| 20 | +use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; |
| 21 | +use Symfony\Component\Serializer\Serializer; |
| 22 | + |
| 23 | +require_once dirname(__DIR__).'/bootstrap.php'; |
| 24 | + |
| 25 | +$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client()); |
| 26 | + |
| 27 | +$redis = new Redis([ |
| 28 | + 'host' => env('REDIS_HOST'), |
| 29 | + 'port' => 6379, |
| 30 | +]); |
| 31 | + |
| 32 | +$store = new MessageStore($redis, 'symfony', new Serializer([ |
| 33 | + new ArrayDenormalizer(), |
| 34 | + new MessageNormalizer(), |
| 35 | +], [ |
| 36 | + new JsonEncoder(), |
| 37 | +])); |
| 38 | +$store->setup(); |
| 39 | + |
| 40 | +$agent = new Agent($platform, 'gpt-4o-mini'); |
| 41 | +$chat = new Chat($agent, $store); |
| 42 | + |
| 43 | +$messages = new MessageBag( |
| 44 | + Message::forSystem('You are a helpful assistant. You only answer with short sentences.'), |
| 45 | +); |
| 46 | + |
| 47 | +$chat->initiate($messages); |
| 48 | +$chat->submit(Message::ofUser('My name is Christopher.')); |
| 49 | +$message = $chat->submit(Message::ofUser('What is my name?')); |
| 50 | + |
| 51 | +echo $message->getContent().\PHP_EOL; |
0 commit comments