Skip to main content

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.

  1. Log in to Firebase:

    firebase login
    
  2. Create a Firestore database first.

  3. Initialize Firebase in your project directory:

    firebase init
    

    Note: Make sure to initialize at least Firestore, Functions, and Storage in your Firebase project.

  4. (Optional) Add venv/ to your .gitignore file to make Google Cloud build more efficient by ignoring virtual environment files..

Step 3: Authenticate with Google Cloud

  1. Set your project:
    gcloud config set project $PROJECT_ID
    
  2. Authenticate with Google Cloud:
    gcloud auth login
    
  3. Authenticate application default credentials:
    gcloud auth application-default login
    

Step 4: Install Dependencies

  1. Ensure gembatch is listed in your requirements.txt file.
  2. Install dependencies in Firebase Functions' virtual environment:
    pip install -r requirements.txt
    

Step 5: Update and Deploy Firebase Functions

  1. Update firebase functions's main.py as 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
    
  2. Deploy Firebase Functions:

    firebase deploy --only=functions
    

    Note: The first deployment may not succeed. Retry if necessary.

    Warning: Ensure the GEMBATCH_CLOUD_STORAGE_BUCKET environment 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")
  1. 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 *
    
  2. Initialize Firebase App:

    initialize_app()
    
  3. Define a Response Handler:

    def echo_action(response: generative_models.GenerationResponse):
        logger.debug(response.to_dict())
        print("Echo action.")
    

    echo_action is 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 handleGemBatchRequestComplete cloud 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")
    
  4. 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-002
  • publishers/google/models/gemini-1.5-flash-001
  • publishers/google/models/gemini-1.5-pro-002
  • publishers/google/models/gemini-1.5-pro-001

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gembatch-0.1.0.tar.gz (25.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gembatch-0.1.0-py3-none-any.whl (26.9 kB view details)

Uploaded Python 3

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

Hashes for gembatch-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c241606eb6bd772bc0d0feb38f6413a116227bd9c8218a4a3d933d06bb288229
MD5 34d398d0fc301efb73aa0a14f51e04e9
BLAKE2b-256 49d4c0c72d682c6b0d6fa236e4e4358017667488f197612cc643e2e5206eb405

See more details on using hashes here.

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

Hashes for gembatch-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ed9b131a7791817dab754648817e8891b1dcf8fbb50a203d17b7faf76d4b649
MD5 3e89612892a5c6556599444403352b19
BLAKE2b-256 e412c6263ddf5e00633139cc2669fb84587b72d85fca0ac7d96fbfab0c84fcc6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page