Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions examples/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,53 @@ function print_vectors(ResultPromise $result): void

echo 'Dimensions: '.$result->asVectors()[0]->getDimensions().\PHP_EOL;
}

function perplexity_print_search_results(Metadata $metadata): void
{
$searchResults = $metadata->get('search_results');

if (null === $searchResults) {
return;
}

echo 'Search results:'.\PHP_EOL;

if (0 === count($searchResults)) {
echo 'No search results.'.\PHP_EOL;

return;
}

foreach ($searchResults as $i => $searchResult) {
echo 'Result #'.($i + 1).':'.\PHP_EOL;
echo $searchResult['title'].\PHP_EOL;
echo $searchResult['url'].\PHP_EOL;
echo $searchResult['date'].\PHP_EOL;
echo $searchResult['last_updated'] ? $searchResult['last_updated'].\PHP_EOL : '';
echo $searchResult['snippet'] ? $searchResult['snippet'].\PHP_EOL : '';
echo \PHP_EOL;
}
}

function perplexity_print_citations(Metadata $metadata): void
{
$citations = $metadata->get('citations');

if (null === $citations) {
return;
}

echo 'Citations:'.\PHP_EOL;

if (0 === count($citations)) {
echo 'No citations.'.\PHP_EOL;

return;
}

foreach ($citations as $i => $citation) {
echo 'Citation #'.($i + 1).':'.\PHP_EOL;
echo $citation.\PHP_EOL;
echo \PHP_EOL;
}
}
36 changes: 36 additions & 0 deletions examples/perplexity/academic-search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\AI\Agent\Agent;
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
$model = new Perplexity();
$agent = new Agent($platform, $model, outputProcessors: [new SearchResultProcessor()], logger: logger());

$messages = new MessageBag(Message::ofUser('What is the best French cheese of the first quarter-century of 21st century?'));
$response = $agent->call($messages, [
'search_mode' => 'academic',
'search_after_date_filter' => '01/01/2000',
'search_before_date_filter' => '01/01/2025',
]);

echo $response->getContent().\PHP_EOL;
echo \PHP_EOL;

perplexity_print_search_results($response->getMetadata());
perplexity_print_citations($response->getMetadata());
27 changes: 27 additions & 0 deletions examples/perplexity/chat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\AI\Agent\Agent;
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
$model = new Perplexity();
$agent = new Agent($platform, $model);

$messages = new MessageBag(Message::ofUser('What is the best French cheese?'));
$response = $agent->call($messages);

echo $response->getContent().\PHP_EOL;
29 changes: 29 additions & 0 deletions examples/perplexity/disable-search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\AI\Agent\Agent;
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
$model = new Perplexity();
$agent = new Agent($platform, $model, logger: logger());

$messages = new MessageBag(Message::ofUser('What is 2 + 2?'));
$response = $agent->call($messages, [
'disable_search' => true,
]);

echo $response->getContent().\PHP_EOL;
38 changes: 38 additions & 0 deletions examples/perplexity/image-input-url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\AI\Agent\Agent;
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor;
use Symfony\AI\Platform\Message\Content\ImageUrl;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
$model = new Perplexity();
$agent = new Agent($platform, $model, outputProcessors: [new SearchResultProcessor()], logger: logger());

$messages = new MessageBag(
Message::forSystem('You are an image analyzer bot that helps identify the content of images.'),
Message::ofUser(
'Describe the image as a comedian would do it.',
new ImageUrl('https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Webysther_20160423_-_Elephpant.svg/350px-Webysther_20160423_-_Elephpant.svg.png'),
),
);
$result = $agent->call($messages);

echo $result->getContent().\PHP_EOL;

perplexity_print_search_results($result->getMetadata());
perplexity_print_citations($result->getMetadata());
40 changes: 40 additions & 0 deletions examples/perplexity/pdf-input-url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\AI\Agent\Agent;
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor;
use Symfony\AI\Platform\Message\Content\DocumentUrl;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
$model = new Perplexity();
$agent = new Agent($platform, $model, outputProcessors: [new SearchResultProcessor()], logger: logger());

$messages = new MessageBag(
Message::ofUser(
new DocumentUrl('https://upload.wikimedia.org/wikipedia/commons/2/20/Re_example.pdf'),
'What is this document about?',
),
);
$result = $agent->call($messages);

echo $result->getContent().\PHP_EOL;
$result = $agent->call($messages);

echo $result->getContent().\PHP_EOL;

perplexity_print_search_results($result->getMetadata());
perplexity_print_citations($result->getMetadata());
39 changes: 39 additions & 0 deletions examples/perplexity/stream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\AI\Agent\Agent;
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
$model = new Perplexity();
$agent = new Agent($platform, $model, outputProcessors: [new SearchResultProcessor()], logger: logger());

$messages = new MessageBag(
Message::forSystem('You are a thoughtful philosopher.'),
Message::ofUser('What is the purpose of an ant?'),
);
$result = $agent->call($messages, [
'stream' => true,
]);

foreach ($result->getContent() as $word) {
echo $word;
}
echo \PHP_EOL;

perplexity_print_search_results($result->getMetadata());
perplexity_print_citations($result->getMetadata());
34 changes: 34 additions & 0 deletions examples/perplexity/token-metadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\AI\Agent\Agent;
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
use Symfony\AI\Platform\Bridge\Perplexity\TokenOutputProcessor;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
$model = new Perplexity();
$agent = new Agent($platform, $model, outputProcessors: [new TokenOutputProcessor()], logger: logger());

$messages = new MessageBag(
Message::forSystem('You are a pirate and you write funny.'),
Message::ofUser('What is the Symfony framework?'),
);
$result = $agent->call($messages, [
'model' => Perplexity::SONAR_DEEP_RESEARCH,
'max_tokens' => 500, // specific options just for this call
]);

print_token_usage($result->getMetadata());
34 changes: 34 additions & 0 deletions examples/perplexity/web-search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\AI\Agent\Agent;
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
$model = new Perplexity();
$agent = new Agent($platform, $model, logger: logger());

$messages = new MessageBag(Message::ofUser('What is the best French cheese?'));
$response = $agent->call($messages, [
'search_domain_filter' => [
'https://en.wikipedia.org/wiki/Cheese',
],
'search_mode' => 'web',
'enable_search_classifier' => true,
'search_recency_filter' => 'week',
]);

echo $response->getContent().\PHP_EOL;
3 changes: 2 additions & 1 deletion src/platform/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CHANGELOG

* Add support for Albert API for French/EU data sovereignty
* Add unified abstraction layer for interacting with various AI models and providers
* Add support for 13+ AI providers:
* Add support for 15+ AI providers:
- OpenAI (GPT-4, GPT-3.5, DALL·E, Whisper)
- Anthropic (Claude models via native API and AWS Bedrock)
- Google (VertexAi and Gemini models with server-side tools support)
Expand All @@ -22,6 +22,7 @@ CHANGELOG
- TransformersPHP (local PHP-based transformer models)
- LM Studio (local model hosting)
- Cerebras (language models like Llama 4, Qwen 3, and more)
- Perplexity (Sonar models, supporting search results)
* Add comprehensive message system with role-based messaging:
- `UserMessage` for user inputs with multi-modal content
- `SystemMessage` for system instructions
Expand Down
Loading