Download the PHP package derrickob/gemini-api without Composer
On this page you can find all versions of the php package derrickob/gemini-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download derrickob/gemini-api
More information about derrickob/gemini-api
Files in derrickob/gemini-api
Package gemini-api
Short Description A lightweight, efficient, custom PHP Client for seamless Google Gemini API integration.
License MIT
Homepage https://github.com/derrickobedgiu1/gemini-api
Informations about the package gemini-api
Image Credit: Google DeepMind
Custom PHP Client for Google Gemini API
[!IMPORTANT] This library is an independent creation that tries to provide access to the latest Google Gemini API features & capabilities. It is neither endorsed nor an official library. If you're looking for Official Google Gemini API libraries, please Check Here.
Built with SaloonPHP as Core Dependency.
Table of Contents
- # Custom PHP Client for Google Gemini API
- Table of Contents
- Releases
- Prerequisites
- Dependencies
- Set up your API Key
- Set Up Service Account
- Installation
- Environment Variables
- Notes
- Getting Started
- API Key
- Service Account
- Manual Credentials
- Proxy
- Cached Content Resource
- create
- delete
- get
- list
- patch
- Corpora Resource
- create
- delete
- get
- list
- patch
- query
- Corpora Document Resource
- create
- delete
- get
- list
- patch
- query
- Corpora Document Chunk Resource
- batchCreate
- batchDelete
- batchUpdate
- create
- delete
- get
- list
- patch
- Corpora Permission Resource
- create
- delete
- get
- list
- patch
- File Resource
- delete
- get
- list
- Media Resource
- upload
- Model Resource
- batchEmbedContents
- batchEmbedText
- countMessageTokens
- countTextTokens
- countTokens
- embedContent
- embedText
- generateContent
- generateMessage
- generateText
- get
- list
- Tuned Model Resource
- create
- delete
- generateContent
- get
- list
- patch
- transferOwnership
- Tuned Model Operation Resource
- cancel
- get
- list
- Tuned Model Permission Resource
- create
- delete
- get
- list
- patch
- Tests
- Support Project
Releases
This library uses the beta version of the Gemini API by default to try and ensure access to the most recent functionalities.
The versioning scheme follows the format v1.x.y
:
y
is incremented for patches and backward-compatible bug fixes.x
is incremented for new features or changes that may include breaking changes.
To ensure stability with this library, you may consider locking your composer version to a specific release. Update your package only after reviewing the changelog and ensuring you can accommodate any breaking changes in the new release. This, however, won't apply for any upstream breaking changes if you're using the default (v1beta
) API version.
Since there may be occasional breaking changes from the beta version, this library may end up having more frequent x
version increments to adapt to these changes, mostly for existing broken functionalities it previously implemented.
You can also lock your usage to v1
by explicitly defining it when constructing the Gemini class, keeping note of which features are available in v1
and those in v1beta
. There's a high chance you'll also get errors if the v1
API receives request containing v1beta
-specific parameters (which fully are), so using v1beta
might seem mandatory with this library as the entire development was based on v1beta
syntax except tuned model operations that route to v1
.
Prerequisites
Dependencies
- PHP 8.1+
- Composer
Set up your API Key
To use the Gemini API, you'll need an API key. If you don't already have one, create a key in Google AI Studio. Get an API Key
Set Up Service Account
- Go to the Google Developer Console and create a new project if you don't already have one.
- In the navigation menu, select APIs & Services > Credentials.
- Click on Create credentials and select Service account.
- Enter a name for the service account and click Create.
- Assign the necessary roles to your service account and click Continue.
- Click on Create Key, choose the JSON key type, and click Create.
- Your new public/private key pair will be downloaded to your computer; this is your service accountkey.
- Securely store the JSON file containing the key, as it allows access to the Gemini API. You'll provide the path to it in your code.
Installation
For caching access token requests from your service account JSON file, use any PSR-6 compatible cache library such as symphony/cache
(requires PHP >= 8.2).
Environment Variables
Set your environment variables:
Powershell
Cmd
Linux or macOS
Notes
- API Syntax and Naming: This library simplifies some aspects of the official API syntax and renames certain methods to avoid conflicts. When writing full requests which don't use the simplified methods, refer to the official documentation alongside the library equivalent class names to ensure accuracy. Only the few renamed classes should prevent you from writing your own documentation-led request if the simplified versions are incomplete/sub-standard for you.
- Query Parameters: Any query parameters not explicitly shown in the examples (e.g.,
pageSize
,pageToken
) can be passed as an associative array within the request. For instance:$gemini->models()->list(['pageSize' => 5, 'pageToken' => 'the-token']);
If the class names are complex to build a request, you can always build using array and passing to their fromArray
static method (available in all classes). The JSON Mode example addon in the examples/
folder uses this. Note that the field data you're passing should be compatible with the expected syntax and data of the class whose fromArray
you're trying to use.
Getting Started
API Key
The example demonstrates an endpoint request that uses an API key for authentication.
Service Account
Tuned Models and Corpora endpoints require a more secure authentication. It's easy to use your service account file.
The example uses symphony/cache
to store the access token, but you can replace it with your preferred PSR-6 cache library, or not cache access token which results in requesting new access token for every single request you make.
For security purposes, the cache configuration are left to default and if you need to modify these then please refer to documentation of cache library you're using. For symphony/cache
can be accessed here. File system cache adapter is used, but you can always use any adapter of your choice.
Manual Credentials
While not recommended for security reasons, it's possible to manually set credentials when initializing the Gemini class if you need flexibility in how you manage and provide API access in your codebase. The security of this method and logic of caching access tokens is up to you to implement as hard-coding the credentials is discouraged; use GOOGLE_APPLICATION_CREDENTIALS
in previous section instead.
Proxy
If your API use requires a proxy for all your requests, a Guzzle proxy
option is open for you to write to when constructing the Gemini class.
Cached Content Resource
create
Creates CachedContent resource.
This example uses the Sherlock Jr. movie video used in documentation. File was first uploaded with media upload
Generate Content with a Cached Content
The timeout for `generateContent` is set to 60s, request timeout to 120s. If your request is still exceeding these limits you may propose an increase.delete
Deletes CachedContent resource.
get
Reads CachedContent resource.
list
Lists CachedContents.
patch
Updates CachedContent resource (only expiration is updatable).
Corpora Resource
create
Creates an empty Corpus
.
delete
Deletes a Corpus
.
get
Gets information about a specific Corpus
.
list
Lists all Corpora
owned by the user.
patch
Updates a Corpus
.
query
Performs semantic search over a Corpus
.
Corpora Document Resource
create
Creates an empty Document
.
delete
Deletes a Document
.
get
Gets information about a specific Document
.
list
Lists all Document
s in a Corpus
.
patch
Updates a Document
.
query
Performs semantic search over a Document
.
Corpora Document Chunk Resource
batchCreate
Batch create Chunk
s.
batchDelete
Batch delete Chunk
s.
batchUpdate
Batch update Chunk
s.
create
Creates a Chunk
.
delete
Deletes a Chunk
.
get
Gets information about a specific Chunk
.
list
Lists all Chunk
s in a Document
.
patch
Updates a Chunk
.
Corpora Permission Resource
create
Create a permission to a specific resource.
Example 1: Everyone
Example 2: Group
Example 3: USER
delete
Deletes the permission.
get
Gets information about a specific Permission.
list
Lists permissions for the specific resource.
patch
Updates the permission.
File Resource
delete
Deletes the File
.
get
Gets the metadata for the given File
.
list
Lists the metadata for File
s owned by the requesting project.
Media Resource
upload
Creates a File
.
Model Resource
batchEmbedContents
Generates multiple embeddings from the model given input text in a synchronous call.
Version 1:
Version 2:
batchEmbedText
Generates multiple embeddings from the model given input text in a synchronous call.
Example 1:
Example 2:
countMessageTokens
Runs a model's tokenizer on a string and returns the token count.
countTextTokens
Runs a model's tokenizer on a text and returns the token count.
countTokens
Runs a model's tokenizer on input content and returns the token count.
embedContent
Generates an embedding from the model given an input Content
.
embedText
Generates an embedding from the model given an input message.
generateContent
Generates a response from the model given an input.
generateMessage
Generates a response from the model given an input MessagePrompt
.
generateText
Generates a response from the model given an input message.
get
Gets information about a specific Model.
list
Lists models available through the API.
Tuned Model Resource
create
Creates a tuned model.
delete
Deletes a tuned model.
generateContent
Generates a response from the model given an input
get
Gets information about a specific TunedModel.
list
Lists tuned models owned by the user.
patch
Updates a tuned model.
transferOwnership
Transfers ownership of the tuned model. The current owner will be downgraded to writer role.
Tuned Model Operation Resource
cancel
Starts asynchronous cancellation on a long-running operation.
get
Gets the latest state of a long-running operation.
list
Lists operations that match the specified filter in the request. If the server doesn't support this method, it returns UNIMPLEMENTED
.
Tuned Model Permission Resource
create
Create a permission to a specific resource.
Example 1: Everyone
Example 2: Group
Example 3: Specific User
delete
Deletes the permission.
get
Gets information about a specific Permission.
list
Lists permissions for the specific resource.
patch
Updates the permission.
Tests
Support Project
If this library has helped you, consider giving it a ⭐️ Good luck with building with the Gemini API.
All versions of gemini-api with dependencies
ext-fileinfo Version *
google/auth Version ^1.41.0
google/protobuf Version ^4.27.3
saloonphp/saloon Version ^3.10.0