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.
- Erdem Köse - github.com/erdemkose
 
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-phpGenerative 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.
$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.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.$client = new GenerativeAI\Client('YOUR_GEMINI_PRO_API_TOKEN'); $response = $client->GeminiPro()->countTokens( new TextPart('PHP in less than 100 chars'), ); print $response->totalTokens;