PHP code example of gemini-api-php / laravel

1. Go to this page and download the library: Download gemini-api-php/laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gemini-api-php / laravel example snippets


use GeminiAPI\Laravel\Facades\Gemini;

print Gemini::generateText('PHP in less than 100 chars');
// PHP: A server-side scripting language used to create dynamic web applications.
// Easy to learn, widely used, and open-source.

use GeminiAPI\Laravel\Facades\Gemini;

print Gemini::generateTextUsingImageFile(
    'image/jpeg',
    'elephpant.jpg',
    'Explain what is in the image',
);
// 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.

use GeminiAPI\Laravel\Facades\Gemini;

print Gemini::generateTextUsingImage(
    'image/jpeg',
    base64_encode(file_get_contents('elephpant.jpg')),
    'Explain what is in the image',
);
// 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.

use GeminiAPI\Laravel\Facades\Gemini;

$chat = Gemini::startChat();

print $chat->sendMessage('Hello World in PHP');
// echo "Hello World!";
// This code will print "Hello World!" to the standard output.

print $chat->sendMessage('in Go');
// fmt.Println("Hello World!")
// This code will print "Hello World!" to the standard output.

use GeminiAPI\Laravel\Facades\Gemini;

$history = [
    [
        'message' => 'Hello World in PHP',
        'role' => 'user',
    ],
    [
        'message' => <<<MESSAGE
            echo "Hello World!";

            This code will print "Hello World!" to the standard output.
            MESSAGE,
        'role' => 'model',
    ],
];
$chat = Gemini::startChat($history);

print $chat->sendMessage('in Go');
// fmt.Println("Hello World!")
// This code will print "Hello World!" to the standard output.

use GeminiAPI\Laravel\Facades\Gemini;

print_r(Gemini::embedText('PHP in less than 100 chars'));
// [
//    [0] => 0.041395925
//    [1] => -0.017692696
//    ...
// ]

use GeminiAPI\Laravel\Facades\Gemini;

print Gemini::countTokens('PHP in less than 100 chars');
// 10

use GeminiAPI\Laravel\Facades\Gemini;

print_r(Gemini::listModels());
//[
//  [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
//      ...
//    )
//]

use GeminiAPI\Laravel\Facades\Gemini;

$client = Gemini::client();
shell
composer 
shell
php artisan vendor:publish --provider=GeminiAPI\Laravel\ServiceProvider