Skip to content

Commit b1f5046

Browse files
junaidbinfarooqchr-hertel
authored andcommitted
[Platform] Add support for Google vertex AI
1 parent 1f39a86 commit b1f5046

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2759
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Symfony AI is a set of components that integrate AI capabilities into PHP applic
1313
Symfony AI consists of several lower and higher level **components** and the respective integration **bundles**:
1414

1515
* **Components**
16-
* **[Platform](src/platform/README.md)**: A unified interface to various AI platforms like OpenAI, Anthropic, Azure, Gemini, and more.
16+
* **[Platform](src/platform/README.md)**: A unified interface to various AI platforms like OpenAI, Anthropic, Azure, Gemini, VertexAI, and more.
1717
* **[Agent](src/agent/README.md)**: Framework for building AI agents that can interact with users and perform tasks.
1818
* **[Store](src/store/README.md)**: Data storage abstraction with indexing and retrieval for AI applications.
1919
* **[MCP SDK](src/mcp-sdk/README.md)**: SDK for [Model Context Protocol](https://modelcontextprotocol.io) enabling communication between AI agents and tools.

examples/.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ RUN_EXPENSIVE_EXAMPLES=false
7676
# For using Gemini
7777
GEMINI_API_KEY=
7878

79+
# Vertex AI
80+
GOOGLE_CLOUD_LOCATION=global
81+
GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
82+
7983
# For using Albert API (French Sovereign AI)
8084
ALBERT_API_KEY=
8185
ALBERT_API_URL=

examples/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"async-aws/bedrock-runtime": "^1.1",
99
"codewithkyrian/transformers": "^0.6.1",
1010
"doctrine/dbal": "^3.3|^4.0",
11+
"google/auth": "^1.47",
1112
"mrmysql/youtube-transcript": "^0.0.5",
1213
"php-http/discovery": "^1.20",
1314
"probots-io/pinecone-php": "^1.1",

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;

0 commit comments

Comments
 (0)