Skip to content

Commit 9124195

Browse files
docs(platform): Add support for Google vertex AI
- Adds examples to interact with vertex ai
1 parent b3ebfca commit 9124195

19 files changed

+416
-17
lines changed

examples/vertexai/audio-input.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\VertexAi\Gemini\Model;
14+
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory;
15+
use Symfony\AI\Platform\Message\Content\Audio;
16+
use Symfony\AI\Platform\Message\Message;
17+
use Symfony\AI\Platform\Message\MessageBag;
18+
19+
require_once __DIR__.'/bootstrap.php';
20+
21+
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client());
22+
$model = new Model(Model::GEMINI_2_5_FLASH);
23+
24+
$agent = new Agent($platform, $model, logger: logger());
25+
$messages = new MessageBag(
26+
Message::ofUser(
27+
'What is this recording about?',
28+
Audio::fromFile(dirname(__DIR__, 2).'/fixtures/audio.mp3'),
29+
),
30+
);
31+
$result = $agent->call($messages);
32+
33+
echo $result->getContent().\PHP_EOL;

examples/vertexai/bootstrap.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Google\Auth\ApplicationDefaultCredentials;
13+
use Psr\Log\LoggerAwareInterface;
14+
use Symfony\Component\HttpClient\HttpClient;
15+
use Symfony\Contracts\HttpClient\HttpClientInterface;
16+
17+
require_once dirname(__DIR__).'/bootstrap.php';
18+
19+
function adc_aware_http_client(): HttpClientInterface
20+
{
21+
$credentials = ApplicationDefaultCredentials::getCredentials(['https://www.googleapis.com/auth/cloud-platform']);
22+
$httpClient = HttpClient::create([
23+
'auth_bearer' => $credentials?->fetchAuthToken()['access_token'] ?? null,
24+
]);
25+
26+
if ($httpClient instanceof LoggerAwareInterface) {
27+
$httpClient->setLogger(logger());
28+
}
29+
30+
return $httpClient;
31+
}

examples/vertexai/chat.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\VertexAi\Gemini\Model;
14+
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory;
15+
use Symfony\AI\Platform\Message\Message;
16+
use Symfony\AI\Platform\Message\MessageBag;
17+
18+
require_once __DIR__.'/bootstrap.php';
19+
20+
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client());
21+
$model = new Model(Model::GEMINI_2_5_FLASH);
22+
23+
$agent = new Agent($platform, $model, logger: logger());
24+
$messages = new MessageBag(
25+
Message::forSystem('You are an expert assistant in geography.'),
26+
Message::ofUser('Where is Mount Fuji?'),
27+
);
28+
$result = $agent->call($messages);
29+
30+
echo $result->getContent().\PHP_EOL;

examples/vertexai/embeddings.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Platform\Bridge\VertexAi\Embeddings\Model;
13+
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory;
14+
15+
require_once __DIR__.'/bootstrap.php';
16+
17+
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client());
18+
$embeddings = new Model();
19+
20+
$result = $platform->invoke($embeddings, <<<TEXT
21+
Once upon a time, there was a country called Japan. It was a beautiful country with a lot of mountains and rivers.
22+
The people of Japan were very kind and hardworking. They loved their country very much and took care of it. The
23+
country was very peaceful and prosperous. The people lived happily ever after.
24+
TEXT);
25+
26+
echo 'Dimensions: '.$result->asVectors()[0]->getDimensions().\PHP_EOL;

examples/vertexai/image-input.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\VertexAi\Gemini\Model;
14+
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory;
15+
use Symfony\AI\Platform\Message\Content\Image;
16+
use Symfony\AI\Platform\Message\Message;
17+
use Symfony\AI\Platform\Message\MessageBag;
18+
19+
require_once __DIR__.'/bootstrap.php';
20+
21+
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client());
22+
$model = new Model(Model::GEMINI_2_5_PRO);
23+
24+
$agent = new Agent($platform, $model, logger: logger());
25+
$messages = new MessageBag(
26+
Message::forSystem('You are an image analyzer bot that helps identify the content of images.'),
27+
Message::ofUser(
28+
'Describe the image as a comedian would do it.',
29+
Image::fromFile(dirname(__DIR__, 2).'/fixtures/image.jpg'),
30+
),
31+
);
32+
$result = $agent->call($messages);
33+
34+
echo $result->getContent().\PHP_EOL;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\VertexAi\Gemini\Model;
14+
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory;
15+
use Symfony\AI\Platform\Message\Content\Document;
16+
use Symfony\AI\Platform\Message\Message;
17+
use Symfony\AI\Platform\Message\MessageBag;
18+
19+
require_once __DIR__.'/bootstrap.php';
20+
21+
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client());
22+
$model = new Model(Model::GEMINI_2_5_FLASH);
23+
24+
$agent = new Agent($platform, $model, logger: logger());
25+
$messages = new MessageBag(
26+
Message::ofUser(
27+
Document::fromFile(dirname(__DIR__, 2).'/fixtures/document.pdf'),
28+
'What is this document about?',
29+
),
30+
);
31+
$result = $agent->call($messages);
32+
33+
echo $result->getContent().\PHP_EOL;

examples/vertexai/server-tools.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Agent\Toolbox\AgentProcessor;
14+
use Symfony\AI\Agent\Toolbox\Tool\Clock;
15+
use Symfony\AI\Agent\Toolbox\Toolbox;
16+
use Symfony\AI\Platform\Bridge\VertexAi\Gemini\Model;
17+
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory;
18+
use Symfony\AI\Platform\Message\Message;
19+
use Symfony\AI\Platform\Message\MessageBag;
20+
21+
require_once __DIR__.'/bootstrap.php';
22+
23+
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client());
24+
25+
$model = new Model(Model::GEMINI_2_5_PRO);
26+
27+
$toolbox = new Toolbox([new Clock()], logger: logger());
28+
$processor = new AgentProcessor($toolbox);
29+
$agent = new Agent($platform, $model, [$processor], [$processor], logger());
30+
31+
$content = file_get_contents('https://www.euribor-rates.eu/en/current-euribor-rates/4/euribor-rate-12-months/');
32+
$messages = new MessageBag(
33+
Message::ofUser("Based on the following page content, what was the 12-month Euribor rate a week ago?\n\n".$content)
34+
);
35+
36+
$result = $agent->call($messages);
37+
38+
echo $result->getContent().\PHP_EOL;

examples/vertexai/stream.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 <[email protected]>
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\VertexAi\Gemini\Model;
14+
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory;
15+
use Symfony\AI\Platform\Message\Message;
16+
use Symfony\AI\Platform\Message\MessageBag;
17+
18+
require_once __DIR__.'/bootstrap.php';
19+
20+
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client());
21+
$model = new Model(Model::GEMINI_2_5_FLASH);
22+
23+
$agent = new Agent($platform, $model, logger: logger());
24+
$messages = new MessageBag(
25+
Message::forSystem('You are an expert assistant in geography.'),
26+
Message::ofUser('Where is Mount Fuji?'),
27+
);
28+
29+
$result = $agent->call($messages, [
30+
'stream' => true,
31+
]);
32+
33+
foreach ($result->getContent() as $word) {
34+
echo $word;
35+
}
36+
37+
echo \PHP_EOL;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Agent\StructuredOutput\AgentProcessor as StructuredOutputProcessor;
14+
use Symfony\AI\Agent\Toolbox\AgentProcessor as ToolProcessor;
15+
use Symfony\AI\Agent\Toolbox\Tool\Clock;
16+
use Symfony\AI\Agent\Toolbox\Toolbox;
17+
use Symfony\AI\Platform\Bridge\VertexAi\Gemini\Model;
18+
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory;
19+
use Symfony\AI\Platform\Message\Message;
20+
use Symfony\AI\Platform\Message\MessageBag;
21+
use Symfony\Component\Clock\Clock as SymfonyClock;
22+
23+
require_once __DIR__.'/bootstrap.php';
24+
25+
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client());
26+
$model = new Model(Model::GEMINI_2_5_PRO);
27+
28+
$clock = new Clock(new SymfonyClock());
29+
$toolbox = new Toolbox([$clock]);
30+
$toolProcessor = new ToolProcessor($toolbox);
31+
$structuredOutputProcessor = new StructuredOutputProcessor();
32+
$agent = new Agent($platform, $model, [$toolProcessor, $structuredOutputProcessor], [$toolProcessor, $structuredOutputProcessor], logger());
33+
34+
$messages = new MessageBag(Message::ofUser('What date and time is it?'));
35+
$result = $agent->call($messages, ['response_format' => [
36+
'type' => 'json_schema',
37+
'json_schema' => [
38+
'name' => 'clock',
39+
'strict' => true,
40+
'schema' => [
41+
'type' => 'object',
42+
'properties' => [
43+
'date' => ['type' => 'string', 'description' => 'The current date in the format YYYY-MM-DD.'],
44+
'time' => ['type' => 'string', 'description' => 'The current time in the format HH:MM:SS.'],
45+
],
46+
'required' => ['date', 'time'],
47+
'additionalProperties' => false,
48+
],
49+
],
50+
]]);
51+
52+
dump($result->getContent());
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Agent\StructuredOutput\AgentProcessor;
14+
use Symfony\AI\Fixtures\StructuredOutput\MathReasoning;
15+
use Symfony\AI\Platform\Bridge\VertexAi\Gemini\Model;
16+
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory;
17+
use Symfony\AI\Platform\Message\Message;
18+
use Symfony\AI\Platform\Message\MessageBag;
19+
20+
require_once __DIR__.'/bootstrap.php';
21+
22+
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client());
23+
$model = new Model(Model::GEMINI_2_5_FLASH_LITE);
24+
25+
$processor = new AgentProcessor();
26+
$agent = new Agent($platform, $model, [$processor], [$processor], logger());
27+
$messages = new MessageBag(
28+
Message::forSystem('You are a helpful math tutor. Guide the user through the solution step by step.'),
29+
Message::ofUser('how can I solve 8x + 7 = -23'),
30+
);
31+
$result = $agent->call($messages, ['output_structure' => MathReasoning::class]);
32+
33+
dump($result->getContent());

0 commit comments

Comments
 (0)