Files
archived-ai/examples/chat/persistent-chat-cache.php
2026-02-03 07:21:58 +01:00

39 lines
1.1 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Symfony\AI\Agent\Agent;
use Symfony\AI\Chat\Bridge\Cache\MessageStore as CacheStore;
use Symfony\AI\Chat\Chat;
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
require_once dirname(__DIR__).'/bootstrap.php';
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client());
$store = new CacheStore(new ArrayAdapter(), 'chat');
$store->setup();
$agent = new Agent($platform, 'gpt-5-mini');
$chat = new Chat($agent, $store);
$messages = new MessageBag(
Message::forSystem('You are a helpful assistant. You only answer with short sentences.'),
);
$chat->initiate($messages);
$chat->submit(Message::ofUser('My name is Christopher.'));
$message = $chat->submit(Message::ofUser('What is my name?'));
echo $message->getContent().\PHP_EOL;