Architecture Refactoring & Performance Optimization
Refactors a prompt-learning codebase with dependency injection, vectorized token counting, and Google AI integration, achieving up to 67x faster performance.
What this file does
Refactors a prompt-learning codebase with dependency injection, vectorized token counting, and Google AI integration, achieving up to 67x faster performance.
When to use it
- You have a tightly coupled Python codebase that needs cleaner separation of concerns
- You need to add new AI providers without rewriting existing logic
- You process large datasets and token counting is a bottleneck
- You want to replace requirements.txt with pyproject.toml for modern packaging
Assumes this stack
Architecture Refactoring & Performance Optimization
Summary
Refactors the prompt-learning codebase with cleaner architecture, dependency injection, and performance optimizations. Token counting is now 67x faster for large datasets, with added Google AI integration and image generation support.
Architecture Changes
The codebase had coupling issues that made it hard to extend. Fixed with proper separation of concerns:
Interface Abstractions:
TokenCounterinterface separates token counting from model validation logicModelProviderabstraction makes adding new AI providers straightforward- Configuration management centralized instead of scattered throughout files
- Custom exception hierarchy instead of generic ValueError everywhere
New Files:
interfaces/token_counter.py- Token counting without model-specific logicproviders/base_provider.py- Abstract interface for AI providersconfig/settings.py- Environment-based configuration managementcore/exceptions.py- Structured error handling
Features Added
Google AI Integration: Full Google AI support with dependency injection. Includes Gemini image generation and search grounding capabilities. For image generation, added human-in-the-loop evaluation since image quality assessment is subjective.
CLI Tool: Click-based CLI with commands for optimization, evaluation, testing, and image generation. Error handling gives useful feedback, and supports both OpenAI and Google providers.
Performance Optimizations
Found and fixed several bottlenecks:
Token Counting Vectorization (interfaces/token_counter.py)
The original code used df.iterrows() which processes rows one by one. Replaced with pandas vectorized operations using .apply() on entire columns.
# Before: Row-by-row iteration
for _, row in df.iterrows():
total_tokens += self.count_tokens(row[col])
# After: Vectorized operations
col_tokens = df[col].fillna('').astype(str).apply(self.count_tokens)
token_counts += col_tokens
Batch Splitting Optimization (core/dataset_splitter.py)
Changed from creating DataFrame copies using index lists to pre-calculating boundaries and using efficient slicing.
Regex Compilation (optimizer_sdk/prompt_learning_optimizer.py)
Moved regex compilation to module level instead of recompiling on every method call.
Performance Results
| Dataset Size | Component | Before | After | Improvement |
|---|---|---|---|---|
| Large (10K rows) | TikToken | 55.3s | 49.8s | 1.1x faster |
| Large (10K rows) | Approximate | 1.6s | 0.2s | 7.3x faster |
| Large (10K rows) | Batch Split | 2.0s | 0.4s | 5.7x faster |
Technical Implementation
Dependency Injection Pattern:
# Clean dependency injection
class PromptLearningOptimizer:
def __init__(self, prompt, model_choice, provider=None, token_counter=None):
self.provider = provider
self.token_counter = token_counter or self._get_default_counter()
Error Handling:
class PromptLearningError(Exception): pass
class DatasetError(PromptLearningError): pass
class ProviderError(PromptLearningError): pass
Testing Infrastructure
Added comprehensive benchmarking framework in tests/benchmarks/ with performance tracking and analysis. The benchmark runner tests different dataset sizes and provides detailed timing and memory usage reports.
Package Management
Replaced requirements.txt with pyproject.toml and added CLI entry point for easy installation and usage.
Commits
1b033fe- Refactor optimizer to use dependency injectionb314992- Add custom exceptions for better error handling327dbb8- Add proper error handling with custom exceptions0e6c605- Improve CLI error handling and user feedback9c9f25b- Add type hints and error handling to GoogleProvider7a966ce- Optimize SDK performance: 7x faster token counting, 6x faster batch splitting
The refactoring makes the codebase more maintainable and significantly faster for large dataset processing.
What's inside
7 sections covering architecture, features, optimizations, results, implementation, testing, and commits with code examples
Change this for your project
- Replace
Arize-ai/prompt-learningwith your own repository name - Replace
PromptLearningOptimizerclass name with your own optimizer class - Replace
PromptLearningErrorexception name with your own custom exception - Replace
tests/benchmarks/path with your own benchmark directory
Where it goes
Save in docs/ or the repository root. Gives agents and new contributors a map of the codebase.
Worth borrowing
- Extract a
TokenCounterinterface to decouple token counting from model validation - Use dependency injection in constructors to make providers and counters swappable
- Replace row-by-row DataFrame iteration with vectorized
.apply()calls for speed
Related Documents
PCB Design Sources for Industry-Scale Testing
Lists GitHub repos and communities where you can download real PCB designs for testing optimization at scale.
Design Document: BharatSeva AI
Describes a 10-agent AWS system that helps India's informal workers access government schemes via voice-first, serverless architecture.
OpenClaw Enterprise Transformation Plan
Transforms a single-user AI agent into a dual-mode platform supporting both viral open-source and Fortune 500 enterprise deployments through phased security, IAM, audit, multi-tenancy, and Kubernetes features.
Qwen Image and Edit: Open-sourcing and Local GGUF Generations with Lightning
Documents the Qwen-Image and Qwen-Image-Edit models, covering architecture, training, benchmarks, ComfyUI setup, and prompting techniques for local GGUF deployment.