1. Go to this page and download the library: Download erdemkose/generative-ai-php 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/ */
erdemkose / generative-ai-php example snippets
use GeminiAPI\Client;
use GeminiAPI\Resources\ModelName;
use GeminiAPI\Resources\Parts\TextPart;
$client = new Client('GEMINI_API_KEY');
$response = $client->generativeModel(ModelName::GEMINI_PRO)->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.
use GeminiAPI\Client;
use GeminiAPI\Resources\ModelName;
use GeminiAPI\Resources\Parts\TextPart;
$client = new Client('GEMINI_API_KEY');
$response = $client->withV1BetaVersion()
->generativeModel(ModelName::GEMINI_1_5_FLASH)
->withSystemInstruction('You are a cat. Your name is Neko.')
->generateContent(
new TextPart('PHP in less than 100 chars'),
);
print $response->text();
// Meow? echo 'Hello, world!';
use GeminiAPI\Client;
use GeminiAPI\Enums\MimeType;
use GeminiAPI\Resources\ModelName;
use GeminiAPI\Resources\Parts\ImagePart;
use GeminiAPI\Resources\Parts\TextPart;
$client = new Client('GEMINI_API_KEY');
$response = $client->generativeModel(ModelName::GEMINI_PRO)->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.
use GeminiAPI\Client;
use GeminiAPI\Resources\ModelName;
use GeminiAPI\Resources\Parts\TextPart;
$client = new Client('GEMINI_API_KEY');
$chat = $client->generativeModel(ModelName::GEMINI_PRO)->startChat();
$response = $chat->sendMessage(new TextPart('Hello World in PHP'));
print $response->text();
$response = $chat->sendMessage(new TextPart('in Go'));
print $response->text();
use GeminiAPI\Client;
use GeminiAPI\Enums\Role;
use GeminiAPI\Resources\Content;
use GeminiAPI\Resources\ModelName;
use GeminiAPI\Resources\Parts\TextPart;
$history = [
Content::text('Hello World in PHP', Role::User),
Content::text(
<<<TEXT
echo "Hello World!";
use GeminiAPI\Client;
use GeminiAPI\Resources\ModelName;
use GeminiAPI\Resources\Parts\TextPart;
use GeminiAPI\Responses\GenerateContentResponse;
$callback = function (GenerateContentResponse $response): void {
static $count = 0;
print "\nResponse #{$count}\n";
print $response->text();
$count++;
};
$client = new Client('GEMINI_API_KEY');
$client->generativeModel(ModelName::GEMINI_PRO)->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.
use GeminiAPI\Client;
use GeminiAPI\Enums\Role;
use GeminiAPI\Resources\Content;
use GeminiAPI\Resources\ModelName;
use GeminiAPI\Resources\Parts\TextPart;
use GeminiAPI\Responses\GenerateContentResponse;
$history = [
Content::text('Hello World in PHP', Role::User),
Content::text(
<<<TEXT
echo "Hello World!";
use GeminiAPI\Client;
use GeminiAPI\Resources\ModelName;
use GeminiAPI\Resources\Parts\TextPart;
$client = new Client('GEMINI_API_KEY');
$response = $client->embeddingModel(ModelName::EMBEDDING_001)
->embedContent(
new TextPart('PHP in less than 100 chars'),
);
print_r($response->embedding->values);
// [
// [0] => 0.041395925
// [1] => -0.017692696
// ...
// ]
use GeminiAPI\Client;
use GeminiAPI\Resources\ModelName;
use GeminiAPI\Resources\Parts\TextPart;
$client = new Client('GEMINI_API_KEY');
$response = $client->generativeModel(ModelName::GEMINI_PRO)->countTokens(
new TextPart('PHP in less than 100 chars'),
);
print $response->totalTokens;
// 10
use GeminiAPI\Client;
$client = new 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
// ...
// )
//]
use GeminiAPI\Client;
use GeminiAPI\Resources\ModelName;
use GeminiAPI\Resources\Parts\TextPart;
$client = (new Client('GEMINI_API_KEY'))
->withV1BetaVersion();
$response = $client->generativeModel(ModelName::GEMINI_PRO)->countTokens(
new TextPart('PHP in less than 100 chars'),
);
print $response->totalTokens;
// 10
use GeminiAPI\Client;
use GeminiAPI\Enums\HarmCategory;
use GeminiAPI\Enums\HarmBlockThreshold;
use GeminiAPI\GenerationConfig;
use GeminiAPI\Resources\ModelName;
use GeminiAPI\Resources\Parts\TextPart;
use GeminiAPI\SafetySetting;
$safetySetting = new SafetySetting(
HarmCategory::HARM_CATEGORY_HATE_SPEECH,
HarmBlockThreshold::BLOCK_LOW_AND_ABOVE,
);
$generationConfig = (new GenerationConfig())
->withCandidateCount(1)
->withMaxOutputTokens(40)
->withTemperature(0.5)
->withTopK(40)
->withTopP(0.6)
->withStopSequences(['STOP']);
$client = new Client('GEMINI_API_KEY');
$response = $client->generativeModel(ModelName::GEMINI_PRO)
->withAddedSafetySetting($safetySetting)
->withGenerationConfig($generationConfig)
->generateContent(
new TextPart('PHP in less than 100 chars')
);
use GeminiAPI\Client as GeminiClient;
use GeminiAPI\Resources\ModelName;
use GeminiAPI\Resources\Parts\TextPart;
use GuzzleHttp\Client as GuzzleClient;
$guzzle = new GuzzleClient([
'proxy' => 'http://localhost:8125',
]);
$client = new GeminiClient('GEMINI_API_KEY', $guzzle);
$response = $client->generativeModel(ModelName::GEMINI_PRO)->generateContent(
new TextPart('PHP in less than 100 chars')
);
use GeminiAPI\Client;
use GeminiAPI\Resources\ModelName;
use GeminiAPI\Resources\Parts\TextPart;
use GeminiAPI\Responses\GenerateContentResponse;
$callback = function (GenerateContentResponse $response): void {
print $response->text();
};
$ch = curl_init();
curl_setopt($ch, \CURLOPT_PROXY, 'http://localhost:8125');
$client = new Client('GEMINI_API_KEY');
$client->withRequestHeaders([
'User-Agent' => 'My Gemini-backed app'
])
->generativeModel(ModelName::GEMINI_PRO)
->generateContentStream(
$callback,
[new TextPart('PHP in less than 100 chars')],
$ch,
);
shell
composer
text
echo "Hello World!";
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.