Artificial intelligence is revolutionizing web applications, and Google’s Gemini AI offers powerful capabilities for natural language processing, text generation, and more. In this tutorial, I will show you step by step how to integrate Google Gemini AI into a Laravel 12 application.
You’ll learn how to set up the API, send requests, and use AI-generated responses in your Laravel project.
Gemini is an advanced AI model from Google that builds on previous language models like Meena and PaLM. However, Gemini is much more powerful because it can understand and respond to not just text but also code, images, audio, and video.
This ability to work with different types of content makes Gemini highly versatile and useful for many applications.
1. Multi-Modal Learning
Gemini can process and understand different types of data, including:
This allows it to perform a wide range of tasks, such as:
2. Content Generation
Gemini can create unique and creative content, including:
This makes it an excellent tool for writers, artists, and developers.
3. Scalability
Gemini is highly scalable, meaning it can be easily adapted for various industries and applications, from small businesses to large enterprises.
How to Use Gemini API Client for Laravel 12
First, make sure you have Laravel 12 installed. If not, create a new project using the following command:
laravel new laravel-gemini-ai
To use Gemini AI, you need an API key from Google AI.
Open the env file and add your API key:
GEMINI_API_KEY=your_google_gemini_api_key
create a service class and open app/Services/GeminiAIService.php and add the following code:
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class GeminiAIService
{
protected $apiUrl = 'https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent';
public function generateText($prompt)
{
$response = Http::post($this->apiUrl . '?key=' . env('GEMINI_API_KEY'), [
'prompt' => ['text' => $prompt]
]);
return $response->json();
}
}
Run the following command:
php artisan make:controller GeminiController
Open app/Http/Controllers/GeminiController.php and update it with:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Services\GeminiAIService;
class GeminiController extends Controller
{
protected $geminiService;
public function __construct(GeminiAIService $geminiService)
{
$this->geminiService = $geminiService;
}
public function generate(Request $request)
{
$request->validate([
'prompt' => 'required|string',
]);
$response = $this->geminiService->generateText($request->input('prompt'));
return response()->json($response);
}
}
Open routes/api.php and add the following route:
use App\Http\Controllers\GeminiController;
Route::post('/generate-text', [GeminiController::class, 'generate']);
You can test your API using Postman or cURL:
Example cURL command:
curl -X POST http://127.0.0.1:8000/api/generate-text \
-H "Content-Type: application/json" \
-d '{"prompt": "Explain Laravel in simple terms"}'
You might also like: