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.
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.
-
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.
-
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.
-
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:
-
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.
-
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.
-
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.
-
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.
-
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.
-
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:
- Efficiency: Gembatch optimizes prompt processing by grouping them into batches, reducing API calls and costs.
- Concurrency: Multiple prompt chains can progress simultaneously, improving overall throughput.
- 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
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.4.tar.gz.
File metadata
- Download URL: gembatch-0.1.4.tar.gz
- Upload date:
- Size: 24.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14bdaa7c03f5a330e4661d6e522332ef9cc85fcbb28330e94d52472777bbf826
|
|
| MD5 |
2f20ba3ecfa61de4ffd902fb8f6e11ea
|
|
| BLAKE2b-256 |
59ae1dc0239d53b4a384607f96f0c0599c1df52e6db6497d3d1ae9f83855ae2e
|
File details
Details for the file gembatch-0.1.4-py3-none-any.whl.
File metadata
- Download URL: gembatch-0.1.4-py3-none-any.whl
- Upload date:
- Size: 26.5 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 |
061c0b99bdd0a7c6d30541b38bab57d21b77afe007dbb9788570b4e68d553403
|
|
| MD5 |
acc29392c1b16b03b6c1e9d4030a3e42
|
|
| BLAKE2b-256 |
728b7e77e92526e48887a3a0f42f62c684c68ea6bb122dfdadc5c8ec343009ac
|