Gemini API 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.
- Erdem Köse - github.com/erdemkose
You need an API key to gain access to Google's Gemini API. Visit Google AI Studio to get an API key.
First step is to install the Gemini API PHP client with Composer.
composer require gemini-api-php/clientGemini API 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.
$client = new GeminiAPI\Client('GEMINI_API_KEY'); $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.Image input modality is only enabled for Gemini Pro Vision model
$client = new GeminiAPI\Client('GEMINI_API_KEY'); $response = $client->geminiProVision()->generateContent( new TextPart('Explain what is in the image'), new ImagePart( MimeType::IMAGE_JPEG, base64_encode(file_get_contents('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.$client = new GeminiAPI\Client('GEMINI_API_KEY'); $chat = $client->geminiPro()->startChat(); $response = $chat->sendMessage(new TextPart('Hello World in PHP')); print $response->text(); $response = $chat->sendMessage(new TextPart('in Go')); print $response->text();<?php echo "Hello World!"; ?> This code will print "Hello World!" to the standard output. package main import "fmt" func main() { fmt.Println("Hello World!") } This code will print "Hello World!" to the standard output. $client = new GeminiAPI\Client('GEMINI_API_KEY'); $history = [ Content::text('Hello World in PHP', Role::User), Content::text( <<<TEXT <?php echo "Hello World!"; ?> This code will print "Hello World!" to the standard output. TEXT, Role::Model, ), ]; $chat = $client->geminiPro() ->startChat() ->withHistory($history); $response = $chat->sendMessage(new TextPart('in Go')); print $response->text();package main import "fmt" func main() { fmt.Println("Hello World!") } This code will print "Hello World!" to the standard output. Requires
curlextension to be enabled
In the streaming response, the callback function will be called whenever a response is returned from the server.
Long responses may be broken into separate responses, and you can start receiving responses faster using a content stream.
$client = new GeminiAPI\Client('GEMINI_API_KEY'); $callback = function (GenerateContentResponse $response): void { static $count = 0; print "\nResponse #{$count}\n"; print $response->text(); $count++; }; $client->geminiPro()->generateContentStream( $callback, new TextPart('PHP in less than 100 chars') ); // Response #0 // PHP: a versatile, general-purpose scripting language for web development, popular for // Response #1 // its simple syntax and rich library of functions.Requires
curlextension to be enabled
$client = new GeminiAPI\Client('GEMINI_API_KEY'); $history = [ Content::text('Hello World in PHP', Role::User), Content::text( <<<TEXT <?php echo "Hello World!"; ?> This code will print "Hello World!" to the standard output. TEXT, Role::Model, ), ]; $chat = $client->geminiPro() ->startChat() ->withHistory($history); $callback = function (GenerateContentResponse $response): void { static $count = 0; print "\nResponse #{$count}\n"; print $response->text(); $count++; }; $chat->sendMessageStream($callback, new TextPart('in Go'));Response #0 package main import "fmt" func main() { Response #1 fmt.Println("Hello World!") } This code will print "Hello World!" to the standard output. $client = new GeminiAPI\Client('GEMINI_API_KEY'); $response = $client->embeddingModel(ModelName::Embedding) ->embedContent( new TextPart('PHP in less than 100 chars'), ); print_r($response->embedding->values); // [ // [0] => 0.041395925 // [1] => -0.017692696 // ... // ]$client = new GeminiAPI\Client('GEMINI_API_KEY'); $response = $client->geminiPro()->countTokens( new TextPart('PHP in less than 100 chars'), ); print $response->totalTokens; // 10$client = new GeminiAPI\Client('GEMINI_API_KEY'); $response = $client->listModels(); print_r($response->models); //[ // [0] => GeminiAPI\Resources\Model Object // ( // [name] => models/gemini-pro // [displayName] => Gemini Pro // [description] => The best model for scaling across a wide range of tasks // ... // ) // [1] => GeminiAPI\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 // ... // ) //]