|
| 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\PlatformFactory; |
| 19 | +use Symfony\AI\Platform\Message\Message; |
| 20 | +use Symfony\AI\Platform\Message\MessageBag; |
| 21 | +use Symfony\AI\Store\Bridge\Redis\Store; |
| 22 | +use Symfony\AI\Store\Document\Loader\InMemoryLoader; |
| 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\Uid\Uuid; |
| 28 | + |
| 29 | +require_once dirname(__DIR__).'/bootstrap.php'; |
| 30 | + |
| 31 | +// initialize the store |
| 32 | +$redis = new Redis([ |
| 33 | + 'host' => 'localhost', |
| 34 | + 'port' => 6379, |
| 35 | +]); |
| 36 | +$store = new Store( |
| 37 | + redis: $redis, |
| 38 | + indexName: 'my_index', |
| 39 | +); |
| 40 | + |
| 41 | +// create embeddings and documents |
| 42 | +$documents = []; |
| 43 | +foreach (Movies::all() as $i => $movie) { |
| 44 | + $documents[] = new TextDocument( |
| 45 | + id: Uuid::v4(), |
| 46 | + content: 'Title: '.$movie['title'].\PHP_EOL.'Director: '.$movie['director'].\PHP_EOL.'Description: '.$movie['description'], |
| 47 | + metadata: new Metadata($movie), |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +// initialize the table |
| 52 | +$store->setup(['vector_size' => 1536]); |
| 53 | + |
| 54 | +// create embeddings for documents |
| 55 | +$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client()); |
| 56 | +$vectorizer = new Vectorizer($platform, 'text-embedding-3-small', logger()); |
| 57 | +$indexer = new Indexer(new InMemoryLoader($documents), $vectorizer, $store, logger: logger()); |
| 58 | +$indexer->index($documents); |
| 59 | + |
| 60 | +$similaritySearch = new SimilaritySearch($vectorizer, $store); |
| 61 | +$toolbox = new Toolbox([$similaritySearch], logger: logger()); |
| 62 | +$processor = new AgentProcessor($toolbox); |
| 63 | +$agent = new Agent($platform, 'gpt-4o-mini', [$processor], [$processor], logger: logger()); |
| 64 | + |
| 65 | +$messages = new MessageBag( |
| 66 | + Message::forSystem('Please answer all user questions only using SimilaritySearch function.'), |
| 67 | + Message::ofUser('Which movie fits the theme of technology?') |
| 68 | +); |
| 69 | +$result = $agent->call($messages); |
| 70 | + |
| 71 | +echo $result->getContent().\PHP_EOL; |
| 72 | + |
| 73 | +$store->drop(); |
0 commit comments