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