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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package google-gemini-api

Test suite Test suite Release License

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

  1. Go to the Google Cloud Console
  2. Go to IAM & Admin
  3. Go to Service Accounts
  4. Click on your project
  5. Click on "Create Service Account" if you don't have one yet
  6. Give it a name and click on "Create"
  7. Click on "Done"
  8. Go back into the service account you just created
  9. Click on "Add Key" and create a JSON key. The file will be downloaded to your computer
  10. 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

PHP Build Version
Package Version
Requires php Version ^8.3
firebase/php-jwt Version ^6.10
saloonphp/saloon Version ^3.8
illuminate/collections Version ^11.7
nesbot/carbon Version ^3.3
azjezz/psl Version ^2.9
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package xbnz/google-gemini-api contains the following files

Loading the files please wait ....