|
| 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 Doctrine\DBAL\DriverManager; |
| 13 | +use Doctrine\DBAL\Tools\DsnParser; |
| 14 | +use Symfony\AI\Agent\Agent; |
| 15 | +use Symfony\AI\Agent\Memory\EmbeddingProvider; |
| 16 | +use Symfony\AI\Agent\Memory\MemoryInputProcessor; |
| 17 | +use Symfony\AI\Platform\Bridge\OpenAI\Embeddings; |
| 18 | +use Symfony\AI\Platform\Bridge\OpenAI\GPT; |
| 19 | +use Symfony\AI\Platform\Bridge\OpenAI\PlatformFactory; |
| 20 | +use Symfony\AI\Platform\Message\Message; |
| 21 | +use Symfony\AI\Platform\Message\MessageBag; |
| 22 | +use Symfony\AI\Store\Bridge\MariaDB\Store; |
| 23 | +use Symfony\AI\Store\Document\Metadata; |
| 24 | +use Symfony\AI\Store\Document\TextDocument; |
| 25 | +use Symfony\AI\Store\Document\Vectorizer; |
| 26 | +use Symfony\AI\Store\Indexer; |
| 27 | +use Symfony\Component\Dotenv\Dotenv; |
| 28 | +use Symfony\Component\Uid\Uuid; |
| 29 | + |
| 30 | +require_once dirname(__DIR__).'/vendor/autoload.php'; |
| 31 | +(new Dotenv())->loadEnv(dirname(__DIR__).'/.env'); |
| 32 | + |
| 33 | +if (!$_ENV['OPENAI_API_KEY'] || !$_ENV['MARIADB_URI']) { |
| 34 | + echo 'Please set OPENAI_API_KEY and MARIADB_URI environment variables.'.\PHP_EOL; |
| 35 | + exit(1); |
| 36 | +} |
| 37 | + |
| 38 | +// initialize the store |
| 39 | +$store = Store::fromDbal( |
| 40 | + connection: DriverManager::getConnection((new DsnParser())->parse($_ENV['MARIADB_URI'])), |
| 41 | + tableName: 'my_table', |
| 42 | + indexName: 'my_index', |
| 43 | + vectorFieldName: 'embedding', |
| 44 | +); |
| 45 | + |
| 46 | +// our data |
| 47 | +$pastConversationPieces = [ |
| 48 | + ['role' => 'user', 'timestamp' => '2024-12-14 12:00:00', 'content' => 'My friends John and Emma are friends, too, are there hints why?'], |
| 49 | + ['role' => 'assistant', 'timestamp' => '2024-12-14 12:00:01', 'content' => 'Based on the found documents i would expect they are friends since childhood, this can give a deep bound!'], |
| 50 | + ['role' => 'user', 'timestamp' => '2024-12-14 12:02:02', 'content' => 'Yeah but how does this bound? I know John was once there with a wound dressing as Emma fell, could this be a hint?'], |
| 51 | + ['role' => 'assistant', 'timestamp' => '2024-12-14 12:02:03', 'content' => 'Yes, this could be a hint that they have been through difficult times together, which can strengthen their bond.'], |
| 52 | +]; |
| 53 | + |
| 54 | +// create embeddings and documents |
| 55 | +foreach ($pastConversationPieces as $i => $message) { |
| 56 | + $documents[] = new TextDocument( |
| 57 | + id: Uuid::v4(), |
| 58 | + content: 'Role: '.$message['role'].\PHP_EOL.'Timestamp: '.$message['timestamp'].\PHP_EOL.'Message: '.$message['content'], |
| 59 | + metadata: new Metadata($message), |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +// initialize the table |
| 64 | +$store->initialize(); |
| 65 | + |
| 66 | +// create embeddings for documents as preparation of the chain memory |
| 67 | +$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']); |
| 68 | +$vectorizer = new Vectorizer($platform, $embeddings = new Embeddings()); |
| 69 | +$indexer = new Indexer($vectorizer, $store); |
| 70 | +$indexer->index($documents); |
| 71 | + |
| 72 | +// Execute a chat call that is utilizing the memory |
| 73 | +$embeddingsMemory = new EmbeddingProvider($platform, $embeddings, $store); |
| 74 | +$memoryProcessor = new MemoryInputProcessor($embeddingsMemory); |
| 75 | + |
| 76 | +$chain = new Agent($platform, new GPT(GPT::GPT_4O_MINI), [$memoryProcessor]); |
| 77 | +$messages = new MessageBag(Message::ofUser('Have we discussed about my friend John in the past? If yes, what did we talk about?')); |
| 78 | +$response = $chain->call($messages); |
| 79 | + |
| 80 | +echo $response->getContent().\PHP_EOL; |
0 commit comments