Skip to content

Commit d971ac0

Browse files
committed
Add Perplexity Platform
1 parent a29a7b2 commit d971ac0

16 files changed

+764
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\Message;
18+
use Symfony\AI\Platform\Message\MessageBag;
19+
20+
require_once dirname(__DIR__).'/bootstrap.php';
21+
22+
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
23+
$model = new Perplexity();
24+
$agent = new Agent($platform, $model, outputProcessors: [new TokenOutputProcessor(), new SearchResultProcessor()], logger: logger());
25+
26+
$messages = new MessageBag(Message::ofUser('What is the best French cheese of the first quarter-century of 21st century?'));
27+
$response = $agent->call($messages, [
28+
'search_mode' => 'academic',
29+
'search_after_date_filter' => '01/01/2000',
30+
'search_before_date_filter' => '01/01/2025',
31+
]);
32+
33+
echo $response->getContent().\PHP_EOL;
34+
echo \PHP_EOL;
35+
36+
$metadata = $response->getMetadata();
37+
if ($metadata->has('search_results')) {
38+
echo 'Search results:'.\PHP_EOL;
39+
if (0 === count($metadata->get('search_results'))) {
40+
echo 'No search results.'.\PHP_EOL;
41+
42+
return;
43+
}
44+
foreach ($metadata->get('search_results') as $i => $searchResult) {
45+
echo 'Result #'.($i + 1).':'.\PHP_EOL;
46+
echo $searchResult['title'].\PHP_EOL;
47+
echo $searchResult['url'].\PHP_EOL;
48+
echo $searchResult['date'].\PHP_EOL;
49+
echo $searchResult['last_updated'] ? $searchResult['last_updated'].\PHP_EOL : '';
50+
echo $searchResult['snippet'] ? $searchResult['snippet'].\PHP_EOL : '';
51+
echo \PHP_EOL;
52+
}
53+
}
54+
55+
if ($metadata->has('citations')) {
56+
echo 'Citations:'.\PHP_EOL;
57+
if (0 === count($metadata->get('citations'))) {
58+
echo 'No citations.'.\PHP_EOL;
59+
60+
return;
61+
}
62+
foreach ($metadata->get('citations') as $i => $citation) {
63+
echo 'Citation #'.($i + 1).':'.\PHP_EOL;
64+
echo $citation.\PHP_EOL;
65+
echo \PHP_EOL;
66+
}
67+
}

examples/perplexity/chat.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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\Message\Message;
16+
use Symfony\AI\Platform\Message\MessageBag;
17+
18+
require_once dirname(__DIR__).'/bootstrap.php';
19+
20+
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
21+
$model = new Perplexity();
22+
$agent = new Agent($platform, $model);
23+
24+
$messages = new MessageBag(Message::ofUser('What is the best French cheese?'));
25+
$response = $agent->call($messages);
26+
27+
echo $response->getContent().\PHP_EOL;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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\Message\Message;
16+
use Symfony\AI\Platform\Message\MessageBag;
17+
18+
require_once dirname(__DIR__).'/bootstrap.php';
19+
20+
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
21+
$model = new Perplexity();
22+
$agent = new Agent($platform, $model, logger: logger());
23+
24+
$messages = new MessageBag(Message::ofUser('What is 2 + 2?'));
25+
$response = $agent->call($messages, [
26+
'disable_search' => true,
27+
]);
28+
29+
echo $response->getContent().\PHP_EOL;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\DocumentUrl;
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::ofUser(
29+
new DocumentUrl('https://upload.wikimedia.org/wikipedia/commons/2/20/Re_example.pdf'),
30+
'What is this document about?',
31+
),
32+
);
33+
$result = $agent->call($messages);
34+
35+
echo $result->getContent().\PHP_EOL;
36+
$result = $agent->call($messages);
37+
38+
echo $result->getContent().\PHP_EOL;
39+
40+
$metadata = $result->getMetadata();
41+
if ($metadata->has('search_results')) {
42+
echo 'Search results:'.\PHP_EOL;
43+
if (0 === count($metadata->get('search_results'))) {
44+
echo 'No search results.'.\PHP_EOL;
45+
46+
return;
47+
}
48+
foreach ($metadata->get('search_results') as $i => $searchResult) {
49+
echo 'Result #'.($i + 1).':'.\PHP_EOL;
50+
echo $searchResult['title'].\PHP_EOL;
51+
echo $searchResult['url'].\PHP_EOL;
52+
echo $searchResult['date'].\PHP_EOL;
53+
echo $searchResult['last_updated'] ? $searchResult['last_updated'].\PHP_EOL : '';
54+
echo $searchResult['snippet'] ? $searchResult['snippet'].\PHP_EOL : '';
55+
echo \PHP_EOL;
56+
}
57+
}
58+
59+
if ($metadata->has('citations')) {
60+
echo 'Citations:'.\PHP_EOL;
61+
if (0 === count($metadata->get('citations'))) {
62+
echo 'No citations.'.\PHP_EOL;
63+
64+
return;
65+
}
66+
foreach ($metadata->get('citations') as $i => $citation) {
67+
echo 'Citation #'.($i + 1).':'.\PHP_EOL;
68+
echo $citation.\PHP_EOL;
69+
echo \PHP_EOL;
70+
}
71+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\Message;
18+
use Symfony\AI\Platform\Message\MessageBag;
19+
use Symfony\AI\Platform\Metadata\TokenUsage;
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 a pirate and you write funny.'),
29+
Message::ofUser('What is the Symfony framework?'),
30+
);
31+
$result = $agent->call($messages, [
32+
'model' => Perplexity::SONAR_DEEP_RESEARCH,
33+
'max_tokens' => 500, // specific options just for this call
34+
]);
35+
36+
$metadata = $result->getMetadata();
37+
$tokenUsage = $metadata->get('token_usage');
38+
39+
assert($tokenUsage instanceof TokenUsage);
40+
41+
print_token_usage($result->getMetadata());

examples/perplexity/web-search.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\Message;
18+
use Symfony\AI\Platform\Message\MessageBag;
19+
20+
require_once dirname(__DIR__).'/bootstrap.php';
21+
22+
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
23+
$model = new Perplexity();
24+
$agent = new Agent($platform, $model, outputProcessors: [new TokenOutputProcessor(), new SearchResultProcessor()], logger: logger());
25+
26+
$messages = new MessageBag(Message::ofUser('What is the best French cheese?'));
27+
$response = $agent->call($messages, [
28+
'search_domain_filter' => [
29+
'https://en.wikipedia.org/wiki/Cheese',
30+
],
31+
'search_mode' => 'web',
32+
'enable_search_classifier' => true,
33+
'search_recency_filter' => 'week',
34+
]);
35+
36+
echo $response->getContent().\PHP_EOL;
37+
echo \PHP_EOL;

src/platform/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ CHANGELOG
2222
- TransformersPHP (local PHP-based transformer models)
2323
- LM Studio (local model hosting)
2424
- Cerebras (language models like Llama 4, Qwen 3, and more)
25+
- Perplexity (Sonar models)
2526
* Add comprehensive message system with role-based messaging:
2627
- `UserMessage` for user inputs with multi-modal content
2728
- `SystemMessage` for system instructions

0 commit comments

Comments
 (0)