|
| 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\Chat\Bridge\HttpFoundation\SessionStore; |
| 14 | +use Symfony\AI\Chat\Chat; |
| 15 | +use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory; |
| 16 | +use Symfony\AI\Platform\Message\Message; |
| 17 | +use Symfony\AI\Platform\Message\MessageBag; |
| 18 | +use Symfony\Component\HttpFoundation\Request; |
| 19 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 20 | +use Symfony\Component\HttpFoundation\Session\Session; |
| 21 | +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
| 22 | + |
| 23 | +require_once dirname(__DIR__).'/bootstrap.php'; |
| 24 | + |
| 25 | +$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client()); |
| 26 | + |
| 27 | +$request = Request::create('/'); |
| 28 | +$request->setSession(new Session(new MockArraySessionStorage())); |
| 29 | + |
| 30 | +$requestStack = new RequestStack(); |
| 31 | +$requestStack->push($request); |
| 32 | + |
| 33 | +$store = new SessionStore($requestStack, 'chat'); |
| 34 | +$store->setup(); |
| 35 | + |
| 36 | +$agent = new Agent($platform, 'gpt-4o-mini', logger: logger()); |
| 37 | +$chat = new Chat($agent, $store); |
| 38 | + |
| 39 | +$messages = new MessageBag( |
| 40 | + Message::forSystem('You are a helpful assistant. You only answer with short sentences.'), |
| 41 | +); |
| 42 | + |
| 43 | +$chat->initiate($messages); |
| 44 | +$chat->submit(Message::ofUser('My name is Christopher.')); |
| 45 | +$message = $chat->submit(Message::ofUser('What is my name?')); |
| 46 | + |
| 47 | +echo $message->content.\PHP_EOL; |
0 commit comments