papi-ai / google
Google Gemini provider for PapiAI
Requires
- php: ^8.2
- ext-curl: *
- papi-ai/papi-core: ^0.15.5
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- pestphp/pest: ^3.0
- vimeo/psalm: ^6.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is not auto-updated.
Last update: 2026-11-17 14:11:53 UTC
README
Google Gemini provider for PapiAI - A simple but powerful PHP library for building AI agents.
Installation
composer require papi-ai/google
Usage
use PapiAI\Core\Agent; use PapiAI\Google\GoogleProvider; $provider = new GoogleProvider( apiKey: $_ENV['GOOGLE_API_KEY'], defaultModel: GoogleProvider::MODEL_3_8_FLASH, ); $agent = new Agent( provider: $provider, model: GoogleProvider::MODEL_3_8_FLASH, instructions: 'You are a helpful assistant.', ); $response = $agent->run('Hello!'); echo $response->text;
Available Models
Gemini (text and chat)
GoogleProvider::MODEL_4_HYDROGEN // 'gemini-4-hydrogen' GoogleProvider::MODEL_4_CARBON // 'gemini-4-carbon' GoogleProvider::MODEL_4_ARGON // 'gemini-4-argon' GoogleProvider::MODEL_3_8_FLASH // 'gemini-3.8-flash' (default) GoogleProvider::MODEL_3_6_FLASH // 'gemini-3.6-flash' GoogleProvider::MODEL_3_5_FLASH_LITE // 'gemini-3.5-flash-lite' GoogleProvider::MODEL_3_1_PRO // 'gemini-3.1-pro-preview' GoogleProvider::MODEL_3_FLASH // 'gemini-3-flash-preview' GoogleProvider::MODEL_2_5_PRO // 'gemini-2.5-pro' GoogleProvider::MODEL_2_5_FLASH // 'gemini-2.5-flash'
Image generation and editing
GoogleProvider::MODEL_3_1_FLASH_IMAGE // 'gemini-3.1-flash-image' (default) GoogleProvider::MODEL_NANO_BANANA_2_1 // 'gemini-nano-banana-2.1' GoogleProvider::MODEL_3_1_FLASH_LITE_IMAGE // 'gemini-3.1-flash-lite-image' GoogleProvider::MODEL_3_PRO_IMAGE // 'gemini-3-pro-image' GoogleProvider::MODEL_2_5_FLASH_IMAGE // 'gemini-2.5-flash-image'
The Imagen constants are still here but every one of them is @deprecated: the Imagen line
shuts down on 17 August 2026, and its :predict endpoint has no successor. Use the Gemini
image models above.
Embeddings
GoogleProvider::MODEL_EMBEDDING_2 // 'gemini-embedding-2' (default, 3072 dimensions) GoogleProvider::MODEL_EMBEDDING_001 // 'gemini-embedding-001'
Pass dimensions (768, 1536 or 3072) to truncate the vector.
Video generation
GoogleProvider::MODEL_VEO_3_1 // 'veo-3.1-generate-preview' (default) GoogleProvider::MODEL_VEO_3_1_LITE // 'veo-3.1-lite-generate-preview'
Endpoint
Chat, streaming, tools, structured output and images go through Google's Interactions API
(POST /v1beta/interactions), which Google now recommends; generateContent is legacy there.
The system message travels as system_instruction, effort as generation_config.thinking_level,
and each request carries the full conversation (store: false). Embeddings and Veo keep their own
endpoints. Gemini 3.6 and later ignore temperature, so passing one throws before the request.
Features
- Tool/function calling, including forced tool choice
- Vision/multimodal support
- Structured output (JSON mode)
- Streaming support
- Reasoning effort, mapped to Gemini's thinking levels and budgets
- Image generation and editing
- Video generation and text embeddings
Thinking counts against maxTokens. Current Gemini models think before they answer, and even a one-line question
can use a couple of hundred tokens at the default effort, so a small ceiling can run out before the
answer: with outputSchema, what comes back is then cut-off prose rather than JSON.
$response->isTruncated() reports it; raise maxTokens or lower effort.
Image Generation
use PapiAI\Google\GoogleProvider; $provider = new GoogleProvider($_ENV['GOOGLE_API_KEY']); $result = $provider->generateImage( prompt: 'A professional product photo of headphones on a white background', options: [ 'model' => GoogleProvider::MODEL_3_1_FLASH_IMAGE, 'aspectRatio' => '1:1', // 1:1, 16:9, 9:16, 4:3, 3:4, and more on 3.1 Flash Image 'imageSize' => '2K', // 1K, 2K or 4K ('0.5K' on 3.1 Flash Image) ] ); $image = $result['images'][0]; // These models return JPEG by default, so take the extension from the response // rather than assuming PNG. $extension = str_replace('image/', '', $image['mimeType']); file_put_contents("output.{$extension}", base64_decode($image['data'])); // Or save straight to disk, choosing your own path $provider->generateImageToFile( prompt: 'A modern minimalist workspace', outputPath: '/path/to/image.jpg' );
Leave aspectRatio and imageSize out and the model picks its own, rather than being forced
into a square.
These models return one image per request. Asking for numberOfImages greater than one
throws a ProviderException instead of quietly handing back a single image; call
generateImage() once per image you need.
Image Editing
$result = $provider->editImage( imageUrl: 'https://example.com/photo.jpg', prompt: 'Make the sky dramatic and overcast', ); file_put_contents('edited.jpg', base64_decode($result['images'][0]['data'])); echo $result['text']; // any commentary the model returned alongside the image
Creating It by Name
An application that picks its provider from configuration can build this one without naming its class:
use PapiAI\Core\Factory\ComposerDiscovery; use PapiAI\Core\Factory\ProviderRegistry; $registry = new ProviderRegistry(new ComposerDiscovery()); $provider = $registry->create('google', [ 'api_key' => $_ENV['GOOGLE_API_KEY'], ]);
It takes api_key (required), model, max_tokens and effort. Anything it is not given falls
back to the constructor's defaults, and a missing or unknown setting throws ConfigurationException
before any request is made.
License
MIT