Download the PHP package xbnz/google-gemini-api without Composer
On this page you can find all versions of the php package xbnz/google-gemini-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download xbnz/google-gemini-api
More information about xbnz/google-gemini-api
Files in xbnz/google-gemini-api
Package google-gemini-api
Short Description HTTP clients for accessing Google's AI models and OAuth2 service
License MIT
Informations about the package google-gemini-api
Google Gemini API client with OAuth2 authentication
Motivation
Other libraries rely on the generativelanguage.googleapis.com
API, which misses some models. For example, the newly-introduced "Gemini Experimental" model, which is free (!) cannot be accessed through that API. This library solves that issue.
[!NOTE] The Gemini Experimental API is not guaranteed to last forever. If you are using a model that is available on the
generativelanguage.googleapis.com
API, you can use this library to access it as well.
Installation
Prerequisites
Creating a service account on Google Cloud
- Go to the Google Cloud Console
- Go to IAM & Admin
- Go to Service Accounts
- Click on your project
- Click on "Create Service Account" if you don't have one yet
- Give it a name and click on "Create"
- Click on "Done"
- Go back into the service account you just created
- Click on "Add Key" and create a JSON key. The file will be downloaded to your computer
- Grab the necessary information from the JSON file:
client_email
private_key
Getting started
Sample application
Here is a sample Laravel command that uses this library. The service provider is where the interfaces are bound to concretes.
Registering to the dependency injection container (optional)
Using the AIPlatform service for single or multi-turn conversations
`
[!NOTE] You may new up the classes directly if you don't want to use a dependency injection container
[!NOTE] You may serialize and store the TextPart::class responses from the model in your database. This will allow you to conduct a persistent chat session with the model, resembling something like teh ChatGPT interface.
Core concepts
Extensible authentication
The Google aiplatform.googleapis.com
API allows several forms of authentication. This library is opinionated toward using the access_token
derived from your service account. However, the underlying HTTP client of this library (SaloonPHP) allows "Connector" classes to be extended with any authenticator before being newed up. As a result, you may create your own Saloon authentication strategy and pass it to the constructor:
Bearer token authentication
The provided GoogleOAuthInterface::token()
method allows you to fetch a new token for bearer authentication. Do keep in mind that if you use this tool for authenticating the AIPlatform, the process generates a new token every time the method is called which may or may not be desirable.
Caching your token until expiry (Laravel example)
This package does not provide a caching mechanism – for example using the PSR cache interface – due to the dead simple cache integration of most modern frameworks. Here is a Laravel example:
Logging and exception handling
Providing your own PSR-compatible logger
This library uses the PSR logger interface for logging. You may provide your own logger by passing it to the constructor of the service:
` The same functionality exists on the OAuth2 service.
Lifecycle hooks for requests and responses
This is not recommended for basic use cases. However, APIs change and maintainers are lazy. If Google adds a new field to a response which I cannot work on, you are free to hook into the response and create your own DTOs if you don't want to make a PR.
`
Retrying failed requests
When using free Gemini models, strict rate limits are imposed. You may decide to retry when 429 responses are encountered.
Prerequisites
A Guzzle retry middleware is not provided. You may implement your own or use a library of your choice. Example:
Testing
This library provides fake implementations for testing purposes. For example, you may use the GoogleOAuth2ServiceFake::class
like so:
Calling code
Test code
[!NOTE] The same concept can be applied to the AIPlatform service. The fake implementation is called
GoogleAIPlatformServiceFake::class
and provides similar test assertions
All versions of google-gemini-api with dependencies
firebase/php-jwt Version ^6.10
saloonphp/saloon Version 3.10.1
illuminate/collections Version ^11.7
nesbot/carbon Version ^3.3
azjezz/psl Version ^3.1