A Python library simplifies building language chain applications with Gemini, leveraging batch mode for cost-effective prompt processing.
Project description
gembatch
A Python library simplifies building language chain applications with Gemini, leveraging batch mode for cost-effective prompt processing.
Introduction
Prompt chaining is a powerful technique for tackling complex tasks by linking multiple prompts together. Numerous libraries like Langchain offer convenient features for building applications with this approach. However, one crucial aspect often overlooked is cost efficiency. Many AI service providers offer batch processing modes with significant discounts (often around 50%) in exchange for longer turnaround times. While enticing, leveraging these batch discounts within a prompt chaining workflow can be challenging. Instead of executing API calls sequentially, developers must manage intricate processes:
- Batching requests: Accumulating prompts into batches.
- Asynchronous handling: Polling and waiting for batch job completion.
- Result processing: Extracting and mapping results to the correct chain segment.
This complexity is compounded by considerations like rate limits, error handling, and potential retries. Implementing such a system often leads to convoluted code, hindering readability and maintainability. This is where GemBatch shines. GemBatch is a framework designed to seamlessly integrate batch processing into prompt chaining workflows without sacrificing simplicity. It allows developers to define their prompt chains sequentially, just as they would with traditional approaches, while automatically optimizing execution using batch APIs behind the scenes. This abstraction simplifies development, improves code clarity, and unlocks significant cost savings.
Requirements
To use the GemBatch library, ensure you have the following prerequisites:
- Python 3.12 installed.
- A Firebase project set up.
- Firebase Functions configured to use Python.
- Access to Gemini models.
Make sure your environment meets these requirements to leverage the full capabilities of GemBatch.
Setup
firebase login firebase init
- create a firestore database first
- .gitignore -> add venv/
Firestore Functions Storage firestore.indexes.json Python
gcloud auth
-
gcloud config set project gembatch-test-v0-0-15
-
gcloud auth login
-
gcloud auth application-default login
-
check requirements.txt has gembatch
-
install in firebase functions's venv
-
firebase deploy --only=functions
first time may not success
-
GEMBATCH_CLOUD_STORAGE_BUCKET needs to be unique
-
firebase deploy --only=firestore
https://console.firebase.google.com/project/${PROJECT_ID}/firestore/databases/-default-/indexes
Setup Guide
Follow these steps to set up the GemBatch library in your environment:
Step 1: Install Python 3.12
Ensure you have Python 3.12 installed on your system. You can download it from the official Python website.
Step 2: Set Up Firebase
Note: For detailed instructions, you can refer to the official Firebase guide.
-
Log in to Firebase:
firebase login -
Create a Firestore database first.
-
Initialize Firebase in your project directory:
firebase initNote: Make sure to initialize at least Firestore, Functions, and Storage in your Firebase project.
-
(Optional) Add
venv/to your.gitignorefile to make Google Cloud build more efficient by ignoring virtual environment files..
Step 3: Authenticate with Google Cloud
- Set your project:
gcloud config set project $PROJECT_ID
- Authenticate with Google Cloud:
gcloud auth login
- Authenticate application default credentials:
gcloud auth application-default login
Step 4: Install Dependencies
- Ensure
gembatchis listed in yourrequirements.txtfile. - Install dependencies in Firebase Functions' virtual environment:
pip install -r requirements.txt
Step 5: Update and Deploy Firebase Functions
-
Update
firebase functions'smain.pyas shown below:from firebase_admin import initialize_app from firebase_functions import https_fn, logger from vertexai import generative_models import gembatch from gembatch import * initialize_app() # omit
-
Deploy Firebase Functions:
firebase deploy --only=functions
Note: The first deployment may not succeed. Retry if necessary.
Warning: Ensure the
GEMBATCH_CLOUD_STORAGE_BUCKETenvironment variable is globally unique for creating a Google Cloud Storage bucket.
Step 6: Initialize GemBatch
Run the following command to initialize GemBatch in your project:
gembatch init
The gembatch init command initializes the GemBatch environment by performing several tasks:
- Identifies the Firebase project and loads environment variables.
- Checks and enables necessary Google Cloud APIs like Vertex AI and BigQuery.
- Enables BigQuery audit logging.
- Creates a BigQuery dataset for storing prediction results.
- Creates a Google Cloud Storage bucket for batch processing.
- Updates Firestore indexes required for GemBatch.
- Deploys Eventarc triggers for handling events in the GemBatch environment.
Step 7: Deploy Firestore
After running gembatch init, make sure the new indexes are deployed:
firebase deploy --only=firestore
You can view your Firestore indexes at: Firestore Indexes
Example
The core of gembatch is the gembatch.submit(...) function:
def submit(
request: dict,
model: str,
handler: types.ResponseHandler,
params: dict | None = None,
) -> str:
"""Enqueue a new generation job.
Args:
request: The Gemini generation request in dictionary form.
model: The model to be used for generation.
handler: The handler for the generation job.
params: The parameters for the handler.
Returns:
The UUID of the job.
Raises:
ValueError: If the handler does not belong to a module or is a lambda function.
Example:
>>> submit(
... {"contents": [{"role": "user", "parts": [{"text": "Hi! How are you?"}]}]},
... "publishers/google/models/gemini-1.5-flash-002",
... echo_action,
... )
'123e4567-e89b-12d3-a456-426614174000'
"""
The following example demonstrates how to use the GemBatch library to submit a prompt to a Gemini model and handle the response.
from firebase_admin import initialize_app
from firebase_functions import https_fn, logger
from vertexai import generative_models
import gembatch
from gembatch import *
initialize_app()
def echo_action(response: generative_models.GenerationResponse):
logger.debug(response.to_dict())
print("Echo action.")
@https_fn.on_request()
def on_request_example(req: https_fn.Request) -> https_fn.Response:
gembatch.submit(
{
"contents": [
{
"role": "user",
"parts": [{"text": "Hi! How are you?"}],
}
],
},
"publishers/google/models/gemini-1.5-flash-002",
echo_action,
)
return https_fn.Response("OK")
-
Import Necessary Modules:
from firebase_admin import initialize_app from firebase_functions import https_fn, logger from vertexai import generative_models import gembatch from gembatch import *
-
Initialize Firebase App:
initialize_app()
-
Define a Response Handler:
def echo_action(response: generative_models.GenerationResponse): logger.debug(response.to_dict()) print("Echo action.")
echo_actionis a function that logs the response and prints a message. This function will be called when the Gemini model returns a response.Note: The handler will be called within the
handleGemBatchRequestCompletecloud function. You can check the log in the logs explorer with the following query:(resource.type = "cloud_function" resource.labels.function_name = "handleGemBatchRequestComplete") OR (resource.type = "cloud_run_revision" resource.labels.service_name = "handlegembatchrequestcomplete") -
Define an HTTP Function:
@https_fn.on_request() def on_request_example(req: https_fn.Request) -> https_fn.Response: gembatch.submit( { "contents": [ { "role": "user", "parts": [{"text": "Hi! How are you?"}], } ], }, "publishers/google/models/gemini-1.5-flash-002", echo_action, ) return https_fn.Response("Hello world!")- on_request_example is an HTTP function that will be triggered by an HTTP request.
- Inside this function, gembatch.submit is called to submit a prompt to the Gemini model. The prompt is a simple message "Hi! How are you?".
- The echo_action function is passed as the handler to process the response from the Gemini model.
- The function returns an HTTP response with the message "OK".
Supported Models
GemBatch currently supports the following models:
publishers/google/models/gemini-1.5-flash-002publishers/google/models/gemini-1.5-flash-001publishers/google/models/gemini-1.5-pro-002publishers/google/models/gemini-1.5-pro-001
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gembatch-0.1.0.tar.gz.
File metadata
- Download URL: gembatch-0.1.0.tar.gz
- Upload date:
- Size: 25.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c241606eb6bd772bc0d0feb38f6413a116227bd9c8218a4a3d933d06bb288229
|
|
| MD5 |
34d398d0fc301efb73aa0a14f51e04e9
|
|
| BLAKE2b-256 |
49d4c0c72d682c6b0d6fa236e4e4358017667488f197612cc643e2e5206eb405
|
File details
Details for the file gembatch-0.1.0-py3-none-any.whl.
File metadata
- Download URL: gembatch-0.1.0-py3-none-any.whl
- Upload date:
- Size: 26.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ed9b131a7791817dab754648817e8891b1dcf8fbb50a203d17b7faf76d4b649
|
|
| MD5 |
3e89612892a5c6556599444403352b19
|
|
| BLAKE2b-256 |
e412c6263ddf5e00633139cc2669fb84587b72d85fca0ac7d96fbfab0c84fcc6
|