aisdk/google

Official Google Gemini provider for the PHP AI SDK.


Keywords
google, ai, Gemini, llm, aisdk
License
MIT

Documentation

aisdk/google

Official Google Gemini provider for the PHP AI SDK.

Installation

composer require aisdk/google

Basic Usage

use AiSdk\Generate;
use AiSdk\Google;

$result = Generate::text()
    ->model(Google::model('gemini-3.5-flash'))
    ->instructions('Write short, clear answers.')
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text;

Configuration

Variable Description Default
GOOGLE_GENERATIVE_AI_API_KEY API key for authentication Required
GEMINI_API_KEY Fallback API key variable Required
GOOGLE_GENERATIVE_AI_BASE_URL Base URL for API requests https://generativelanguage.googleapis.com/v1beta
Google::create([
    'apiKey' => '...',
    'baseUrl' => 'https://generativelanguage.googleapis.com/v1beta',
]);

Streaming

$stream = Generate::text('Tell me a story.')
    ->model(Google::model('gemini-3.5-flash'))
    ->stream();

foreach ($stream->chunks() as $chunk) {
    echo $chunk;
}

$result = $stream->run();

Image Generation

use AiSdk\Generate;
use AiSdk\Google;

$result = Generate::image()
    ->model(Google::image('gemini-3.1-flash-image'))
    ->prompt('A clean app icon for a PHP AI SDK')
    ->aspectRatio('1:1')
    ->run();

$result->output->save(__DIR__.'/icon.png');

Speech Generation

use AiSdk\Generate;
use AiSdk\Google;

$result = Generate::speech()
    ->model(Google::speech('gemini-3.1-flash-tts-preview'))
    ->input('Say cheerfully: Have a wonderful day!')
    ->voice('Kore')
    ->run();

$result->output->save(__DIR__.'/speech.wav');

Google speech generation uses Gemini Interactions API audio responses. You can pass native speech configuration through provider options:

$result = Generate::speech('Read this as a short dialogue.')
    ->model(Google::speech('gemini-3.1-flash-tts-preview'))
    ->providerOptions('google', [
        'generation_config' => [
            'speech_config' => [
                ['speaker' => 'Joe', 'voice' => 'Kore'],
                ['speaker' => 'Jane', 'voice' => 'Puck'],
            ],
        ],
    ])
    ->run();

Provider-Specific Options

$result = Generate::text('Hello')
    ->model(Google::model('gemini-3.5-flash'))
    ->providerOptions('google', [
        'raw' => [
            'generation_config' => ['temperature' => 0.2],
            'store' => false,
        ],
    ])
    ->run();

Testing

composer test