-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.py
More file actions
226 lines (181 loc) · 7.32 KB
/
Copy pathenv.py
File metadata and controls
226 lines (181 loc) · 7.32 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
"""Data Science Environment — Titanic dataset analysis tasks.
Provides:
- Agent tools: bash, editor, shell, apply_patch
- Scenarios representing reusable evaluation patterns:
analyze_dataset — single-output analysis with binary grading
multi_output_analysis — multiple outputs with weighted SubScores
Usage:
hud dev env:env --stdio # Run as MCP server
from env import analyze_dataset # Import scenarios
"""
import json
import logging
import os
import shutil
import subprocess
import sys
from pathlib import Path
from typing import Any
from hud import Environment
from hud.tools.coding import ApplyPatchTool, BashTool, EditTool, ShellTool
from hud.tools.types import EvaluationResult, SubScore
logging.basicConfig(
stream=sys.stderr,
level=logging.INFO,
format="[%(levelname)s] %(asctime)s | %(name)s | %(message)s",
force=True,
)
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
WORKSPACE = "/home/ubuntu/workspace"
TEMPLATES_DIR = "/problem_templates"
_REPO_ROOT = Path(__file__).parent
# Fallback for local development (outside Docker)
if not Path(TEMPLATES_DIR).exists():
TEMPLATES_DIR = str(_REPO_ROOT / "problem_templates")
# ---------------------------------------------------------------------------
# Environment
# ---------------------------------------------------------------------------
env = Environment(name="datascience")
# ---------------------------------------------------------------------------
# Agent-Visible Tools (registered directly from the SDK)
# ---------------------------------------------------------------------------
env.add_tool(BashTool())
env.add_tool(ShellTool())
env.add_tool(EditTool())
env.add_tool(ApplyPatchTool(base_path=WORKSPACE))
@env.tool()
async def hud_validate() -> str:
"""Run the test suite to validate the environment is working correctly."""
result = subprocess.run(
[sys.executable, "-m", "pytest", "tests/", "-v", "--tb=short"],
capture_output=True,
text=True,
cwd=str(_REPO_ROOT),
)
output = result.stdout + result.stderr
if result.returncode != 0:
raise RuntimeError(output or f"pytest exited with code {result.returncode}")
return output
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _setup_workspace(template: str) -> None:
"""Copy a problem template into the workspace directory."""
template_path = Path(TEMPLATES_DIR) / template
os.makedirs(WORKSPACE, exist_ok=True)
# Clear existing workspace contents
for item in Path(WORKSPACE).iterdir():
if item.name == ".git":
continue
if item.is_dir():
shutil.rmtree(item)
else:
item.unlink()
# Copy template files into workspace
for item in template_path.iterdir():
dst = Path(WORKSPACE) / item.name
if item.is_dir():
shutil.copytree(item, dst)
else:
shutil.copy2(item, dst)
# Ensure ubuntu owns workspace (if running as root)
if os.getuid() == 0:
subprocess.run(["chown", "-R", "ubuntu:ubuntu", WORKSPACE], check=False)
logger.info("Workspace set up with template '%s'", template)
def _make_prompt(description: str) -> str:
"""Format a task description into a full agent prompt."""
return f"""You will be working on a data science task.
The workspace has been set up at {WORKSPACE} with the necessary data files.
Python3 is available. You can use standard library modules (csv, json, etc.).
If you need additional packages, you can install them with pip.
Use the tools provided to complete the following task:
{description}"""
def _outputs_match(output_file: str, actual: str, expected: str) -> bool:
"""Compare actual vs expected. For .json files, compare parsed structures
so pretty-printing and key order don't break grading."""
if output_file.endswith(".json"):
try:
return json.loads(actual) == json.loads(expected)
except json.JSONDecodeError:
return False
return actual == expected
def _check_outputs(required_outputs: dict[str, str]) -> dict[str, dict]:
"""Check each required output file against its expected value.
Returns {filename: {passed, expected, actual}}.
"""
results = {}
for output_file, expected in required_outputs.items():
file_path = Path(WORKSPACE) / output_file
entry: dict[str, Any] = {"passed": False, "expected": expected.strip()}
if not file_path.exists():
entry["actual"] = None
else:
actual = file_path.read_text().strip()
entry["actual"] = actual
entry["passed"] = _outputs_match(output_file, actual, expected.strip())
results[output_file] = entry
return results
# ---------------------------------------------------------------------------
# Scenarios
# ---------------------------------------------------------------------------
@env.scenario("analyze_dataset", exclude_tools=["hud_validate"])
async def analyze_dataset(
prompt: str,
template: str,
required_outputs: dict[str, str],
):
"""Analyze a dataset and produce one or more output files.
Binary grading: 1.0 if every required output matches, 0.0 otherwise.
Good for straightforward analysis tasks with a single correct answer.
"""
_setup_workspace(template)
yield _make_prompt(prompt)
results = _check_outputs(required_outputs)
all_passed = all(r["passed"] for r in results.values())
yield 1.0 if all_passed else 0.0
@env.scenario("multi_output_analysis", exclude_tools=["hud_validate"])
async def multi_output_analysis(
prompt: str,
template: str,
required_outputs: dict[str, str],
output_weights: dict[str, float] | None = None,
):
"""Analyze a dataset and produce multiple output files with weighted scoring.
Each output is scored independently (1.0 correct, 0.0 wrong).
The final reward is the weighted sum across outputs, reported as SubScores.
If output_weights is omitted, all outputs are weighted equally.
"""
_setup_workspace(template)
yield _make_prompt(prompt)
results = _check_outputs(required_outputs)
outputs = list(required_outputs.keys())
# Build normalised weights
weights = dict(output_weights or {})
if not weights:
w = 1.0 / len(outputs)
weights = {name: w for name in outputs}
total_w = sum(weights.values())
weights = {k: v / total_w for k, v in weights.items()}
subscores = [
SubScore(
name=name,
weight=weights.get(name, 1.0 / len(outputs)),
value=1.0 if results[name]["passed"] else 0.0,
)
for name in outputs
]
reward = sum(s.weight * s.value for s in subscores)
yield EvaluationResult(
reward=reward,
done=True,
content=f"{sum(1 for s in subscores if s.value > 0)}/{len(subscores)} outputs correct",
subscores=subscores,
)
# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------
if __name__ == "__main__":
env.run(transport="stdio")