Skip to content

Commit cc7f142

Browse files
committed
Add tests to cover Perplexity platform bridge
1 parent 086f84e commit cc7f142

File tree

13 files changed

+607
-148
lines changed

13 files changed

+607
-148
lines changed

examples/bootstrap.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,53 @@ function print_vectors(ResultPromise $result): void
8080

8181
echo 'Dimensions: '.$result->asVectors()[0]->getDimensions().\PHP_EOL;
8282
}
83+
84+
function perplexity_print_search_results(Metadata $metadata): void
85+
{
86+
$searchResults = $metadata->get('search_results');
87+
88+
if (null === $searchResults) {
89+
return;
90+
}
91+
92+
echo 'Search results:'.\PHP_EOL;
93+
94+
if (0 === count($searchResults)) {
95+
echo 'No search results.'.\PHP_EOL;
96+
97+
return;
98+
}
99+
100+
foreach ($searchResults as $i => $searchResult) {
101+
echo 'Result #'.($i + 1).':'.\PHP_EOL;
102+
echo $searchResult['title'].\PHP_EOL;
103+
echo $searchResult['url'].\PHP_EOL;
104+
echo $searchResult['date'].\PHP_EOL;
105+
echo $searchResult['last_updated'] ? $searchResult['last_updated'].\PHP_EOL : '';
106+
echo $searchResult['snippet'] ? $searchResult['snippet'].\PHP_EOL : '';
107+
echo \PHP_EOL;
108+
}
109+
}
110+
111+
function perplexity_print_citations(Metadata $metadata): void
112+
{
113+
$citations = $metadata->get('citations');
114+
115+
if (null === $citations) {
116+
return;
117+
}
118+
119+
echo 'Citations:'.\PHP_EOL;
120+
121+
if (0 === count($citations)) {
122+
echo 'No citations.'.\PHP_EOL;
123+
124+
return;
125+
}
126+
127+
foreach ($citations as $i => $citation) {
128+
echo 'Citation #'.($i + 1).':'.\PHP_EOL;
129+
echo $citation.\PHP_EOL;
130+
echo \PHP_EOL;
131+
}
132+
}

examples/perplexity/academic-search.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
1414
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
1515
use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor;
16-
use Symfony\AI\Platform\Bridge\Perplexity\TokenOutputProcessor;
1716
use Symfony\AI\Platform\Message\Message;
1817
use Symfony\AI\Platform\Message\MessageBag;
1918

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

2221
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
2322
$model = new Perplexity();
24-
$agent = new Agent($platform, $model, outputProcessors: [new TokenOutputProcessor(), new SearchResultProcessor()], logger: logger());
23+
$agent = new Agent($platform, $model, outputProcessors: [new SearchResultProcessor()], logger: logger());
2524

2625
$messages = new MessageBag(Message::ofUser('What is the best French cheese of the first quarter-century of 21st century?'));
2726
$response = $agent->call($messages, [
@@ -33,35 +32,5 @@
3332
echo $response->getContent().\PHP_EOL;
3433
echo \PHP_EOL;
3534

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-
}
35+
perplexity_print_search_results($response->getMetadata());
36+
perplexity_print_citations($response->getMetadata());

examples/perplexity/image-input-url.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
1414
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
1515
use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor;
16-
use Symfony\AI\Platform\Bridge\Perplexity\TokenOutputProcessor;
1716
use Symfony\AI\Platform\Message\Content\ImageUrl;
1817
use Symfony\AI\Platform\Message\Message;
1918
use Symfony\AI\Platform\Message\MessageBag;
@@ -22,7 +21,7 @@
2221

2322
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
2423
$model = new Perplexity();
25-
$agent = new Agent($platform, $model, outputProcessors: [new TokenOutputProcessor(), new SearchResultProcessor()], logger: logger());
24+
$agent = new Agent($platform, $model, outputProcessors: [new SearchResultProcessor()], logger: logger());
2625

2726
$messages = new MessageBag(
2827
Message::forSystem('You are an image analyzer bot that helps identify the content of images.'),
@@ -35,35 +34,5 @@
3534

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

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-
}
37+
perplexity_print_search_results($result->getMetadata());
38+
perplexity_print_citations($result->getMetadata());

examples/perplexity/pdf-input-url.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
1414
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
1515
use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor;
16-
use Symfony\AI\Platform\Bridge\Perplexity\TokenOutputProcessor;
1716
use Symfony\AI\Platform\Message\Content\DocumentUrl;
1817
use Symfony\AI\Platform\Message\Message;
1918
use Symfony\AI\Platform\Message\MessageBag;
@@ -22,7 +21,7 @@
2221

2322
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
2423
$model = new Perplexity();
25-
$agent = new Agent($platform, $model, outputProcessors: [new TokenOutputProcessor(), new SearchResultProcessor()], logger: logger());
24+
$agent = new Agent($platform, $model, outputProcessors: [new SearchResultProcessor()], logger: logger());
2625

2726
$messages = new MessageBag(
2827
Message::ofUser(
@@ -37,35 +36,5 @@
3736

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

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-
}
39+
perplexity_print_search_results($result->getMetadata());
40+
perplexity_print_citations($result->getMetadata());

examples/perplexity/stream.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
1414
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
1515
use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor;
16-
use Symfony\AI\Platform\Bridge\Perplexity\TokenOutputProcessor;
1716
use Symfony\AI\Platform\Message\Message;
1817
use Symfony\AI\Platform\Message\MessageBag;
1918

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

2221
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
2322
$model = new Perplexity();
24-
$agent = new Agent($platform, $model, outputProcessors: [new TokenOutputProcessor(), new SearchResultProcessor()], logger: logger());
23+
$agent = new Agent($platform, $model, outputProcessors: [new SearchResultProcessor()], logger: logger());
2524

2625
$messages = new MessageBag(
2726
Message::forSystem('You are a thoughtful philosopher.'),
@@ -36,35 +35,5 @@
3635
}
3736
echo \PHP_EOL;
3837

39-
$metadata = $result->getMetadata();
40-
if ($metadata->has('search_results')) {
41-
echo 'Search results:'.\PHP_EOL;
42-
if (0 === count($metadata->get('search_results'))) {
43-
echo 'No search results.'.\PHP_EOL;
44-
45-
return;
46-
}
47-
foreach ($metadata->get('search_results') as $i => $searchResult) {
48-
echo 'Result #'.($i + 1).':'.\PHP_EOL;
49-
echo $searchResult['title'].\PHP_EOL;
50-
echo $searchResult['url'].\PHP_EOL;
51-
echo $searchResult['date'].\PHP_EOL;
52-
echo $searchResult['last_updated'] ? $searchResult['last_updated'].\PHP_EOL : '';
53-
echo $searchResult['snippet'] ? $searchResult['snippet'].\PHP_EOL : '';
54-
echo \PHP_EOL;
55-
}
56-
}
57-
58-
if ($metadata->has('citations')) {
59-
echo 'Citations:'.\PHP_EOL;
60-
if (0 === count($metadata->get('citations'))) {
61-
echo 'No citations.'.\PHP_EOL;
62-
63-
return;
64-
}
65-
foreach ($metadata->get('citations') as $i => $citation) {
66-
echo 'Citation #'.($i + 1).':'.\PHP_EOL;
67-
echo $citation.\PHP_EOL;
68-
echo \PHP_EOL;
69-
}
70-
}
38+
perplexity_print_search_results($result->getMetadata());
39+
perplexity_print_citations($result->getMetadata());

examples/perplexity/token-metadata.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@
1212
use Symfony\AI\Agent\Agent;
1313
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
1414
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
15-
use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor;
1615
use Symfony\AI\Platform\Bridge\Perplexity\TokenOutputProcessor;
1716
use Symfony\AI\Platform\Message\Message;
1817
use Symfony\AI\Platform\Message\MessageBag;
19-
use Symfony\AI\Platform\Metadata\TokenUsage;
2018

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

2321
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
2422
$model = new Perplexity();
25-
$agent = new Agent($platform, $model, outputProcessors: [new TokenOutputProcessor(), new SearchResultProcessor()], logger: logger());
23+
$agent = new Agent($platform, $model, outputProcessors: [new TokenOutputProcessor()], logger: logger());
2624

2725
$messages = new MessageBag(
2826
Message::forSystem('You are a pirate and you write funny.'),
@@ -33,9 +31,4 @@
3331
'max_tokens' => 500, // specific options just for this call
3432
]);
3533

36-
$metadata = $result->getMetadata();
37-
$tokenUsage = $metadata->get('token_usage');
38-
39-
assert($tokenUsage instanceof TokenUsage);
40-
4134
print_token_usage($result->getMetadata());

examples/perplexity/web-search.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
use Symfony\AI\Agent\Agent;
1313
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
1414
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
15-
use Symfony\AI\Platform\Bridge\Perplexity\SearchResultProcessor;
16-
use Symfony\AI\Platform\Bridge\Perplexity\TokenOutputProcessor;
1715
use Symfony\AI\Platform\Message\Message;
1816
use Symfony\AI\Platform\Message\MessageBag;
1917

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

2220
$platform = PlatformFactory::create(env('PERPLEXITY_API_KEY'), http_client());
2321
$model = new Perplexity();
24-
$agent = new Agent($platform, $model, outputProcessors: [new TokenOutputProcessor(), new SearchResultProcessor()], logger: logger());
22+
$agent = new Agent($platform, $model, logger: logger());
2523

2624
$messages = new MessageBag(Message::ofUser('What is the best French cheese?'));
2725
$response = $agent->call($messages, [
@@ -34,4 +32,3 @@
3432
]);
3533

3634
echo $response->getContent().\PHP_EOL;
37-
echo \PHP_EOL;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
namespace Symfony\AI\Platform\Tests\Bridge\Perplexity\Contract;
13+
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\Attributes\DataProvider;
16+
use PHPUnit\Framework\Attributes\Medium;
17+
use PHPUnit\Framework\TestCase;
18+
use Symfony\AI\Platform\Bridge\Perplexity\Contract\FileUrlNormalizer;
19+
use Symfony\AI\Platform\Bridge\Perplexity\Perplexity;
20+
use Symfony\AI\Platform\Contract;
21+
use Symfony\AI\Platform\Contract\Normalizer\Message\MessageBagNormalizer;
22+
use Symfony\AI\Platform\Message\Content\DocumentUrl;
23+
24+
#[Medium]
25+
#[CoversClass(FileUrlNormalizer::class)]
26+
#[CoversClass(MessageBagNormalizer::class)]
27+
final class FileUrlNormalizerTest extends TestCase
28+
{
29+
public function testSupportsNormalization()
30+
{
31+
$normalizer = new FileUrlNormalizer();
32+
33+
$this->assertTrue($normalizer->supportsNormalization(new DocumentUrl(\dirname(__DIR__, 6).'/fixtures/not-a-document.pdf'), context: [
34+
Contract::CONTEXT_MODEL => new Perplexity(),
35+
]));
36+
$this->assertFalse($normalizer->supportsNormalization('not a document'));
37+
}
38+
39+
public function testGetSupportedTypes()
40+
{
41+
$normalizer = new FileUrlNormalizer();
42+
43+
$expected = [
44+
DocumentUrl::class => true,
45+
];
46+
47+
$this->assertSame($expected, $normalizer->getSupportedTypes(null));
48+
}
49+
50+
#[DataProvider('normalizeDataProvider')]
51+
public function testNormalize(DocumentUrl $document, array $expected)
52+
{
53+
$normalizer = new FileUrlNormalizer();
54+
55+
$normalized = $normalizer->normalize($document);
56+
57+
$this->assertEquals($expected, $normalized);
58+
}
59+
60+
public static function normalizeDataProvider(): iterable
61+
{
62+
yield 'document from file url' => [
63+
new DocumentUrl(\dirname(__DIR__, 6).'/fixtures/document.pdf'),
64+
[
65+
'type' => 'file_url',
66+
'file_url' => [
67+
'url' => \dirname(__DIR__, 6).'/fixtures/document.pdf',
68+
],
69+
],
70+
];
71+
}
72+
}

0 commit comments

Comments
 (0)