Skip to content

Commit 8ee03e5

Browse files
committed
minor #1148 [Platform][Perplexity] Refactor extraction of perplexity metadata in favor of platform over agent (chr-hertel)
This PR was merged into the main branch. Discussion ---------- [Platform][Perplexity] Refactor extraction of perplexity metadata in favor of platform over agent | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | Issues | Fix # | License | MIT Finally getting rid of dependency violation Platform => Agent component 🙌 🥳 🎉 Commits ------- 457368b Refactor extraction of perplexity metadata in favor of platform over agent
2 parents 25982f8 + 457368b commit 8ee03e5

File tree

12 files changed

+95
-89
lines changed

12 files changed

+95
-89
lines changed

deptrac.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,3 @@ deptrac:
471471
WeaviateStore:
472472
- StoreComponent
473473
- PlatformComponent
474-
# Baseline of known violations to be skipped for now
475-
skip_violations:
476-
Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor: [Symfony\AI\Agent\OutputProcessorInterface, Symfony\AI\Agent\Output]

examples/bootstrap.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ function perplexity_print_search_results(Metadata $metadata): void
146146

147147
foreach ($searchResults as $i => $searchResult) {
148148
echo 'Result #'.($i + 1).':'.\PHP_EOL;
149-
echo $searchResult['title'].\PHP_EOL;
150-
echo $searchResult['url'].\PHP_EOL;
151-
echo $searchResult['date'].\PHP_EOL;
152-
echo $searchResult['last_updated'] ? $searchResult['last_updated'].\PHP_EOL : '';
153-
echo $searchResult['snippet'] ? $searchResult['snippet'].\PHP_EOL : '';
149+
echo isset($searchResult['title']) ? ' Title: '.$searchResult['title'].\PHP_EOL : '';
150+
echo isset($searchResult['url']) ? ' URL: '.$searchResult['url'].\PHP_EOL : '';
151+
echo isset($searchResult['date']) ? ' Date: '.$searchResult['date'].\PHP_EOL : '';
152+
echo isset($searchResult['last_updated']) ? ' Last Updated: '.$searchResult['last_updated'].\PHP_EOL : '';
153+
echo isset($searchResult['snippet']) ? ' Snippet: '.$searchResult['snippet'].\PHP_EOL : '';
154154
echo \PHP_EOL;
155155
}
156156
}
@@ -173,7 +173,7 @@ function perplexity_print_citations(Metadata $metadata): void
173173

174174
foreach ($citations as $i => $citation) {
175175
echo 'Citation #'.($i + 1).':'.\PHP_EOL;
176-
echo $citation.\PHP_EOL;
176+
echo ' '.$citation.\PHP_EOL;
177177
echo \PHP_EOL;
178178
}
179179
}

examples/perplexity/academic-search.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,23 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
use Symfony\AI\Agent\Agent;
1312
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
14-
use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor;
1513
use Symfony\AI\Platform\Message\Message;
1614
use Symfony\AI\Platform\Message\MessageBag;
1715

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

2018
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
21-
$agent = new Agent($platform, 'sonar', outputProcessors: [new SearchResultProcessor()]);
2219

