Spaces:
Sleeping
Sleeping
File size: 27,563 Bytes
cacd4d0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
"""
Batch LLM Client for cost-effective processing using Gemini Batch API.
This client provides 50% cost savings by using Google's Gemini Batch API
instead of real-time API calls. Ideal for large-scale prompt optimization
where latency is acceptable.
Features:
- 50% cost reduction compared to standard API
- Automatic batching and job management
- Built-in retry and polling logic
- Thread-safe operation
- Comprehensive error handling
Author: GEPA Optimizer Team
"""
import os
import json
import time
import logging
import tempfile
import io
from pathlib import Path
from typing import Dict, List, Any, Optional, Tuple
from .base_llm import BaseLLMClient
try:
from PIL import Image
PIL_AVAILABLE = True
except ImportError:
PIL_AVAILABLE = False
Image = None
try:
from google import genai
from google.genai import types
GENAI_AVAILABLE = True
except ImportError:
GENAI_AVAILABLE = False
genai = None
types = None
logger = logging.getLogger(__name__)
class BatchLLMClient(BaseLLMClient):
"""
Batch LLM client that uses Gemini Batch API for cost-effective processing.
This client processes multiple requests together in batch jobs, providing:
- 50% cost savings vs standard API
- No rate limit impact
- Automatic job management and polling
Usage:
>>> from gepa_optimizer.llms import BatchLLMClient
>>>
>>> client = BatchLLMClient(
... provider="google",
... model_name="gemini-2.5-flash",
... api_key="your-key",
... batch_size=20,
... polling_interval=30
... )
>>>
>>> # Use just like VisionLLMClient - adapter handles the rest!
>>> result = client.generate(
... system_prompt="You are a helpful assistant",
... user_prompt="Analyze this image",
... image_base64="..."
... )
Performance Note:
Batch processing adds latency (30s+ polling time) but reduces costs by 50%.
Choose this mode for large-scale optimization where cost > speed.
"""
def __init__(
self,
provider: str,
model_name: str,
api_key: Optional[str] = None,
batch_size: int = 20,
polling_interval: int = 30,
max_polling_time: int = 3600,
temp_dir: str = ".gepa_batch_temp",
**kwargs
):
"""
Initialize Batch LLM Client.
Args:
provider: Must be "google" or "gemini"
model_name: Gemini model (e.g., "gemini-2.5-flash", "gemini-1.5-flash")
api_key: Google API key (defaults to GEMINI_API_KEY env var)
batch_size: Number of samples to process per batch job (1-100)
polling_interval: Seconds between job status checks (default: 30)
max_polling_time: Maximum seconds to wait for job completion (default: 3600)
temp_dir: Directory for temporary files (default: ".gepa_batch_temp")
**kwargs: Additional parameters
Raises:
ValueError: If provider is not Google/Gemini
ImportError: If google-genai is not installed
"""
super().__init__(provider=provider, model_name=model_name, **kwargs)
# Validate provider
if provider.lower() not in ["google", "gemini"]:
raise ValueError(
f"BatchLLMClient only supports Google/Gemini provider. Got: {provider}"
)
# Check dependencies
if not GENAI_AVAILABLE:
raise ImportError(
"google-genai not installed. Install with: pip install google-genai"
)
# Configuration
self.batch_size = batch_size
self.polling_interval = polling_interval
self.max_polling_time = max_polling_time
self.temp_dir = Path(temp_dir)
self.temp_dir.mkdir(exist_ok=True)
# Initialize Gemini client
from ..utils.api_keys import APIKeyManager
self.api_key = api_key or APIKeyManager().get_api_key("google")
if not self.api_key:
raise ValueError(
"Google API key required. Provide via api_key parameter or "
"set GEMINI_API_KEY environment variable."
)
self.client = genai.Client(api_key=self.api_key)
logger.info(
f"β BatchLLMClient initialized: {model_name} "
f"(batch_size={batch_size}, polling={polling_interval}s)"
)
def generate(
self,
system_prompt: str,
user_prompt: str,
image_base64: Optional[str] = None,
**kwargs
) -> Dict[str, Any]:
"""
Generate response using batch API.
Note: This method is primarily for compatibility. For batch optimization,
the adapter will call generate_batch() directly with multiple requests.
Args:
system_prompt: System-level instructions
user_prompt: User's input prompt
image_base64: Optional base64 encoded image
**kwargs: Additional generation parameters
Returns:
Dict with 'content' key containing generated text
"""
# Single request - process as a batch of 1
requests = [{
'system_prompt': system_prompt,
'user_prompt': user_prompt,
'image_base64': image_base64
}]
results = self.generate_batch(requests)
return results[0] if results else {"content": "", "error": "No results"}
def generate_batch(
self,
requests: List[Dict[str, Any]],
timeout_override: Optional[int] = None
) -> List[Dict[str, Any]]:
"""
Process multiple requests in a single batch job.
This is the main method called by UniversalGepaAdapter during GEPA optimization.
Args:
requests: List of request dicts with keys:
- system_prompt: System instructions
- user_prompt: User input
- image_base64: Optional base64 image
timeout_override: Override max_polling_time for this batch
Returns:
List of response dicts with 'content' key
Raises:
RuntimeError: If batch job fails
TimeoutError: If polling exceeds timeout
"""
logger.info(f"π¦ Processing batch of {len(requests)} requests via Gemini Batch API...")
start_time = time.time()
try:
# Step 1: Upload images if needed
file_uris, mime_types = self._upload_images_for_batch(requests)
# Step 2: Create JSONL file
jsonl_path = self._create_batch_jsonl(requests, file_uris, mime_types)
# Step 3: Submit batch job
batch_job_name = self._submit_batch_job(jsonl_path)
# Step 4: Wait for completion
timeout = timeout_override or self.max_polling_time
self._wait_for_batch_completion(batch_job_name, timeout)
# Step 5: Retrieve results
results = self._retrieve_batch_results(batch_job_name)
# Cleanup
jsonl_path.unlink(missing_ok=True)
elapsed_time = time.time() - start_time
logger.info(
f"β Batch processing complete: {len(results)} results in {elapsed_time:.1f}s "
f"(~{elapsed_time/len(results):.1f}s per request)"
)
return results
except Exception as e:
elapsed_time = time.time() - start_time
logger.error(f"β Batch processing failed after {elapsed_time:.1f}s: {e}")
raise
def _upload_images_for_batch(self, requests: List[Dict]) -> Tuple[List[Optional[str]], List[Optional[str]]]:
"""
Upload images to Gemini and return file URIs and MIME types.
Args:
requests: List of request dicts
Returns:
Tuple of (file_uris, mime_types) - both are lists with None for requests without images
"""
file_uris = []
mime_types = []
images_to_upload = sum(1 for r in requests if r.get('image_base64'))
if images_to_upload > 0:
logger.info(f" β¬οΈ Uploading {images_to_upload} images to Gemini...")
for i, request in enumerate(requests):
image_base64 = request.get('image_base64')
if not image_base64:
file_uris.append(None)
mime_types.append(None)
continue
try:
# Decode image data
import base64
image_data = base64.b64decode(image_base64)
# Detect image format using Pillow
image_format = None
if PIL_AVAILABLE:
try:
img = Image.open(io.BytesIO(image_data))
image_format = img.format.lower() if img.format else None
except Exception as e:
logger.warning(f" β οΈ Could not detect image format: {e}")
# Map format to extension and MIME type
format_map = {
'jpeg': ('.jpg', 'image/jpeg'),
'jpg': ('.jpg', 'image/jpeg'),
'png': ('.png', 'image/png'),
'gif': ('.gif', 'image/gif'),
'webp': ('.webp', 'image/webp'),
'bmp': ('.bmp', 'image/bmp'),
'tiff': ('.tiff', 'image/tiff'),
'tif': ('.tiff', 'image/tiff'),
}
# Get extension and MIME type (default to PNG if unknown)
ext, mime_type = format_map.get(image_format, ('.png', 'image/png'))
if image_format and image_format not in format_map:
logger.warning(f" β οΈ Unknown image format '{image_format}' for image {i}, defaulting to PNG")
elif not image_format:
logger.debug(f" βΉοΈ Could not detect format for image {i}, using PNG")
# Save to temp file with correct extension
temp_file = tempfile.NamedTemporaryFile(
delete=False,
suffix=ext,
dir=self.temp_dir
)
temp_file.write(image_data)
temp_file.close()
# Upload to Gemini with correct MIME type
uploaded_file = self.client.files.upload(
file=temp_file.name,
config=types.UploadFileConfig(
display_name=f"batch_image_{i}_{int(time.time())}{ext}",
mime_type=mime_type
)
)
logger.debug(f" β Uploaded image {i} as {mime_type}")
# Wait for file to be active
self._wait_for_file_active(uploaded_file)
file_uris.append(uploaded_file.uri)
mime_types.append(mime_type)
# Cleanup temp file
Path(temp_file.name).unlink()
except Exception as e:
logger.error(f" β Failed to upload image {i}: {e}")
file_uris.append(None)
mime_types.append(None)
if images_to_upload > 0:
successful = sum(1 for uri in file_uris if uri is not None)
logger.info(f" β Uploaded {successful}/{images_to_upload} images successfully")
return file_uris, mime_types
def _create_batch_jsonl(
self,
requests: List[Dict],
file_uris: List[Optional[str]],
mime_types: List[Optional[str]]
) -> Path:
"""
Create JSONL file for batch job.
Args:
requests: List of request dicts
file_uris: List of uploaded file URIs
mime_types: List of MIME types for uploaded files
Returns:
Path to created JSONL file
"""
timestamp = int(time.time())
jsonl_path = self.temp_dir / f"batch_{timestamp}.jsonl"
with open(jsonl_path, 'w', encoding='utf-8') as f:
for i, (request, file_uri, mime_type) in enumerate(zip(requests, file_uris, mime_types)):
# Combine system and user prompts
system_prompt = request.get('system_prompt', '')
user_prompt = request.get('user_prompt', '')
full_prompt = f"{system_prompt}\n\n{user_prompt}".strip()
# Build request parts
parts = [{"text": full_prompt}]
if file_uri:
parts.append({
"file_data": {
"file_uri": file_uri,
"mime_type": mime_type or "image/png" # Use actual MIME type
}
})
# Gemini Batch API format according to official docs
# Reference: https://ai.google.dev/gemini-api/docs/batch-inference
# NOTE: The "request" wrapper is REQUIRED for Gemini 2.5 batch API
batch_request = {
"custom_id": f"request-{i}",
"request": {
"contents": [{
"role": "user",
"parts": parts
}]
}
}
f.write(json.dumps(batch_request, ensure_ascii=False) + '\n')
logger.info(f" π Created JSONL file: {jsonl_path.name} ({len(requests)} requests)")
return jsonl_path
def _submit_batch_job(self, jsonl_path: Path) -> str:
"""
Submit batch job to Gemini.
Args:
jsonl_path: Path to JSONL file
Returns:
Batch job name
"""
# Upload JSONL file
# Try multiple methods as the google-genai SDK can be finicky
try:
logger.info(f" π€ Uploading JSONL file: {jsonl_path.name}")
# Read and validate file content
with open(jsonl_path, 'r', encoding='utf-8') as f:
content = f.read()
line_count = len(content.strip().split('\n'))
logger.debug(f" π JSONL: {len(content)} bytes, {line_count} lines")
# Validate JSONL format
for line_num, line in enumerate(content.strip().split('\n'), 1):
try:
json.loads(line)
except json.JSONDecodeError as e:
logger.error(f" β Invalid JSON at line {line_num}: {e}")
logger.error(f" Content: {line[:100]}...")
raise ValueError(f"Invalid JSONL format at line {line_num}") from e
# Method 1: Try uploading with Path object
logger.info(f" π Upload method 1: Using Path object...")
try:
jsonl_file = self.client.files.upload(
file=jsonl_path,
config=types.UploadFileConfig(
display_name=f'gepa-batch-{int(time.time())}',
mime_type='application/json' # Try application/json instead of application/jsonl
)
)
logger.info(f" β JSONL file uploaded: {jsonl_file.name}")
except Exception as e1:
logger.warning(f" β οΈ Method 1 failed: {e1}")
logger.info(f" π Upload method 2: Using string path...")
# Method 2: Fallback to string path
try:
jsonl_file = self.client.files.upload(
file=str(jsonl_path.absolute()),
config=types.UploadFileConfig(
display_name=f'gepa-batch-{int(time.time())}',
mime_type='application/json'
)
)
logger.info(f" β JSONL file uploaded (method 2): {jsonl_file.name}")
except Exception as e2:
logger.error(f" β Method 2 also failed: {e2}")
raise e2
except KeyError as e:
logger.error(f"β KeyError during JSONL upload: {e}")
logger.error(f" This suggests the Gemini API response format changed")
logger.error(f" Try updating google-genai: pip install --upgrade google-genai")
raise RuntimeError(f"Gemini Batch API response format error: {e}") from e
except Exception as e:
logger.error(f"β Failed to upload JSONL file: {e}")
logger.error(f" File path: {jsonl_path}")
logger.error(f" File exists: {jsonl_path.exists()}")
logger.error(f" File size: {jsonl_path.stat().st_size if jsonl_path.exists() else 'N/A'} bytes")
raise RuntimeError(f"Gemini Batch API file upload failed: {e}") from e
# Wait for JSONL to be active
try:
logger.info(f" β³ Waiting for JSONL file to be processed...")
self._wait_for_file_active(jsonl_file)
except Exception as e:
logger.error(f"β JSONL file processing failed: {e}")
raise
# Create batch job
try:
logger.info(f" π Creating batch job...")
batch_job = self.client.batches.create(
model=self.model_name,
src=jsonl_file.name,
config={'display_name': f'gepa-opt-{int(time.time())}'}
)
logger.info(f" β Batch job submitted: {batch_job.name}")
return batch_job.name
except Exception as e:
logger.error(f"β Failed to create batch job: {e}")
raise RuntimeError(f"Batch job creation failed: {e}") from e
def _wait_for_batch_completion(self, job_name: str, timeout: int):
"""
Poll batch job until completion.
Args:
job_name: Batch job name
timeout: Maximum seconds to wait
Raises:
TimeoutError: If polling exceeds timeout
RuntimeError: If batch job fails
"""
logger.info(f" β³ Polling for completion (checking every {self.polling_interval}s)...")
start_time = time.time()
poll_count = 0
while True:
elapsed = time.time() - start_time
if elapsed > timeout:
raise TimeoutError(
f"Batch job timeout after {elapsed:.0f}s "
f"(max: {timeout}s)"
)
try:
batch_job = self.client.batches.get(name=job_name)
state = batch_job.state.name
# Success states
if state in ['JOB_STATE_SUCCEEDED', 'SUCCEEDED']:
logger.info(f" β Batch job completed in {elapsed:.0f}s")
return
# Failure states
if state in ['JOB_STATE_FAILED', 'FAILED']:
raise RuntimeError(f"Batch job failed with state: {state}")
if state in ['JOB_STATE_CANCELLED', 'CANCELLED']:
raise RuntimeError(f"Batch job was cancelled: {state}")
# Still processing
poll_count += 1
if poll_count % 5 == 0: # Log every 5 polls
logger.info(f" ... still processing ({elapsed:.0f}s elapsed, state: {state})")
time.sleep(self.polling_interval)
except (TimeoutError, RuntimeError):
raise
except Exception as e:
logger.warning(f" β οΈ Error checking job status: {e}, retrying...")
time.sleep(5)
def _retrieve_batch_results(self, job_name: str) -> List[Dict[str, Any]]:
"""
Retrieve and parse batch results.
Args:
job_name: Batch job name
Returns:
List of result dicts
"""
batch_job = self.client.batches.get(name=job_name)
# Check for inline responses (preferred)
if hasattr(batch_job.dest, 'inlined_responses') and batch_job.dest.inlined_responses:
logger.info(f" π₯ Processing inline responses...")
return self._parse_inline_results(batch_job.dest.inlined_responses)
# Download results file (fallback)
if hasattr(batch_job.dest, 'file_name') and batch_job.dest.file_name:
logger.info(f" π₯ Downloading results file: {batch_job.dest.file_name}")
file_data = self.client.files.download(file=batch_job.dest.file_name)
return self._parse_file_results(file_data)
raise RuntimeError("No results available from batch job")
def _parse_inline_results(self, inline_responses) -> List[Dict[str, Any]]:
"""Parse inline batch results."""
results = []
for response_obj in inline_responses:
if hasattr(response_obj, 'response') and response_obj.response:
text = self._extract_text_from_response(response_obj.response)
results.append({
"content": text,
"role": "assistant",
"model": self.model_name,
"provider": "google"
})
else:
error_msg = str(getattr(response_obj, 'error', 'Unknown error'))
logger.warning(f" β οΈ Response error: {error_msg}")
results.append({
"content": "",
"error": error_msg
})
return results
def _parse_file_results(self, file_data) -> List[Dict[str, Any]]:
"""Parse JSONL results file."""
if isinstance(file_data, bytes):
jsonl_content = file_data.decode('utf-8')
else:
jsonl_content = file_data
results = []
for line_num, line in enumerate(jsonl_content.strip().split('\n'), 1):
if not line.strip():
continue
try:
result = json.loads(line)
if 'response' in result:
text = self._extract_text_from_dict(result['response'])
results.append({
"content": text,
"role": "assistant",
"model": self.model_name,
"provider": "google"
})
else:
error_msg = result.get('error', 'Unknown error')
logger.warning(f" β οΈ Line {line_num} error: {error_msg}")
results.append({
"content": "",
"error": error_msg
})
except json.JSONDecodeError as e:
logger.error(f" β Line {line_num}: JSON decode error: {e}")
results.append({"content": "", "error": f"JSON decode error: {e}"})
return results
def _extract_text_from_response(self, response_obj) -> str:
"""Extract text from response object."""
try:
# Direct text attribute
if hasattr(response_obj, 'text'):
return response_obj.text
# Navigate through candidates
if hasattr(response_obj, 'candidates') and response_obj.candidates:
candidate = response_obj.candidates[0]
if hasattr(candidate, 'content'):
content = candidate.content
if hasattr(content, 'parts') and content.parts:
part = content.parts[0]
if hasattr(part, 'text'):
return part.text
# Fallback to string representation
return str(response_obj)
except Exception as e:
logger.error(f"Error extracting text from response: {e}")
return ""
def _extract_text_from_dict(self, response_dict: Dict) -> str:
"""Extract text from response dictionary."""
try:
# Direct text key
if 'text' in response_dict:
return response_dict['text']
# Navigate through candidates
if 'candidates' in response_dict and response_dict['candidates']:
candidate = response_dict['candidates'][0]
if 'content' in candidate and 'parts' in candidate['content']:
parts = candidate['content']['parts']
if parts and 'text' in parts[0]:
return parts[0]['text']
# Fallback to JSON string
return json.dumps(response_dict)
except Exception as e:
logger.error(f"Error extracting text from dict: {e}")
return ""
def _wait_for_file_active(self, uploaded_file, timeout: int = 60):
"""
Wait for uploaded file to become active.
Args:
uploaded_file: Uploaded file object
timeout: Maximum seconds to wait
Raises:
TimeoutError: If file processing exceeds timeout
RuntimeError: If file processing fails
"""
start_time = time.time()
while uploaded_file.state.name == "PROCESSING":
if time.time() - start_time > timeout:
raise TimeoutError(f"File processing timeout: {uploaded_file.name}")
time.sleep(1)
uploaded_file = self.client.files.get(name=uploaded_file.name)
if uploaded_file.state.name != "ACTIVE":
raise RuntimeError(
f"File processing failed: {uploaded_file.name} "
f"(state: {uploaded_file.state.name})"
)
def get_model_info(self) -> Dict[str, str]:
"""Get model information for logging and debugging."""
return {
'provider': self.provider,
'model_name': self.model_name,
'class': self.__class__.__name__,
'mode': 'batch',
'batch_size': str(self.batch_size),
'polling_interval': f'{self.polling_interval}s'
}
|