|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\Platform\Bridge\Perplexity\Perplexity; |
| 14 | +use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory; |
| 15 | +use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor; |
| 16 | +use Symfony\AI\Platform\Bridge\Perplexity\TokenOutputProcessor; |
| 17 | +use Symfony\AI\Platform\Message\Content\ImageUrl; |
| 18 | +use Symfony\AI\Platform\Message\Message; |
| 19 | +use Symfony\AI\Platform\Message\MessageBag; |
| 20 | + |
| 21 | +require_once dirname(__DIR__).'/bootstrap.php'; |
| 22 | + |
| 23 | +$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client()); |
| 24 | +$model = new Perplexity(); |
| 25 | +$agent = new Agent($platform, $model, outputProcessors: [new TokenOutputProcessor(), new SearchResultProcessor()], logger: logger()); |
| 26 | + |
| 27 | +$messages = new MessageBag( |
| 28 | + Message::forSystem('You are an image analyzer bot that helps identify the content of images.'), |
| 29 | + Message::ofUser( |
| 30 | + 'Describe the image as a comedian would do it.', |
| 31 | + new ImageUrl('https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Webysther_20160423_-_Elephpant.svg/350px-Webysther_20160423_-_Elephpant.svg.png'), |
| 32 | + ), |
| 33 | +); |
| 34 | +$result = $agent->call($messages); |
| 35 | + |
| 36 | +echo $result->getContent().\PHP_EOL; |
| 37 | + |
| 38 | +$metadata = $result->getMetadata(); |
| 39 | +if ($metadata->has('search_results')) { |
| 40 | + echo 'Search results:'.\PHP_EOL; |
| 41 | + if (0 === count($metadata->get('search_results'))) { |
| 42 | + echo 'No search results.'.\PHP_EOL; |
| 43 | + |
| 44 | + return; |
| 45 | + } |
| 46 | + foreach ($metadata->get('search_results') as $i => $searchResult) { |
| 47 | + echo 'Result #'.($i + 1).':'.\PHP_EOL; |
| 48 | + echo $searchResult['title'].\PHP_EOL; |
| 49 | + echo $searchResult['url'].\PHP_EOL; |
| 50 | + echo $searchResult['date'].\PHP_EOL; |
| 51 | + echo $searchResult['last_updated'] ? $searchResult['last_updated'].\PHP_EOL : ''; |
| 52 | + echo $searchResult['snippet'] ? $searchResult['snippet'].\PHP_EOL : ''; |
| 53 | + echo \PHP_EOL; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +if ($metadata->has('citations')) { |
| 58 | + echo 'Citations:'.\PHP_EOL; |
| 59 | + if (0 === count($metadata->get('citations'))) { |
| 60 | + echo 'No citations.'.\PHP_EOL; |
| 61 | + |
| 62 | + return; |
| 63 | + } |
| 64 | + foreach ($metadata->get('citations') as $i => $citation) { |
| 65 | + echo 'Citation #'.($i + 1).':'.\PHP_EOL; |
| 66 | + echo $citation.\PHP_EOL; |
| 67 | + echo \PHP_EOL; |
| 68 | + } |
| 69 | +} |
0 commit comments