Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Using Google's Gemini AI in PHP

Generative AI is all the rage these days. Many big players (OpenAI, Meta, Microsoft) have already launched their own AI models that can generate images, text, and even code. So, it’s only natural that Google has also joined the bandwagon and launched its own AI model called Gemini recently.

Now, being a PHP developer, I was curious to see if I could use this model in PHP. And that’s where I found this relatively new PHP package erdemkose/generative-ai-php which is a PHP wrapper for Google’s Gemini AI model. It allows you to conveniently interact with the Gemini AI model using PHP.

Installing the package

To get started, you can install the erdemkose/generative-ai-php package via Composer like so.

composer require erdemkose/generative-ai-php

Once installed, you can start using it in your application.

Grabbing the Google AI Studio API key

Next, you need to grab the Google AI Studio API key. You can do so by visiting the Google AI Studio page and creating a new API key for yourself.

This is currently free to use. But you need to have a Google account to use it.

Interacting with the Gemini AI model

Once you have the API key, you can start interacting with the Gemini AI model. For instance, let’s say you want to generate a response for a specific prompt, here’s how you can do it.

use GenerativeAI\Resources\Parts\TextPart;

$client = new GenerativeAI\Client('YOUR_GEMINI_PRO_API_TOKEN');

$response = $client->GeminiPro()->generateContent(
    new TextPart('what is memento mori?')
);

echo $response->text();

As you can tell, you need to pass the API key to the GenerativeAI\Client constructor. And then, you can use the generateContent method to generate the response for a specific prompt. In this case, we’re passing the TextPart which is a prompt to the generateContent method.

This will return a text response generated by the Gemini AI back.

Using images as prompts

You can also use images as prompts to generate responses. For instance, let’s say you want to generate a response for a specific image, here’s how you can do it.

$client = new GenerativeAI\Client('YOUR_GEMINI_PRO_API_TOKEN');

$response = $client->GeminiProVision()->generateContent(
    new TextPart('Explain what is in the image'),
    new ImagePart(
        MimeType::IMAGE_JPEG,
        base64_encode(file_get_contents(__DIR__ . '/assets/elephpant.jpg')),
    ),
);

print $response->text();
// The image shows an elephant standing on the Earth.
// The elephant is made of metal and has a glowing symbol on its forehead.
// The Earth is surrounded by a network of glowing lines.
// The image is set against a starry background.

The library also lets you fetch the count of tokens used for a prompt. It can list the models available for a specific API key.

Learn more about the library: erdemkose/generative-ai-php

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?