Yes, there are several ways to add a progress bar to your async function. Here are a few popular options:Option 1: Using tqdm with asyncio.as_completed()pythonimport asyncio
from tqdm.asyncio import tqdm
from typing import List, Dict, Any
import os
import json
from datetime import datetime
import aiofiles
async def run_batch_pairwise_evaluation(
judge_client: AsyncOpenAI,
evaluation_data: List[Dict[str, Any]],
output_dir: str = "outputs",
max_concurrent: int = 3
) -> List[Dict[str, Any]]:
"""
Run batch pairwise evaluations with progress bar
"""
semaphore = asyncio.Semaphore(max_concurrent)
async def evaluate_with_semaphore(eval_item):
async with semaphore:
return await run_pairwise_evaluation(
judge_client=judge_client,
instruction=eval_item['instruction'],
response_1=eval_item['response_1'],
response_2=eval_item['response_2'],
model_1_name=eval_item.get('model_1_name', 'Model1'),
model_2_name=eval_item.get('model_2_name', 'Model2'),
uid=eval_item.get('uid'),
output_dir=output_dir
)
tasks = [evaluate_with_semaphore(item) for item in evaluation_data]
# Use tqdm with as_completed for real-time progress updates
results = [None] * len(tasks)
with tqdm(total=len(tasks), desc="Evaluating pairs") as pbar:
for i, task in enumerate(asyncio.as_completed(tasks)):
result = await task
results[i] = result
pbar.update(1)
# Save summary results
summary_file = os.path.join(output_dir, f"batch_summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json")
async with aiofiles.open(summary_file, 'w', encoding='utf-8') as f:
await f.write(json.dumps(results, indent=2, ensure_ascii=False))
return resultsOption 2: Using tqdm.gather() (simpler but less real-time)pythonasync def run_batch_pairwise_evaluation(
judge_client: AsyncOpenAI,
evaluation_data: List[Dict[str, Any]],
output_dir: str = "outputs",
max_concurrent: int = 3
) -> List[Dict[str, Any]]:
"""
Run batch pairwise evaluations with progress bar
"""
semaphore = asyncio.Semaphore(max_concurrent)
async def evaluate_with_semaphore(eval_item):
async with semaphore:
return await run_pairwise_evaluation(
judge_client=judge_client,
instruction=eval_item['instruction'],
response_1=eval_item['response_1'],
response_2=eval_item['response_2'],
model_1_name=eval_item.get('model_1_name', 'Model1'),
model_2_name=eval_item.get('model_2_name', 'Model2'),
uid=eval_item.get('uid'),
output_dir=output_dir
)
tasks = [evaluate_with_semaphore(item) for item in evaluation_data]
# Use tqdm.gather for progress tracking
results = await tqdm.gather(*tasks, desc="Evaluating pairs")
# Save summary results
summary_file = os.path.join(output_dir, f"batch_summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json")
async with aiofiles.open(summary_file, 'w', encoding='utf-8') as f:
await f.write(json.dumps(results, indent=2, ensure_ascii=False))
return resultsOption 3: Using rich for a fancier progress barpythonfrom rich.progress import Progress, TaskID
import asyncio
async def run_batch_pairwise_evaluation(
judge_client: AsyncOpenAI,
evaluation_data: List[Dict[str, Any]],
output_dir: str = "outputs",
max_concurrent: int = 3
) -> List[Dict[str, Any]]:
"""
Run batch pairwise evaluations with rich progress bar
"""
semaphore = asyncio.Semaphore(max_concurrent)
async def evaluate_with_semaphore(eval_item, progress: Progress, task_id: TaskID):
async with semaphore:
result = await run_pairwise_evaluation(
judge_client=judge_client,
instruction=eval_item['instruction'],
response_1=eval_item['response_1'],
response_2=eval_item['response_2'],
model_1_name=eval_item.get('model_1_name', 'Model1'),
model_2_name=eval_item.get('model_2_name', 'Model2'),
uid=eval_item.get('uid'),
output_dir=output_dir
)
progress.advance(task_id)
return result
with Progress() as progress:
task_id = progress.add_task("Evaluating pairs...", total=len(evaluation_data))
tasks = [
evaluate_with_semaphore(item, progress, task_id)
for item in evaluation_data
]
results = await asyncio.gather(*tasks, return_exceptions=True)
# Save summary results
summary_file = os.path.join(output_dir, f"batch_summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json")
async with aiofiles.open(summary_file, 'w', encoding='utf-8') as f:
await f.write(json.dumps(results, indent=2, ensure_ascii=False))
return resultsInstallation RequirementsFor the tqdm options:
bashpip install tqdmFor the rich option:
bashpip install richRecommendationI'd recommend Option 1 with tqdm and asyncio.as_completed() because:
It provides real-time updates as tasks complete
It's lightweight and widely used
It handles the async nature