Integrate Google Gemini AI in Laravel 12

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.

Integrate Google Gemini AI in Laravel 12

What is Google's AI Model Gemini?

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.

Key Features of the Gemini AI Model

1. Multi-Modal Learning

Gemini can process and understand different types of data, including:

  1. Text
  2. Code
  3. Audio
  4. Images
  5. Video

This allows it to perform a wide range of tasks, such as:

  • Generating creative content (stories, translations, articles, etc.)
  • Understanding and writing code
  • Answering complex questions in an insightful way

2. Content Generation

Gemini can create unique and creative content, including:

  1. Text (articles, poems, translations)
  2. Code (programming solutions)
  3. Music
  4. Images

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.

 

Benefits of the Google AI Model Gemini
  1. Better Customer Support – Gemini can power AI chatbots to provide more helpful and personalized responses.
  2. Improved Education – It can create custom learning programs for students based on their needs.
  3. Medical Assistance – Doctors can use Gemini to analyze medical data and diagnose diseases more accurately.
  4. Scientific Discoveries – Researchers can use Gemini to analyze large data sets and uncover new insights.

 

How to Use Gemini API Client for Laravel 12

Step 1: Install 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

 

Step 2: Get Google Gemini API Key

To use Gemini AI, you need an API key from Google AI.

  1. Go to Google AI Studio and sign in.
  2. Generate an API key and copy it for later use

 

Step 3: Configure the API Key in Laravel

Open the env file and add your API key:

GEMINI_API_KEY=your_google_gemini_api_key

 

Step 4: Create a Service for Google Gemini AI

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();
    }
}

 

Step 5: Create a Controller

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);
    }
}

 

Step 6: Define API Route

Open routes/api.php and add the following route:

use App\Http\Controllers\GeminiController;

Route::post('/generate-text', [GeminiController::class, 'generate']);

 

Step 7: Test the API

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:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS