PHP code example of liteopensource / gemini-lite-laravel
1. Go to this page and download the library: Download liteopensource/gemini-lite-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/ */
liteopensource / gemini-lite-laravel example snippets
artisan vendor:publish --tag="geminilite-config"
use LiteOpenSource\GeminiLiteLaravel\Src\Facades\Gemini;
$chat = Gemini::newChat();
$chat->changeGeminiModel("gemini-2.0-flash"); // Example using new model
$currentConfig = $chat->getGeminiModelConfig();
$gemini = Gemini::newChat();
// Make some prompts
$response1 = $gemini->newPrompt('Hello');
$response2 = $gemini->newPrompt('How are you?');
// Get the full chat history
$history = $gemini->getHistory();
// The history contains all messages exchanged, including both user prompts and model responses
// Each message contains:
// - role: "user" or "model"
// - parts: array containing the text or file data
use LiteOpenSource\GeminiLiteLaravel\Src\Traits\HasGeminiRoles;
class User extends Authenticatable
{
use HasGeminiRoles;
}
$testUser = User::create([
'name' => 'John',
'email' => '[email protected]',
'password'=> Hash::make("password"),
]);
/*
You can assign the role by the
index or name of the role in the database
$testUser2->assignGeminiRole('limited_user');
*/
$testUser->assignGeminiRole(1);
// $user MUST BE an instance of model User
$user = User::find(1);
$canMakeRequest = $user->canMakeRequestToGemini();
// $user MUST BE an instance of model User
$user = User::find(1);
$canMakeRequest = $user->isActiveInGemini();
// $user MUST BE an instance of model User
$user = User::find(1);
$user->updateUsageTracking($tokens);
use LiteOpenSource\GeminiLiteLaravel\Src\Facades\Embedding;
// Generate embedding for a single text
$embedding = Embedding::embedText("Hello world");
// Generate batch embeddings
$embeddings = Embedding::embedBatch([
"First text",
"Second text",
"Third text"
]);
// With additional options
$embedding = Embedding::embedText($text, [
'taskType' => 'SEMANTIC_SIMILARITY',
'title' => 'Document Title'
]);
$gemini = Gemini::newChat();
$response = $gemini->newPrompt('How much is 1 + 1?');
$followUp = $gemini->newPrompt('Add 8 to the previous result');
$testImagePath = storage_path('app/public/test_image.jpeg');
$uploadedFile = UploadFileToGemini::processFileFromPath($testImagePath);
$gemini = Gemini::newChat();
$response = $gemini->newPrompt(
"What do you see in this image?",
$uploadedFile->getUri(),
$uploadedFile->getMimeType()
);
use LiteOpenSource\GeminiLiteLaravel\Src\Facades\Gemini;
$geminiChat = Gemini::newChat();
// Get initial configuration
$initialModelConfig = $geminiChat->getGeminiModelConfig();
// Change configuration
$geminiChat->setGeminiModelConfig(2, 64, 1, 8192, 'text/plain');
// Get updated configuration
$updatedModelConfig = $geminiChat->getGeminiModelConfig();
// Now you can compare or use these configurations
$tokens = GeminiTokenCount::coutTextTokens("Hello Gemini can you write a funny story");