Skip to main content

A Python library simplifies building language chain applications with Gemini, leveraging batch mode for cost-effective prompt processing.

Project description

PyPI - Python Version PyPi License - MIT Firebase

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.

Example at a Glance

This example demonstrates how to create a prompt chain using gembatch.submit() within your Firebase environment.

import gembath


# Task A
def task_a_prompt1():
   gembatch.submit(
       {
           "contents": [
               {
                   "role": "user",
                   "parts": [{"text": "some prompts..."}],
               }
           ],
       }, # prompt 1
       "publishers/google/models/gemini-1.5-pro-002",
       task_a_prompt2
   )


def task_a_prompt2(response: generative_models.GenerationResponse):
   gembatch.submit(
       {
           "contents": [
               {
                   "role": "model",
                   "parts": [{"text": response.text}],
               },
               {
                   "role": "user",
                   "parts": [{"text": "some prompts..."}],
               }
           ],
       }, # prompt 2
       "publishers/google/models/gemini-1.5-pro-002",
       task_a_output
   )


def task_a_output(response: generative_models.GenerationResponse):
   print(response.text)

The code defines three functions: task_a_prompt1, task_a_prompt2, and task_a_output. These functions represent the steps in your prompt chain.

  1. task_a_prompt1():

    • This function initiates the prompt chain.
    • It uses gembatch.submit() to send the first prompt to Gemini.
    • The prompt is defined as a dictionary, following the structure of GenerateContentRequest
    • It specifies the Gemini model to use ("publishers/google/models/gemini-1.5-pro-002").
    • It designates task_a_prompt2 as the callback function to be executed when the response for this prompt is received.
  2. task_a_prompt2(response):

    • This function is the callback function for the first prompt. It receives the response from Gemini as an argument.
    • It constructs the second prompt, including the model's response from the previous step and a new user message.
    • It again uses gembatch.submit() to send this prompt to Gemini.
    • It specifies the same Gemini model.
    • It sets task_a_output as the callback function for the second prompt.
  3. task_a_output(response):

    • This function is the callback function for the second prompt.
    • It receives the final response from Gemini.
    • It simply prints the response text.

The chain is started by calling the first function:

task_a_prompt1()

This pattern of chaining prompts and callbacks allows you to create complex interactions with the language model, where each step depends on the output of the previous step. The gembatch library handles the underlying communication with Gemini and manages the execution of the prompt chain using efficient batch jobs. This not only simplifies development but also reduces costs by taking advantage of Gemini's batch pricing, which offers a 50% discount compared to individual requests.

How Gembatch Works

This flowchart illustrates how Gembatch efficiently manages multiple prompt chains concurrently using batch processing. Here's a breakdown:

  1. Independent Prompt Chains:

    The diagram depicts three separate prompt chains (Task A, Task B, Task C), each with its own sequence of prompts and responses. Each chain represents a distinct conversation or task with the language model.

  2. Gembatch Submission:

    As each prompt in a chain is ready for submission, Gembatch's submit() function is called. Instead of sending the prompt to Gemini immediately, Gembatch adds it to a job queue.

  3. Batch Formation:

    Gembatch intelligently groups prompts from different chains into batches. This is shown in the "Job Queue" where "Batch 1" contains "Prompt 1" from each of the tasks.

  4. Batch Execution:

    Gembatch periodically sends these batches to Gemini for processing. This minimizes the number of interactions with the API and leverages Gemini's batch discounts.

  5. Response Handling:

    When Gemini returns responses for a batch, Gembatch routes them back to the appropriate prompt chains. For instance, "Responses Batch 1" is distributed back to each task, triggering the next step in their respective chains.

  6. Chain Continuation:

    Each task processes the response and, if necessary, generates the next prompt in its sequence. This continues until the chain reaches its "Output" stage, signifying the completion of that specific task.

Key Takeaways:

  1. Efficiency: Gembatch optimizes prompt processing by grouping them into batches, reducing API calls and costs.
  2. Concurrency: Multiple prompt chains can progress simultaneously, improving overall throughput.
  3. Simplified Management: Gembatch handles the complexities of batching and response routing, allowing you to focus on designing your prompt chains.

Ready to get started?

Now that you have an overview of Gembatch, learn how to install and configure it in your Firebase project with our step-by-step Installation Guide.

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.6.dev0.tar.gz (25.1 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.6.dev0-py3-none-any.whl (26.8 kB view details)

Uploaded Python 3

File details

Details for the file gembatch-0.1.6.dev0.tar.gz.

File metadata

  • Download URL: gembatch-0.1.6.dev0.tar.gz
  • Upload date:
  • Size: 25.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.8

File hashes

Hashes for gembatch-0.1.6.dev0.tar.gz
Algorithm Hash digest
SHA256 e6875aa081ba24aae85a27c673b11a2028562e78fb301f6c78e1eba5cc35aa3d
MD5 212eb8db5f887fa58f2f4180a528e644
BLAKE2b-256 ad8cf2e205c85a40d08bd07586fe928aeeabb7e6889800a09360ec3be7250f62

See more details on using hashes here.

File details

Details for the file gembatch-0.1.6.dev0-py3-none-any.whl.

File metadata

  • Download URL: gembatch-0.1.6.dev0-py3-none-any.whl
  • Upload date:
  • Size: 26.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.8

File hashes

Hashes for gembatch-0.1.6.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 5384eb2c13442024c6c3651209a22ecf0f9c0b2e880da428b11b16b5dc5b9c36
MD5 2b28554f26936ead476edc38bf8ff8a5
BLAKE2b-256 6fddf316e57de37941a9222666f27368e893af93f02c322c0cfcb0e26b7d2066

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