Download source/workbench/surface.py from ajinkyamulay/analyst-workbench: direct link, hf CLI and curl.
- Browser
- Download file 5.41 kB
-
https://huggingface.co/datasets/ajinkyamulay/analyst-workbench/resolve/main/source/workbench/surface.py
- Command line
-
hf download hf://datasets/ajinkyamulay/analyst-workbench/source/workbench/surface.py
-
curl -L -o surface.py https://huggingface.co/datasets/ajinkyamulay/analyst-workbench/resolve/main/source/workbench/surface.py
5.41 kB
| """v8: a different tool surface in every episode. | |
| Each private arena task has its own action schema, so v8 trains the habit of reading the tools | |
| offered and replying with exactly one call that fits them. Every episode draws its own tool | |
| names, argument names and answer shape from the episode seed; the underlying operations (list, | |
| read, search, run Python, submit) and the task content are unchanged. A call that does not fit | |
| (unknown tool, missing or unknown argument, wrong type) ends the episode with score 0, as a | |
| schema-invalid reply does in the arena. | |
| """ | |
| import json | |
| import random | |
| NAMES = { | |
| "ls": ["ls", "list_files", "files", "dir", "list_workspace"], | |
| "read": ["read", "read_file", "view", "open_file", "show_lines"], | |
| "grep": ["grep", "search", "find_text", "search_files"], | |
| "python": ["python", "run_python", "exec_python", "run_code", "py"], | |
| "submit": ["submit", "answer", "final_answer", "submit_answer", "finish_task"], | |
| } | |
| ARGS = { | |
| ("read", "path"): ["path", "file", "filename", "file_path"], | |
| ("read", "start"): ["start", "from_line", "offset", "first_line"], | |
| ("read", "lines"): ["lines", "count", "n", "num_lines"], | |
| ("grep", "pattern"): ["pattern", "regex", "query", "expression"], | |
| ("grep", "path"): ["path", "file", "in_file", "target"], | |
| ("python", "code"): ["code", "source", "script", "program"], | |
| ("submit", "answer"): ["answer", "result", "value", "final", "response"], | |
| } | |
| DOC = { | |
| "ls": "list the workspace files", | |
| "read": "show numbered lines of a file (at most 80 per call)", | |
| "grep": "search a regular expression in one file, or in all files", | |
| "python": "run Python in the workspace folder; print what you need (10 s limit)", | |
| "submit": "submit your final answer; ends the episode", | |
| } | |
| ARG_DOC = { | |
| ("read", "path"): "file to show", | |
| ("read", "start"): "first line, 1-based", | |
| ("read", "lines"): "number of lines", | |
| ("grep", "pattern"): "regular expression", | |
| ("grep", "path"): "file to search; omit to search all files", | |
| ("python", "code"): "Python source", | |
| ("submit", "answer"): "the answer, as the task asks", | |
| } | |
| OPTIONAL = {("read", "start"): 1, ("read", "lines"): 40, ("grep", "path"): ""} | |
| INTS = {("read", "start"), ("read", "lines")} | |
| FINISH = "finish" # reserved: the arena's finish_action, always accepted, scores an empty answer | |
| class CallError(ValueError): | |
| """A call that does not fit this episode's tools: ends the episode with score 0.""" | |
| class Surface: | |
| def __init__(self, seed): | |
| rng = random.Random(f"surface:{seed}") | |
| self.tool = {op: rng.choice(names) for op, names in NAMES.items()} | |
| self.arg = {} | |
| for op in NAMES: # argument names must differ within one tool | |
| used = set() | |
| for (o, canon), names in ARGS.items(): | |
| if o != op: | |
| continue | |
| name = rng.choice([n for n in names if n not in used]) | |
| used.add(name) | |
| self.arg[(o, canon)] = name | |
| self.order = list(NAMES) | |
| rng.shuffle(self.order) | |
| self.op = {name: op for op, name in self.tool.items()} | |
| def help(self): | |
| rows = [] | |
| for op in self.order: | |
| params = [] | |
| for (o, canon), name in self.arg.items(): | |
| if o != op: | |
| continue | |
| kind = "int" if (o, canon) in INTS else "string" | |
| opt = f" = {json.dumps(OPTIONAL[(o, canon)])}" if (o, canon) in OPTIONAL else "" | |
| params.append(f"{name}: {kind}{opt} ({ARG_DOC[(o, canon)]})") | |
| sig = ", ".join(params) | |
| rows.append(f"- {self.tool[op]}({sig}): {DOC[op]}") | |
| example = self.example() | |
| return ("Tools. Call one per reply as {\"tool\": NAME, \"args\": {...}}, for example " | |
| f"{example}:\n" + "\n".join(rows)) | |
| def example(self): | |
| name = self.arg[("read", "path")] | |
| return json.dumps({"tool": self.tool["read"], "args": {name: "notes.txt"}}) | |
| def translate(self, tool, args): | |
| """Return (canonical op, canonical kwargs) or raise CallError.""" | |
| if tool == FINISH: | |
| return "submit", {"answer": ""} | |
| op = self.op.get(tool) | |
| if op is None: | |
| raise CallError(f"Unknown tool {tool!r}.") | |
| if not isinstance(args, dict): | |
| raise CallError("args must be an object.") | |
| wanted = {name: canon for (o, canon), name in self.arg.items() if o == op} | |
| unknown = sorted(set(args) - set(wanted)) | |
| if unknown: | |
| raise CallError(f"{tool} has no argument {unknown[0]!r}.") | |
| out = {} | |
| for name, canon in wanted.items(): | |
| if name not in args: | |
| if (op, canon) in OPTIONAL: | |
| out[canon] = OPTIONAL[(op, canon)] | |
| continue | |
| raise CallError(f"{tool} needs {name!r}.") | |
| value = args[name] | |
| if (op, canon) in INTS: | |
| if isinstance(value, bool) or not isinstance(value, (int, str)) or \ | |
| (isinstance(value, str) and not value.strip().lstrip("-").isdigit()): | |
| raise CallError(f"{name} must be an integer.") | |
| value = int(value) | |
| elif not isinstance(value, str): | |
| raise CallError(f"{name} must be a string.") | |
| out[canon] = value | |
| return op, out | |