Skip to content

gemini-api-php/client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Generative AI PHP Client

Total Downloads Latest Version License

Generative AI PHP Client

Generative AI PHP Client allows you to use the Google's Generative AI models, like Gemini Pro and Gemini Pro Vision.

This library is not developed or endorsed by Google.

Installation

You need an API key to gain access to Google Generative AI services. Visit Google AI Studio to get an API key.

First step is to install the Generative AI PHP client with Composer.

composer require erdemkose/generative-ai-php

Generative AI PHP client does not come with an HTTP client. If you are just testing or do not have an HTTP client library in your project, you need to allow php-http/discovery composer plugin or install a PSR-18 compatible client library.

How to use

Basic text generation

$client = new GenerativeAI\Client('YOUR_GEMINI_PRO_API_TOKEN'); $response = $client->GeminiPro()->generateContent( new TextPart('PHP in less than 100 chars') ); print $response->text(); // PHP: A server-side scripting language used to create dynamic web applications. // Easy to learn, widely used, and open-source.

Multimodal input

Image input modality is only enabled for Gemini Pro Vision model

$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.

Tokens counting

$client = new GenerativeAI\Client('YOUR_GEMINI_PRO_API_TOKEN'); $response = $client->GeminiPro()->countTokens( new TextPart('PHP in less than 100 chars'), ); print $response->totalTokens; // 10

Listing models

$client = new GenerativeAI\Client('YOUR_GEMINI_PRO_API_TOKEN'); $response = $client->listModels(); print_r($response->models); //[ // [0] => GenerativeAI\Resources\Model Object // ( // [name] => models/gemini-pro // [displayName] => Gemini Pro // [description] => The best model for scaling across a wide range of tasks // ... // ) // [1] => GenerativeAI\Resources\Model Object // ( // [name] => models/gemini-pro-vision // [displayName] => Gemini Pro Vision // [description] => The best image understanding model to handle a broad range of applications // ... // ) //]