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.

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 gemini-api

Image Credit: Google DeepMind

Packagist Version GitHub License

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

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:

  1. y is incremented for patches and backward-compatible bug fixes.
  2. 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

  1. PHP 8.1+
  2. 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

  1. Go to the Google Developer Console and create a new project if you don't already have one.
  2. In the navigation menu, select APIs & Services > Credentials.
  3. Click on Create credentials and select Service account.
  4. Enter a name for the service account and click Create.
  5. Assign the necessary roles to your service account and click Continue.
  6. Click on Create Key, choose the JSON key type, and click Create.
  7. Your new public/private key pair will be downloaded to your computer; this is your service accountkey.
  8. 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

  1. 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.
  2. 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 Documents in a Corpus.

patch

Updates a Document.

query

Performs semantic search over a Document.

Corpora Document Chunk Resource

batchCreate

Batch create Chunks.

batchDelete

Batch delete Chunks.

batchUpdate

Batch update Chunks.

create

Creates a Chunk.

delete

Deletes a Chunk.

get

Gets information about a specific Chunk.

list

Lists all Chunks 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 Files 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

PHP Build Version
Package Version
Requires php Version ^8.1
ext-fileinfo Version *
google/auth Version ^1.41.0
google/protobuf Version ^4.27.3
saloonphp/saloon Version ^3.10.0
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 derrickob/gemini-api contains the following files

Loading the files please wait ....