google-gemini-php/client
Community-maintained PHP client for the Google Gemini API. Send text, images, and video; run multi-turn chat with streaming; generate images and speech; structured output, function calling, code execution, grounding/search, token counting, plus file and cached-content management.
Expanded Use Cases with New Tools:
Laravel-Specific Optimizations:
Storage facade to preprocess files (e.g., OCR for scanned PDFs via spatie/pdf-to-text).Laravel\Sanctum or Laravel\Passport to secure file uploads if user-specific.spatie/laravel-geocoder to validate/transform Google Maps Tool inputs (e.g., placeId → coordinates).locations table with Laravel’s spatial extensions (e.g., point column type).FileSearchCompleted, MapsToolResponseReceived) to trigger downstream actions (e.g., updating a database, sending notifications).event(new FileSearchCompleted($fileId, $results));
Microservice Readiness:
thinkingBudget).Cache::rememberForever) or use Laravel Scout for hybrid search.New Features Compatibility:
mimeType, displayName). Use Laravel’s Storage facade to extract this:
$file = Storage::disk('s3')->get('contract.pdf');
$metadata = ['mimeType' => 'application/pdf', 'displayName' => 'NDA.pdf'];
GeminiFileSearchJob::dispatch($file, $metadata)->onQueue('gemini');
placeId, coordinates) using Laravel’s Validator:
Validator::make(['placeId' => $request->placeId], [
'placeId' => 'required|string|max:255|regex:/^[A-Za-z0-9_-]{25,}$/'
]);
places, directions) to Eloquent models or DTOs:
$places = $mapsTool->execute($query);
$location = Location::create($places->first()->toArray());
Backward Compatibility:
thinkingBudget: No breaking changes; existing configs will work with thinkingBudget omitted.thinkingBudget if unused).php artisan vendor:publish --provider="Gemini\Provider\ThinkingServiceProvider" --tag="config"
Testing Enhancements:
Storage::fake() to simulate file uploads:
Storage::fake('s3');
Storage::put('test.pdf', file_get_contents('test.pdf'));
placeId) with Pest:
test('invalid placeId throws exception', function () {
$this->expectException(InvalidArgumentException::class);
GeminiMapsTool::execute(['placeId' => 'invalid']);
});
vcr to record/replay File Search responses for deterministic tests.| Risk Area | Mitigation Strategy |
|---|---|
| File Size/Type Limits | Validate uploads with Laravel’s File::maxSize() and mimeType checks; reject early. |
| API Cost Spikes | Log File Search/Maps Tool usage (e.g., GeminiFileSearch::fire() events) and set budget alerts in Telescope. |
| Geospatial Errors | Use Laravel’s Validator::extend() to validate placeId/coordinates formats. |
| Async Processing Failures | Implement job retries with exponential backoff (retryAfter). |
| Data Privacy | Scrub PII from files before File Search (e.g., Str::of($file)->replaceMatches('/\d{3}-\d{2}-\d{4}/', 'XXX-XX-XXXX')). |
| Gemini API Changes | Abstract API calls behind an adapter interface for easier future updates. |
File Search Workflow:
Storage::temporaryUrl()).gemini_file_id) be stored in a gemini_files table for tracking?Google Maps Tool Use Cases:
required|string|max:255) and rate limiting.Cache::remember) for frequent queries?Cost Modeling:
Cache::tags() to invalidate cached results when budgets are exceeded?Error Handling:
placeId inputs trigger a Laravel notification (e.g., Notification::send($user, new InvalidLocation))?Extensibility:
HasFileSearch, HasMapsTool) for reusable logic across models?Gemini) be added for cleaner syntax (e.g., Gemini::fileSearch()->query($file))?Monitoring:
Security:
.pdf, .docx)?Laravel\Filesystem\FilesystemAdapter to preprocess files (e.g., extract text from PDFs with spatie/pdf-to-text).gemini_files table:
Schema::create('gemini_files', function (Blueprint $table) {
$table->id();
$table->string('gemini_file_id');
$table->string('original_name');
$table->string('mime_type');
$table->unsignedBigInteger('fileable_id');
$table->string('fileable_type');
$table->timestamps();
});
spatie/laravel-geocoder to validate/transform Google Maps Tool inputs.locations table with Laravel’s spatial extensions:
Schema::create('locations', function (Blueprint $table) {
$table->id();
$table->point('coordinates');
$table->string('place_id')->nullable();
$table->json('maps_tool_data');
$table->timestamps();
});
class FileSearchCompleted implements ShouldBroadcast
How can I help you explore Laravel packages today?