|
| 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 PhpLlm\LlmChain\Chain\Chain; |
| 15 | +use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor; |
| 16 | +use PhpLlm\LlmChain\Chain\Toolbox\Tool\SimilaritySearch; |
| 17 | +use PhpLlm\LlmChain\Chain\Toolbox\Toolbox; |
| 18 | +use PhpLlm\LlmChain\Platform\Bridge\Google\Embeddings; |
| 19 | +use PhpLlm\LlmChain\Platform\Bridge\Google\Embeddings\TaskType; |
| 20 | +use PhpLlm\LlmChain\Platform\Bridge\Google\Gemini; |
| 21 | +use PhpLlm\LlmChain\Platform\Bridge\Google\PlatformFactory; |
| 22 | +use PhpLlm\LlmChain\Platform\Message\Message; |
| 23 | +use PhpLlm\LlmChain\Platform\Message\MessageBag; |
| 24 | +use PhpLlm\LlmChain\Store\Bridge\MariaDB\Store; |
| 25 | +use PhpLlm\LlmChain\Store\Document\Metadata; |
| 26 | +use PhpLlm\LlmChain\Store\Document\TextDocument; |
| 27 | +use PhpLlm\LlmChain\Store\Indexer; |
| 28 | +use Symfony\Component\Dotenv\Dotenv; |
| 29 | +use Symfony\Component\Uid\Uuid; |
| 30 | + |
| 31 | +require_once dirname(__DIR__, 2).'/vendor/autoload.php'; |
| 32 | +(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env'); |
| 33 | + |
| 34 | +if (empty($_ENV['GOOGLE_API_KEY']) || empty($_ENV['MARIADB_URI'])) { |
| 35 | + echo 'Please set GOOGLE_API_KEY and MARIADB_URI environment variables.'.\PHP_EOL; |
| 36 | + exit(1); |
| 37 | +} |
| 38 | + |
| 39 | +// initialize the store |
| 40 | +$store = Store::fromDbal( |
| 41 | + connection: DriverManager::getConnection((new DsnParser())->parse($_ENV['MARIADB_URI'])), |
| 42 | + tableName: 'my_table', |
| 43 | + indexName: 'my_index', |
| 44 | + vectorFieldName: 'embedding', |
| 45 | +); |
| 46 | + |
| 47 | +// our data |
| 48 | +$movies = [ |
| 49 | + ['title' => 'Inception', 'description' => 'A skilled thief is given a chance at redemption if he can successfully perform inception, the act of planting an idea in someone\'s subconscious.', 'director' => 'Christopher Nolan'], |
| 50 | + ['title' => 'The Matrix', 'description' => 'A hacker discovers the world he lives in is a simulated reality and joins a rebellion to overthrow its controllers.', 'director' => 'The Wachowskis'], |
| 51 | + ['title' => 'The Godfather', 'description' => 'The aging patriarch of an organized crime dynasty transfers control of his empire to his reluctant son.', 'director' => 'Francis Ford Coppola'], |
| 52 | +]; |
| 53 | + |
| 54 | +// create embeddings and documents |
| 55 | +foreach ($movies as $i => $movie) { |
| 56 | + $documents[] = new TextDocument( |
| 57 | + id: Uuid::v4(), |
| 58 | + content: 'Title: '.$movie['title'].\PHP_EOL.'Director: '.$movie['director'].\PHP_EOL.'Description: '.$movie['description'], |
| 59 | + metadata: new Metadata($movie), |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +// initialize the table |
| 64 | +$store->initialize(['dimensions' => 768]); |
| 65 | + |
| 66 | +// create embeddings for documents |
| 67 | +$platform = PlatformFactory::create($_ENV['GOOGLE_API_KEY']); |
| 68 | +$embeddings = new Embeddings(options: ['dimensions' => 768, 'task_type' => TaskType::SemanticSimilarity]); |
| 69 | +$indexer = new Indexer($platform, $embeddings, $store); |
| 70 | +$indexer->index($documents); |
| 71 | + |
| 72 | +$model = new Gemini(Gemini::GEMINI_2_FLASH_LITE); |
| 73 | + |
| 74 | +$similaritySearch = new SimilaritySearch($platform, $embeddings, $store); |
| 75 | +$toolbox = Toolbox::create($similaritySearch); |
| 76 | +$processor = new ChainProcessor($toolbox); |
| 77 | +$chain = new Chain($platform, $model, [$processor], [$processor]); |
| 78 | + |
| 79 | +$messages = new MessageBag( |
| 80 | + Message::forSystem('Please answer all user questions only using SimilaritySearch function.'), |
| 81 | + Message::ofUser('Which movie fits the theme of the mafia?') |
| 82 | +); |
| 83 | +$response = $chain->call($messages); |
| 84 | + |
| 85 | +echo $response->getContent().\PHP_EOL; |
0 commit comments