### Install Gemini Parallel Package Source: https://github.com/miroblog/gemini-parallel/blob/main/README.md Installs the Gemini Parallel package and its dependencies using pip. It supports installation from a requirements file or in development mode for active coding. ```bash pip install -r requirements.txt ``` ```bash pip install -e . ``` -------------------------------- ### Run Gemini Parallel Examples Source: https://github.com/miroblog/gemini-parallel/blob/main/README.md Executes the example Python scripts included with the Gemini Parallel package to demonstrate its functionalities. ```bash # Basic text processing python examples/basic_parallel.py # Multimodal with images python examples/with_images.py # Structured output python examples/structured_output.py ``` -------------------------------- ### Configure Environment Variables for Gemini API and MongoDB Source: https://github.com/miroblog/gemini-parallel/blob/main/README.md Sets up essential environment variables for the Gemini Parallel package. This involves copying a template file and providing your Gemini API key and MongoDB connection URI. ```bash # Gemini API Key GEMINI_API_KEY=your_gemini_api_key_here # MongoDB Connection URI MONGODB_URI=mongodb://localhost:27017 ``` -------------------------------- ### Basic Text Processing with Gemini Parallel Source: https://github.com/miroblog/gemini-parallel/blob/main/README.md Demonstrates how to perform parallel text-based queries using the Gemini API. It initializes the ParallelExecutor, defines prompts, runs them in parallel, and prints the status and responses. ```python import asyncio import os from dotenv import load_dotenv from gemini_parallel import ParallelExecutor load_dotenv() async def main(): executor = ParallelExecutor( model="gemini-2.0-flash", max_concurrent=50, mongodb_uri=os.getenv("MONGODB_URI") ) prompts = [ "Explain AI in simple terms", "What is machine learning?", "Define deep learning" ] results = await executor.run_parallel( items=prompts, mode="text" ) for result in results: print(f"Status: {result['status']}") if result['status'] == 'success': print(f"Response: {result['response']}") asyncio.run(main()) ``` -------------------------------- ### Multimodal Processing with Gemini Parallel Source: https://github.com/miroblog/gemini-parallel/blob/main/README.md Illustrates how to process images using the Gemini API in parallel. It utilizes ContentBuilder to create image-based prompts and runs them using the ParallelExecutor. ```python from gemini_parallel import ParallelExecutor, ContentBuilder # Create content with images contents = [ ContentBuilder.with_image("image1.jpg", "Describe this image"), ContentBuilder.with_image("image2.jpg", "What's in this photo?"), ContentBuilder.with_images(["img1.jpg", "img2.jpg"], "Compare these images") ] executor = ParallelExecutor(mongodb_uri=os.getenv("MONGODB_URI")) results = await executor.run_parallel( items=contents, mode="multimodal" ) ``` -------------------------------- ### Structured Output Processing with Gemini Parallel and Pydantic Source: https://github.com/miroblog/gemini-parallel/blob/main/README.md Shows how to obtain structured output from the Gemini API using Pydantic models. It defines a Pydantic schema for the expected output and uses it with the ParallelExecutor. ```python from pydantic import BaseModel from typing import List class Analysis(BaseModel): sentiment: str confidence: float keywords: List[str] executor = ParallelExecutor(mongodb_uri=os.getenv("MONGODB_URI")) results = await executor.run_parallel( items=["Great product!", "Terrible service"], mode="structured", response_schema=Analysis ) ``` -------------------------------- ### Python Session Management with ParallelExecutor Source: https://github.com/miroblog/gemini-parallel/blob/main/README.md Demonstrates how to create a ParallelExecutor, access its session ID, run parallel tasks, and query MongoDB for calls within that session. This helps in grouping and debugging related API calls. ```python executor = ParallelExecutor(mongodb_uri=uri) print(f"Session ID: {executor.session_id}") # All calls in this session will be tagged with this ID results = await executor.run_parallel(items) # Query MongoDB for this session's calls db.api_calls.find({"session_id": executor.session_id}) ``` -------------------------------- ### Querying Gemini Parallel Session Statistics and Logs Source: https://github.com/miroblog/gemini-parallel/blob/main/README.md Provides methods to retrieve statistics and individual call logs from MongoDB. It allows for calculating success rates and iterating through recorded API calls. ```python # Get session statistics stats = await executor.get_session_stats() print(f"Total calls: {stats['total_calls']}") print(f"Success rate: {stats['successful_calls']/stats['total_calls']*100:.1f}%") # Get all calls for session calls = await executor.get_session_calls() for call in calls: print(f"{call['request_id']}: {call['status']} - {call['duration_ms']}ms") ``` -------------------------------- ### MongoDB Log Structure for Gemini API Calls Source: https://github.com/miroblog/gemini-parallel/blob/main/README.md Defines the schema for logging individual Gemini API calls to MongoDB. It includes details such as session ID, request status, prompt, response, error messages, duration, and token counts. ```json { "_id": ObjectId, "session_id": "uuid-string", "request_id": "req_0", "status": "success|failed", "prompt": "...", "response": "...", "error": "...", "duration_ms": 1234.5, "timestamp": ISODate, "model": "gemini-2.0-flash", "mode": "text|multimodal|structured", "tokens_input": 100, "tokens_output": 200 } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.