-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun_eval.py
More file actions
238 lines (190 loc) · 7.02 KB
/
run_eval.py
File metadata and controls
238 lines (190 loc) · 7.02 KB
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
#!/usr/bin/env python3
"""
Run evaluation on LinkedIn browser automation tasks.
This script runs Claude Code on LinkedIn tasks using Kernel cloud browsers
and evaluates the results. Use this to test the skill before running GEPA.
Usage:
# Run a single task
uv run python run_eval.py --task own_follower_count
# Run all tasks
uv run python run_eval.py --all
# Run specific seeds
uv run python run_eval.py --seeds 0 1 2
"""
import argparse
import asyncio
import os
import sys
from pathlib import Path
# Force unbuffered output for real-time streaming visibility
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(line_buffering=True)
if hasattr(sys.stderr, "reconfigure"):
sys.stderr.reconfigure(line_buffering=True)
# Add src to path for imports
sys.path.insert(0, str(Path(__file__).parent / "src"))
from dotenv import load_dotenv
load_dotenv()
from linkedin_bench.kernel_runner import (
DEFAULT_POOL_NAME,
get_or_create_pool,
run_task_in_kernel,
)
from linkedin_bench.skill_template import get_skill_content
from linkedin_bench.tasks import TASKS, get_task_by_id, get_task_by_seed
from linkedin_bench.verifier import (
calculate_reward,
count_agent_steps,
count_sleep_commands,
verify_with_llm,
)
async def run_single_task(task_id: str, timeout: int | None = None, skill_content: str | None = None):
"""Run a single task and print results."""
task = get_task_by_id(task_id)
skill = skill_content or get_skill_content()
# Use task timeout if not explicitly provided
effective_timeout = timeout if timeout is not None else task.timeout
print(f"\n{'=' * 60}")
print(f"Task: {task.id}")
print(f"Prompt: {task.prompt}")
print(f"Expected: {task.expected}")
print(f"Timeout: {effective_timeout}s")
print(f"{'=' * 60}\n")
result = await run_task_in_kernel(
task_prompt=task.prompt,
skill_content=skill,
timeout=effective_timeout,
)
print(f"\n{'=' * 60}")
print(f"Result: exit_code={result.exit_code}, elapsed={result.elapsed_seconds:.1f}s")
print(f"{'=' * 60}")
# Show last part of output
print("\nAgent output (last 2000 chars):")
print("-" * 40)
print(result.output[-2000:])
print("-" * 40)
# Verify
print("\nVerifying with LLM judge...")
anthropic_key = os.environ.get("ANTHROPIC_API_KEY")
verification = await verify_with_llm(task, result.output, anthropic_key)
num_steps = count_agent_steps(result.output)
num_sleeps, total_sleep_ms = count_sleep_commands(result.output)
reward = calculate_reward(
correctness_score=verification.raw_score,
elapsed_seconds=result.elapsed_seconds,
num_agent_steps=num_steps,
num_sleep_commands=num_sleeps,
total_sleep_ms=total_sleep_ms,
max_time=float(effective_timeout),
)
print(f"\nVerification:")
print(f" Correct: {verification.correct}")
print(f" Extracted: {verification.extracted_answer}")
print(f" Reason: {verification.reason}")
if num_sleeps > 0:
print(f" Sleep commands: {num_sleeps} ({total_sleep_ms}ms total) - PENALIZED")
print(f" Steps: {num_steps}")
print(f" Reward: {reward:.3f}")
return {
"task_id": task.id,
"correct": verification.correct,
"reward": reward,
"elapsed": result.elapsed_seconds,
"steps": num_steps,
}
async def run_all_tasks(timeout: int = 120):
"""Run all tasks and summarize results."""
results = []
for task in TASKS:
try:
result = await run_single_task(task.id, timeout)
results.append(result)
except Exception as e:
print(f"Task {task.id} failed: {e}")
results.append({
"task_id": task.id,
"correct": False,
"reward": 0.0,
"elapsed": 0,
"steps": 0,
"error": str(e),
})
# Summary
print(f"\n{'=' * 60}")
print("SUMMARY")
print(f"{'=' * 60}")
total_reward = 0
correct_count = 0
for r in results:
status = "✓" if r.get("correct") else "✗"
print(f" {status} {r['task_id']}: reward={r['reward']:.3f}, elapsed={r['elapsed']:.1f}s")
total_reward += r["reward"]
if r.get("correct"):
correct_count += 1
print(f"\nTotal: {correct_count}/{len(results)} correct")
print(f"Mean reward: {total_reward / len(results):.3f}")
return results
async def run_seeds(seeds: list[int], timeout: int = 120):
"""Run specific seeds."""
results = []
for seed in seeds:
task = get_task_by_seed(seed)
try:
result = await run_single_task(task.id, timeout)
results.append(result)
except Exception as e:
print(f"Seed {seed} (task {task.id}) failed: {e}")
results.append({
"task_id": task.id,
"correct": False,
"reward": 0.0,
"error": str(e),
})
return results
async def ensure_pool_exists():
"""Make sure the browser pool exists before running (optional - falls back to direct browsers)."""
from kernel import AsyncKernel
api_key = os.environ.get("KERNEL_API_KEY")
if not api_key:
print("ERROR: KERNEL_API_KEY not set")
sys.exit(1)
client = AsyncKernel(api_key=api_key)
print(f"Checking browser pool '{DEFAULT_POOL_NAME}'...")
try:
await get_or_create_pool(client, DEFAULT_POOL_NAME, "linkedin", 10)
print("Pool ready.")
except Exception as e:
print(f"Pool not available ({e}), will use direct browser creation instead.")
def main():
parser = argparse.ArgumentParser(description="Run LinkedIn browser automation evaluation")
parser.add_argument("--task", type=str, help="Run a specific task by ID")
parser.add_argument("--all", action="store_true", help="Run all tasks")
parser.add_argument("--seeds", type=int, nargs="+", help="Run specific seeds")
parser.add_argument("--timeout", type=int, default=120, help="Timeout per task (default: 120)")
parser.add_argument("--list", action="store_true", help="List available tasks")
args = parser.parse_args()
if args.list:
print("Available tasks:")
for task in TASKS:
print(f" {task.id}: {task.prompt}")
return
# Check environment
required_vars = ["KERNEL_API_KEY", "ANTHROPIC_API_KEY"]
missing = [v for v in required_vars if not os.environ.get(v)]
if missing:
print(f"ERROR: Missing environment variables: {', '.join(missing)}")
print("Make sure .env file exists or export the variables.")
sys.exit(1)
async def run():
await ensure_pool_exists()
if args.task:
await run_single_task(args.task, args.timeout)
elif args.all:
await run_all_tasks(args.timeout)
elif args.seeds:
await run_seeds(args.seeds, args.timeout)
else:
parser.print_help()
asyncio.run(run())
if __name__ == "__main__":
main()