|
| 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\Agent\Toolbox\AgentProcessor; |
| 14 | +use Symfony\AI\Agent\Toolbox\Tool\SimilaritySearch; |
| 15 | +use Symfony\AI\Agent\Toolbox\Toolbox; |
| 16 | +use Symfony\AI\Fixtures\Movies; |
| 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\Neo4j\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\HttpClient\HttpClient; |
| 28 | +use Symfony\Component\Uid\Uuid; |
| 29 | + |
| 30 | +require_once dirname(__DIR__).'/bootstrap.php'; |
| 31 | + |
| 32 | +// initialize the store |
| 33 | +$store = new Store( |
| 34 | + httpClient: HttpClient::create(), |
| 35 | + endpointUrl: env('NEO4J_HOST'), |
| 36 | + username: env('NEO4J_USERNAME'), |
| 37 | + password: env('NEO4J_PASSWORD'), |
| 38 | + databaseName: env('NEO4J_DATABASE'), |
| 39 | + vectorIndexName: 'Movies', |
| 40 | + nodeName: 'movies', |
| 41 | +); |
| 42 | + |
| 43 | +// initialize the table |
| 44 | +$store->initialize(); |
| 45 | + |
| 46 | +// create embeddings and documents |
| 47 | +$documents = []; |
| 48 | +foreach (Movies::all() as $i => $movie) { |
| 49 | + $documents[] = new TextDocument( |
| 50 | + id: Uuid::v4(), |
| 51 | + content: 'Title: '.$movie['title'].\PHP_EOL.'Director: '.$movie['director'].\PHP_EOL.'Description: '.$movie['description'], |
| 52 | + metadata: new Metadata($movie), |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +// create embeddings for documents |
| 57 | +$platform = PlatformFactory::create($_SERVER['OPENAI_API_KEY']); |
| 58 | +$vectorizer = new Vectorizer($platform, $embeddings = new Embeddings()); |
| 59 | +$indexer = new Indexer($vectorizer, $store); |
| 60 | +$indexer->index($documents); |
| 61 | + |
| 62 | +$model = new GPT(GPT::GPT_4O_MINI); |
| 63 | + |
| 64 | +$similaritySearch = new SimilaritySearch($platform, $embeddings, $store); |
| 65 | +$toolbox = new Toolbox([$similaritySearch], logger: logger()); |
| 66 | +$processor = new AgentProcessor($toolbox); |
| 67 | +$agent = new Agent($platform, $model, [$processor], [$processor]); |
| 68 | + |
| 69 | +$messages = new MessageBag( |
| 70 | + Message::forSystem('Please answer all user questions only using SimilaritySearch function.'), |
| 71 | + Message::ofUser('Which movie fits the theme of technology?') |
| 72 | +); |
| 73 | +$response = $agent->call($messages); |
| 74 | + |
| 75 | +echo $response->getContent().\PHP_EOL; |
0 commit comments