PHP code example of aisdk / google-agent-platform

1. Go to this page and download the library: Download aisdk/google-agent-platform library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aisdk / google-agent-platform example snippets


use AiSdk\Generate;
use AiSdk\GoogleAgentPlatform;

$result = Generate::text()
    ->model(GoogleAgentPlatform::model('google/gemini-2.5-flash'))
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text;

$embedding = Generate::embedding(['First document to index', 'Second document to index'])
    ->model(GoogleAgentPlatform::model('gemini-embedding-001'))
    ->dimensions(768)
    ->providerOptions('google-agent-platform', [
        'task_type' => 'RETRIEVAL_DOCUMENT',
        'autoTruncate' => false,
    ])
    ->run();

$vector = $embedding->output->vector;

$image = Generate::image('A clean product photograph')
    ->model(GoogleAgentPlatform::model('google/gemini-3.1-flash-image'))
    ->aspectRatio('16:9')
    ->run();

$speech = Generate::speech('Welcome to the application.')
    ->model(GoogleAgentPlatform::model('google/gemini-3.1-flash-tts-preview'))
    ->voice('Kore')
    ->run();

use AiSdk\Content;
use AiSdk\Generate;
use AiSdk\GoogleAgentPlatform;

$result = Generate::transcription(Content::audio(__DIR__.'/meeting.mp3'))
    ->model(GoogleAgentPlatform::model('google/gemini-2.5-flash'))
    ->run();

echo $result->output->text;

use AiSdk\GoogleAgentPlatform;
use AiSdk\Live;
use AiSdk\Live\AudioDelta;
use AiSdk\Live\TranscriptDelta;
use AiSdk\Transport;

$session = Live::voice()
    ->model(GoogleAgentPlatform::model('gemini-live-2.5-flash-native-audio'))
    ->instructions('You are a concise customer-support agent.')
    ->voice('Kore')
    ->language('en-US')
    ->connect(Transport::auto());

// Send 16 kHz mono PCM chunks while reading events concurrently.
$session->sendAudio($pcmBytes);

foreach ($session->events() as $event) {
    if ($event instanceof AudioDelta) {
        playAudio($event->bytes);
    }

    if ($event instanceof TranscriptDelta) {
        echo $event->delta;
    }
}

->providerOptions('google-agent-platform', [
    'raw' => ['session_resumption' => ['handle' => $resumeHandle]],
])

$session = Live::voice()
    ->model(GoogleAgentPlatform::model('gemini-live-2.5-flash-native-audio'))
    ->connect($appWebSocketTransport);

foreach (Generate::text('Tell me a story.')->model(GoogleAgentPlatform::model('google/gemini-2.5-flash'))->stream()->chunks() as $chunk) {
    echo $chunk;
}

$result = Generate::video('A cinematic ocean scene')
    ->model(GoogleAgentPlatform::model('veo-3.1-generate-001'))
    ->aspectRatio('16:9')
    ->resolution('1920x1080')
    ->run(timeout: 600);

$result = Generate::text('Explain the tradeoff.')
    ->model(GoogleAgentPlatform::model('google/gemini-3.1-pro-preview'))
    ->reasoning(\AiSdk\Reasoning::effort('medium'))
    ->run();

// Application Default Credentials (nothing to configure)
GoogleAgentPlatform::create(['project' => 'my-project', 'location' => 'us-central1']);

// Service account file
GoogleAgentPlatform::create([
    'project' => 'my-project',
    'location' => 'us-central1',
    'credentialsPath' => '/path/to/service-account.json',
]);

// Service account array
GoogleAgentPlatform::create([
    'project' => 'my-project',
    'credentials' => $decodedServiceAccountJson,
]);

// Static access token
GoogleAgentPlatform::create(['project' => 'my-project', 'accessToken' => 'ya29....']);