| |
| """ |
| Run OpenHands agent experiments with concurrent execution. |
| """ |
| import os |
| import sys |
| import json |
| import argparse |
| import subprocess |
| from typing import List, Dict, Any |
| from concurrent.futures import ProcessPoolExecutor, as_completed |
| from tqdm import tqdm |
|
|
| |
| DATASET_PATH = "/eval/dataset.jsonl" |
| RESULTS_DIR = "/experiments/openhands/results" |
|
|
| |
| DEFAULT_API_BASE = "http://localhost:60101/v1" |
| DEFAULT_MODEL = "qwen-3.5-27b" |
|
|
|
|
| def load_dataset(path: str = DATASET_PATH) -> List[Dict[str, Any]]: |
| """Load questions from dataset.jsonl.""" |
| questions = [] |
| with open(path, 'r') as f: |
| for line in f: |
| line = line.strip() |
| if line: |
| questions.append(json.loads(line)) |
| return questions |
|
|
|
|
| def run_single_task(args: tuple) -> Dict[str, Any]: |
| """ |
| Run a single OpenHands agent task. |
| |
| This function calls run_agent.py as a subprocess to avoid |
| issues with forking and OpenHands internal state. |
| |
| Args: |
| args: Tuple of (question, try_id, llm_name, api_base, model, enable_thinking) |
| |
| Returns: |
| Dict with result information |
| """ |
| question, try_id, llm_name, api_base, model, enable_thinking = args |
|
|
| |
| cmd = [ |
| '/opt/py312/bin/python3', '/experiments/openhands/exp/run_agent.py', |
| '--prog', question['prog'], |
| '--src-site-id', str(question['src_site_id']), |
| '--to-take', str(question['to_take']), |
| '--format', question.get('format', 'unknown'), |
| '--try-id', str(try_id), |
| '--api-base', api_base, |
| '--model', model, |
| '--llm-name', llm_name, |
| ] |
|
|
| if enable_thinking: |
| cmd.append('--enable-thinking') |
|
|
| try: |
| result = subprocess.run( |
| cmd, |
| capture_output=True, |
| text=True, |
| timeout=10800 * 3, |
| ) |
|
|
| return { |
| 'success': result.returncode == 0, |
| 'question': question, |
| 'try_id': try_id, |
| 'stdout': result.stdout, |
| 'stderr': result.stderr, |
| } |
|
|
| except subprocess.TimeoutExpired: |
| return { |
| 'success': False, |
| 'question': question, |
| 'try_id': try_id, |
| 'error': 'Task timed out', |
| } |
| except Exception as e: |
| return { |
| 'success': False, |
| 'question': question, |
| 'try_id': try_id, |
| 'error': str(e), |
| } |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description="Run OpenHands agent experiments") |
|
|
| |
| parser.add_argument("--api-base", type=str, default=DEFAULT_API_BASE, |
| help="OpenAI-compatible API base URL") |
| parser.add_argument("--model", type=str, default=DEFAULT_MODEL, |
| help="Model name to use") |
| parser.add_argument("--llm-name", type=str, default=None, |
| help="LLM name for result directory (defaults to model name)") |
| parser.add_argument("--enable-thinking", action="store_true", |
| help="Enable thinking/reasoning mode") |
|
|
| |
| parser.add_argument("--concurrency", type=int, default=1, |
| help="Number of concurrent agents (default: 1, recommend low for OpenHands)") |
|
|
| |
| parser.add_argument("--num-tries", type=int, default=1, |
| help="Number of tries per question") |
| parser.add_argument("--dataset", type=str, default=DATASET_PATH, |
| help="Path to dataset.jsonl") |
| parser.add_argument("--num-questions", type=int, default=None, |
| help="Number of questions to test (default: all)") |
| parser.add_argument("--start-idx", type=int, default=0, |
| help="Start index in dataset") |
|
|
| args = parser.parse_args() |
|
|
| |
| llm_name = args.llm_name if args.llm_name else args.model |
|
|
| |
| print(f"Loading dataset from {args.dataset}...") |
| questions = load_dataset(args.dataset) |
| print(f"Loaded {len(questions)} questions") |
|
|
| |
| if args.start_idx > 0: |
| questions = questions[args.start_idx:] |
| print(f"Starting from index {args.start_idx}") |
|
|
| if args.num_questions: |
| questions = questions[:args.num_questions] |
| print(f"Testing {args.num_questions} questions") |
|
|
| |
| tasks = [] |
| for question in questions: |
| for try_id in range(args.num_tries): |
| tasks.append(( |
| question, |
| try_id, |
| llm_name, |
| args.api_base, |
| args.model, |
| args.enable_thinking, |
| )) |
|
|
| total_tasks = len(tasks) |
| print(f"\nTotal tasks: {total_tasks}") |
| print(f"Questions: {len(questions)}, Tries per question: {args.num_tries}") |
| print(f"Concurrency: {args.concurrency}") |
| print(f"Model: {args.model}") |
| print(f"Thinking mode: {args.enable_thinking}") |
|
|
| |
| os.makedirs(RESULTS_DIR, exist_ok=True) |
|
|
| |
| completed = 0 |
| successes = 0 |
|
|
| with ProcessPoolExecutor(max_workers=args.concurrency) as executor: |
| futures = {executor.submit(run_single_task, task): task for task in tasks} |
|
|
| with tqdm(total=total_tasks, desc="Processing") as pbar: |
| for future in as_completed(futures): |
| try: |
| result = future.result() |
| if result.get('success'): |
| successes += 1 |
| except Exception as e: |
| print(f"\nTask failed with exception: {e}") |
|
|
| completed += 1 |
| pbar.update(1) |
| pbar.set_postfix({"success": successes, "remaining": total_tasks - completed}) |
|
|
| print(f"\n\nExperiment completed!") |
| print(f"Total tasks: {total_tasks}") |
| print(f"Successful: {successes}") |
| print(f"Failed: {total_tasks - successes}") |
| print(f"Success rate: {successes / total_tasks * 100:.1f}%") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|