|
| 1 | +<?php |
| 2 | + |
| 3 | +use MongoDB\Client as MongoDBClient; |
| 4 | +use PhpLlm\LlmChain\Chain; |
| 5 | +use PhpLlm\LlmChain\Document\Document; |
| 6 | +use PhpLlm\LlmChain\Document\Metadata; |
| 7 | +use PhpLlm\LlmChain\DocumentEmbedder; |
| 8 | +use PhpLlm\LlmChain\Message\Message; |
| 9 | +use PhpLlm\LlmChain\Message\MessageBag; |
| 10 | +use PhpLlm\LlmChain\OpenAI\Model\Embeddings; |
| 11 | +use PhpLlm\LlmChain\OpenAI\Model\Gpt; |
| 12 | +use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version; |
| 13 | +use PhpLlm\LlmChain\OpenAI\Platform\OpenAI; |
| 14 | +use PhpLlm\LlmChain\Store\MongoDB\Store; |
| 15 | +use PhpLlm\LlmChain\ToolBox\ChainProcessor; |
| 16 | +use PhpLlm\LlmChain\ToolBox\Tool\SimilaritySearch; |
| 17 | +use PhpLlm\LlmChain\ToolBox\ToolAnalyzer; |
| 18 | +use PhpLlm\LlmChain\ToolBox\ToolBox; |
| 19 | +use Symfony\Component\Dotenv\Dotenv; |
| 20 | +use Symfony\Component\HttpClient\HttpClient; |
| 21 | +use Symfony\Component\Uid\Uuid; |
| 22 | + |
| 23 | +require_once dirname(__DIR__).'/vendor/autoload.php'; |
| 24 | +(new Dotenv())->loadEnv(dirname(__DIR__).'/.env'); |
| 25 | + |
| 26 | +if (empty($_ENV['OPENAI_API_KEY']) || empty($_ENV['MONGODB_URI'])) { |
| 27 | + echo 'Please set OPENAI_API_KEY and MONGODB_URI environment variables.'.PHP_EOL; |
| 28 | + exit(1); |
| 29 | +} |
| 30 | + |
| 31 | +// initialize the store |
| 32 | +$store = new Store( |
| 33 | + client: new MongoDBClient($_ENV['MONGODB_URI']), |
| 34 | + databaseName: 'my-database', |
| 35 | + collectionName: 'my-collection', |
| 36 | + indexName: 'my-index', |
| 37 | + vectorFieldName: 'vector', |
| 38 | +); |
| 39 | + |
| 40 | +// our data |
| 41 | +$movies = [ |
| 42 | + ['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.', 'regisseur' => 'Christopher Nolan'], |
| 43 | + ['title' => 'The Matrix', 'description' => 'A hacker discovers the world he lives in is a simulated reality and joins a rebellion to overthrow its controllers.', 'regisseur' => 'The Wachowskis'], |
| 44 | + ['title' => 'The Godfather', 'description' => 'The aging patriarch of an organized crime dynasty transfers control of his empire to his reluctant son.', 'regisseur' => 'Francis Ford Coppola'], |
| 45 | +]; |
| 46 | + |
| 47 | +// create embeddings and documents |
| 48 | +foreach ($movies as $movie) { |
| 49 | + $documents[] = Document::fromText( |
| 50 | + id: Uuid::v4(), |
| 51 | + text: $movie['title'].' '.$movie['description'], |
| 52 | + metadata: new Metadata($movie), |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +// create embeddings for documents |
| 57 | +$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']); |
| 58 | +$embedder = new DocumentEmbedder($embeddings = new Embeddings($platform), $store); |
| 59 | +$embedder->embed($documents); |
| 60 | + |
| 61 | +// initialize the index |
| 62 | +$store->initialize(); |
| 63 | + |
| 64 | +$llm = new Gpt($platform, Version::gpt4oMini()); |
| 65 | + |
| 66 | +$similaritySearch = new SimilaritySearch($embeddings, $store); |
| 67 | +$toolBox = new ToolBox(new ToolAnalyzer(), [$similaritySearch]); |
| 68 | +$processor = new ChainProcessor($toolBox); |
| 69 | +$chain = new Chain($llm, [$processor], [$processor]); |
| 70 | + |
| 71 | +$messages = new MessageBag( |
| 72 | + Message::forSystem('Please answer all user questions only using SimilaritySearch function.'), |
| 73 | + Message::ofUser('Which movie fits the theme of the mafia?') |
| 74 | +); |
| 75 | +$response = $chain->call($messages); |
| 76 | + |
| 77 | +echo $response.PHP_EOL; |
0 commit comments