2320
$messages = new MessageBag(Message::ofUser('What is the best French cheese of the first quarter-century of 21st century?'));
24-
$response = $agent->call($messages, [
21+
$result = $platform->invoke('sonar', $messages, [
2522
'search_mode' => 'academic',
2623
'search_after_date_filter' => '01/01/2000',
2724
'search_before_date_filter' => '01/01/2025',
2825
]);
2926

30-
echo $response->getContent().\PHP_EOL;
27+
echo $result->asText().\PHP_EOL;
3128
echo \PHP_EOL;
3229

33-
perplexity_print_search_results($response->getMetadata());
34-
perplexity_print_citations($response->getMetadata());
30+
perplexity_print_search_results($result->getMetadata());
31+
perplexity_print_citations($result->getMetadata());

examples/perplexity/image-input-url.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
use Symfony\AI\Agent\Agent;
1312
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
14-
use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor;
1513
use Symfony\AI\Platform\Message\Content\ImageUrl;
1614
use Symfony\AI\Platform\Message\Message;
1715
use Symfony\AI\Platform\Message\MessageBag;
1816

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

2119
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
22-
$agent = new Agent($platform, 'sonar', outputProcessors: [new SearchResultProcessor()]);
2320

2421
$messages = new MessageBag(
2522
Message::forSystem('You are an image analyzer bot that helps identify the content of images.'),
@@ -28,9 +25,9 @@
2825
new ImageUrl('https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Webysther_20160423_-_Elephpant.svg/350px-Webysther_20160423_-_Elephpant.svg.png'),
2926
),
3027
);
31-
$result = $agent->call($messages);
28+
$result = $platform->invoke('sonar', $messages);
3229

33-
echo $result->getContent().\PHP_EOL;
30+
echo $result->asText().\PHP_EOL;
3431

3532
perplexity_print_search_results($result->getMetadata());
3633
perplexity_print_citations($result->getMetadata());

examples/perplexity/pdf-input-url.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,24 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
use Symfony\AI\Agent\Agent;
1312
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
14-
use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor;
1513
use Symfony\AI\Platform\Message\Content\DocumentUrl;
1614
use Symfony\AI\Platform\Message\Message;
1715
use Symfony\AI\Platform\Message\MessageBag;
1816

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

2119
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
22-
$agent = new Agent($platform, 'sonar', outputProcessors: [new SearchResultProcessor()]);
2320

2421
$messages = new MessageBag(
2522
Message::ofUser(
2623
new DocumentUrl('https://upload.wikimedia.org/wikipedia/commons/2/20/Re_example.pdf'),
2724
'What is this document about?',
2825
),
2926
);
30-
$result = $agent->call($messages);
27+
$result = $platform->invoke('sonar', $messages);
3128

32-
echo $result->getContent().\PHP_EOL;
33-
$result = $agent->call($messages);
34-
35-
echo $result->getContent().\PHP_EOL;
29+
echo $result->asText().\PHP_EOL;
3630

3731
perplexity_print_search_results($result->getMetadata());
3832
perplexity_print_citations($result->getMetadata());

examples/perplexity/stream.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,23 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
use Symfony\AI\Agent\Agent;
1312
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
14-
use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor;
1513
use Symfony\AI\Platform\Message\Message;
1614
use Symfony\AI\Platform\Message\MessageBag;
1715

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

2018
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
21-
$agent = new Agent($platform, 'sonar', outputProcessors: [new SearchResultProcessor()]);
2219

2320
$messages = new MessageBag(
2421
Message::forSystem('You are a thoughtful philosopher.'),
2522
Message::ofUser('What is the purpose of an ant?'),
2623
);
27-
$result = $agent->call($messages, [
24+
$result = $platform->invoke('sonar', $messages, [
2825
'stream' => true,
2926
]);
3027

31-
foreach ($result->getContent() as $word) {
28+
foreach ($result->asStream() as $word) {
3229
echo $word;
3330
}
3431
echo \PHP_EOL;

examples/perplexity/token-metadata.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,19 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
use Symfony\AI\Agent\Agent;
1312
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
1413
use Symfony\AI\Platform\Message\Message;
1514
use Symfony\AI\Platform\Message\MessageBag;
1615

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

1918
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
20-
$agent = new Agent($platform, 'sonar');
2119

2220
$messages = new MessageBag(
2321
Message::forSystem('You are a pirate and you write funny.'),
2422
Message::ofUser('What is the Symfony framework?'),
2523
);
26-
$result = $agent->call($messages, [
24+
$result = $platform->invoke('sonar', $messages, [
2725
'max_tokens' => 500, // specific options just for this call
2826
]);
2927

examples/perplexity/web-search.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@
2828
]);
2929

3030
echo $result->asText().\PHP_EOL;
31+
echo \PHP_EOL;
32+
33+
perplexity_print_search_results($result->getMetadata());
34+
perplexity_print_citations($result->getMetadata());

src/platform/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
"phpstan/phpstan-strict-rules": "^2.0",
7070
"phpstan/phpstan-symfony": "^2.0.6",
7171
"phpunit/phpunit": "^11.5.46",
72-
"symfony/ai-agent": "@dev",
7372
"symfony/cache": "^7.3|^8.0",
7473
"symfony/console": "^7.3|^8.0",
7574
"symfony/dotenv": "^7.3|^8.0",

src/platform/src/Bridge/Perplexity/ResultConverter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public function convert(RawResultInterface $result, array $options = []): Result
4747

4848
$result = 1 === \count($choices) ? $choices[0] : new ChoiceResult(...$choices);
4949

50+
$metadata = $result->getMetadata();
51+
52+
if (\array_key_exists('search_results', $data)) {
53+
$metadata->add('search_results', $data['search_results']);
54+
}
55+
56+
if (\array_key_exists('citations', $data)) {
57+
$metadata->add('citations', $data['citations']);
58+
}
59+
5060
return $result;
5161
}
5262

0 commit comments

Comments
 (0)