-
-
Notifications
You must be signed in to change notification settings - Fork 56
[Platform] Add support for Google vertex AI #297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
junaidbinfarooq
wants to merge
9
commits into
symfony:main
Choose a base branch
from
junaidbinfarooq:feat/platform-support-for-vertex-ai
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,508
−5
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
640d57c
feat(platform): Add support for Google vertex AI
junaidbinfarooq 0a614cd
feat(platform): Add support for Google vertex AI
junaidbinfarooq 7dfc531
feat(platform): Add support for Google vertex AI
junaidbinfarooq c70b486
docs(platform): Add support for Google vertex AI
junaidbinfarooq a5a593b
feat(platform): Add support for Google vertex AI
junaidbinfarooq 98b930f
docs(platform): Add support for Google vertex AI
junaidbinfarooq 76d574c
feat(platform): Add support for Google vertex AI
junaidbinfarooq 71e7cbe
feat(platform): Add support for Google vertex AI
junaidbinfarooq 6f392c8
refactor(platform): Add support for Google vertex AI
junaidbinfarooq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\AI\Agent\Agent; | ||
use Symfony\AI\Platform\Bridge\VertexAi\Gemini\Model; | ||
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory; | ||
use Symfony\AI\Platform\Message\Content\Audio; | ||
use Symfony\AI\Platform\Message\Message; | ||
use Symfony\AI\Platform\Message\MessageBag; | ||
|
||
require_once __DIR__.'/bootstrap.php'; | ||
|
||
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client()); | ||
$model = new Model(Model::GEMINI_2_5_FLASH); | ||
|
||
$agent = new Agent($platform, $model, logger: logger()); | ||
$messages = new MessageBag( | ||
Message::ofUser( | ||
'What is this recording about?', | ||
Audio::fromFile(dirname(__DIR__, 2).'/fixtures/audio.mp3'), | ||
), | ||
); | ||
$result = $agent->call($messages); | ||
|
||
echo $result->getContent().\PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Google\Auth\ApplicationDefaultCredentials; | ||
use Psr\Log\LoggerAwareInterface; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
require_once dirname(__DIR__).'/bootstrap.php'; | ||
|
||
function adc_aware_http_client(): HttpClientInterface | ||
{ | ||
$credentials = ApplicationDefaultCredentials::getCredentials(['https://www.googleapis.com/auth/cloud-platform']); | ||
$httpClient = HttpClient::create([ | ||
'auth_bearer' => $credentials?->fetchAuthToken()['access_token'] ?? null, | ||
]); | ||
|
||
if ($httpClient instanceof LoggerAwareInterface) { | ||
$httpClient->setLogger(logger()); | ||
} | ||
|
||
return $httpClient; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\AI\Agent\Agent; | ||
use Symfony\AI\Platform\Bridge\VertexAi\Gemini\Model; | ||
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory; | ||
use Symfony\AI\Platform\Message\Message; | ||
use Symfony\AI\Platform\Message\MessageBag; | ||
|
||
require_once __DIR__.'/bootstrap.php'; | ||
|
||
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client()); | ||
$model = new Model(Model::GEMINI_2_5_FLASH); | ||
|
||
$agent = new Agent($platform, $model, logger: logger()); | ||
$messages = new MessageBag( | ||
Message::forSystem('You are an expert assistant in geography.'), | ||
Message::ofUser('Where is Mount Fuji?'), | ||
); | ||
$result = $agent->call($messages); | ||
|
||
echo $result->getContent().\PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\AI\Platform\Bridge\VertexAi\Embeddings\Model; | ||
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory; | ||
|
||
require_once __DIR__.'/bootstrap.php'; | ||
|
||
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client()); | ||
$embeddings = new Model(); | ||
|
||
$result = $platform->invoke($embeddings, <<<TEXT | ||
Once upon a time, there was a country called Japan. It was a beautiful country with a lot of mountains and rivers. | ||
The people of Japan were very kind and hardworking. They loved their country very much and took care of it. The | ||
country was very peaceful and prosperous. The people lived happily ever after. | ||
TEXT); | ||
|
||
echo 'Dimensions: '.$result->asVectors()[0]->getDimensions().\PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\AI\Agent\Agent; | ||
use Symfony\AI\Platform\Bridge\VertexAi\Gemini\Model; | ||
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory; | ||
use Symfony\AI\Platform\Message\Content\Image; | ||
use Symfony\AI\Platform\Message\Message; | ||
use Symfony\AI\Platform\Message\MessageBag; | ||
|
||
require_once __DIR__.'/bootstrap.php'; | ||
|
||
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client()); | ||
$model = new Model(Model::GEMINI_2_5_PRO); | ||
|
||
$agent = new Agent($platform, $model, logger: logger()); | ||
$messages = new MessageBag( | ||
Message::forSystem('You are an image analyzer bot that helps identify the content of images.'), | ||
Message::ofUser( | ||
'Describe the image as a comedian would do it.', | ||
Image::fromFile(dirname(__DIR__, 2).'/fixtures/image.jpg'), | ||
), | ||
); | ||
$result = $agent->call($messages); | ||
|
||
echo $result->getContent().\PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\AI\Agent\Agent; | ||
use Symfony\AI\Platform\Bridge\VertexAi\Gemini\Model; | ||
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory; | ||
use Symfony\AI\Platform\Message\Content\Document; | ||
use Symfony\AI\Platform\Message\Message; | ||
use Symfony\AI\Platform\Message\MessageBag; | ||
|
||
require_once __DIR__.'/bootstrap.php'; | ||
|
||
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client()); | ||
$model = new Model(Model::GEMINI_2_5_FLASH); | ||
|
||
$agent = new Agent($platform, $model, logger: logger()); | ||
$messages = new MessageBag( | ||
Message::ofUser( | ||
Document::fromFile(dirname(__DIR__, 2).'/fixtures/document.pdf'), | ||
'What is this document about?', | ||
), | ||
); | ||
$result = $agent->call($messages); | ||
|
||
echo $result->getContent().\PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\AI\Agent\Agent; | ||
use Symfony\AI\Agent\Toolbox\AgentProcessor; | ||
use Symfony\AI\Agent\Toolbox\Tool\Clock; | ||
use Symfony\AI\Agent\Toolbox\Toolbox; | ||
use Symfony\AI\Platform\Bridge\VertexAi\Gemini\Model; | ||
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory; | ||
use Symfony\AI\Platform\Message\Message; | ||
use Symfony\AI\Platform\Message\MessageBag; | ||
|
||
require_once __DIR__.'/bootstrap.php'; | ||
|
||
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client()); | ||
|
||
$model = new Model(Model::GEMINI_2_5_PRO); | ||
|
||
$toolbox = new Toolbox([new Clock()], logger: logger()); | ||
$processor = new AgentProcessor($toolbox); | ||
$agent = new Agent($platform, $model, [$processor], [$processor], logger()); | ||
|
||
$content = file_get_contents('https://www.euribor-rates.eu/en/current-euribor-rates/4/euribor-rate-12-months/'); | ||
$messages = new MessageBag( | ||
Message::ofUser("Based on the following page content, what was the 12-month Euribor rate a week ago?\n\n".$content) | ||
); | ||
|
||
$result = $agent->call($messages); | ||
|
||
echo $result->getContent().\PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\AI\Agent\Agent; | ||
use Symfony\AI\Platform\Bridge\VertexAi\Gemini\Model; | ||
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory; | ||
use Symfony\AI\Platform\Message\Message; | ||
use Symfony\AI\Platform\Message\MessageBag; | ||
|
||
require_once __DIR__.'/bootstrap.php'; | ||
|
||
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client()); | ||
$model = new Model(Model::GEMINI_2_5_FLASH); | ||
|
||
$agent = new Agent($platform, $model, logger: logger()); | ||
$messages = new MessageBag( | ||
Message::forSystem('You are an expert assistant in geography.'), | ||
Message::ofUser('Where is Mount Fuji?'), | ||
); | ||
|
||
$result = $agent->call($messages, [ | ||
'stream' => true, | ||
]); | ||
|
||
foreach ($result->getContent() as $word) { | ||
echo $word; | ||
} | ||
|
||
echo \PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\AI\Agent\Agent; | ||
use Symfony\AI\Agent\StructuredOutput\AgentProcessor as StructuredOutputProcessor; | ||
use Symfony\AI\Agent\Toolbox\AgentProcessor as ToolProcessor; | ||
use Symfony\AI\Agent\Toolbox\Tool\Clock; | ||
use Symfony\AI\Agent\Toolbox\Toolbox; | ||
use Symfony\AI\Platform\Bridge\VertexAi\Gemini\Model; | ||
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory; | ||
use Symfony\AI\Platform\Message\Message; | ||
use Symfony\AI\Platform\Message\MessageBag; | ||
use Symfony\Component\Clock\Clock as SymfonyClock; | ||
|
||
require_once __DIR__.'/bootstrap.php'; | ||
|
||
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client()); | ||
$model = new Model(Model::GEMINI_2_5_PRO); | ||
|
||
$clock = new Clock(new SymfonyClock()); | ||
$toolbox = new Toolbox([$clock]); | ||
$toolProcessor = new ToolProcessor($toolbox); | ||
$structuredOutputProcessor = new StructuredOutputProcessor(); | ||
$agent = new Agent($platform, $model, [$toolProcessor, $structuredOutputProcessor], [$toolProcessor, $structuredOutputProcessor], logger()); | ||
|
||
$messages = new MessageBag(Message::ofUser('What date and time is it?')); | ||
$result = $agent->call($messages, ['response_format' => [ | ||
'type' => 'json_schema', | ||
'json_schema' => [ | ||
'name' => 'clock', | ||
'strict' => true, | ||
'schema' => [ | ||
'type' => 'object', | ||
'properties' => [ | ||
'date' => ['type' => 'string', 'description' => 'The current date in the format YYYY-MM-DD.'], | ||
'time' => ['type' => 'string', 'description' => 'The current time in the format HH:MM:SS.'], | ||
], | ||
'required' => ['date', 'time'], | ||
'additionalProperties' => false, | ||
], | ||
], | ||
]]); | ||
|
||
dump($result->getContent()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\AI\Agent\Agent; | ||
use Symfony\AI\Agent\StructuredOutput\AgentProcessor; | ||
use Symfony\AI\Fixtures\StructuredOutput\MathReasoning; | ||
use Symfony\AI\Platform\Bridge\VertexAi\Gemini\Model; | ||
use Symfony\AI\Platform\Bridge\VertexAi\PlatformFactory; | ||
use Symfony\AI\Platform\Message\Message; | ||
use Symfony\AI\Platform\Message\MessageBag; | ||
|
||
require_once __DIR__.'/bootstrap.php'; | ||
|
||
$platform = PlatformFactory::create(env('GOOGLE_CLOUD_LOCATION'), env('GOOGLE_CLOUD_PROJECT'), adc_aware_http_client()); | ||
$model = new Model(Model::GEMINI_2_5_FLASH_LITE); | ||
|
||
$processor = new AgentProcessor(); | ||
$agent = new Agent($platform, $model, [$processor], [$processor], logger()); | ||
$messages = new MessageBag( | ||
Message::forSystem('You are a helpful math tutor. Guide the user through the solution step by step.'), | ||
Message::ofUser('how can I solve 8x + 7 = -23'), | ||
); | ||
$result = $agent->call($messages, ['output_structure' => MathReasoning::class]); | ||
|
||
dump($result->getContent()); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.