Back to .md Directory

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.

May 2, 2026
0 downloads
1 views
ai prompt eval openai gemini
View source

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

PythonpandasClickGoogle AIOpenAITikToken

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:

  • TokenCounter interface separates token counting from model validation logic
  • ModelProvider abstraction 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 logic
  • providers/base_provider.py - Abstract interface for AI providers
  • config/settings.py - Environment-based configuration management
  • core/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 SizeComponentBeforeAfterImprovement
Large (10K rows)TikToken55.3s49.8s1.1x faster
Large (10K rows)Approximate1.6s0.2s7.3x faster
Large (10K rows)Batch Split2.0s0.4s5.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 injection
  • b314992 - Add custom exceptions for better error handling
  • 327dbb8 - Add proper error handling with custom exceptions
  • 0e6c605 - Improve CLI error handling and user feedback
  • 9c9f25b - Add type hints and error handling to GoogleProvider
  • 7a966ce - 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-learning with your own repository name
  • Replace PromptLearningOptimizer class name with your own optimizer class
  • Replace PromptLearningError exception 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 TokenCounter interface 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