Skip to content

Commit de56002

Browse files
committed
improve how errors are displayed to users
1 parent 6bacec3 commit de56002

File tree

4 files changed

+64
-78
lines changed

4 files changed

+64
-78
lines changed

weco/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22

33
# DO NOT EDIT
4-
__pkg_version__ = "0.2.15"
4+
__pkg_version__ = "0.2.16"
55
__api_version__ = "v1"
66

77
__base_url__ = f"https://api.weco.ai/{__api_version__}"

weco/api.py

Lines changed: 39 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,28 @@ def start_optimization_session(
2828
) -> Dict[str, Any]:
2929
"""Start the optimization session."""
3030
with console.status("[bold green]Starting Optimization..."):
31-
try:
32-
# __base_url__ already contains /v1
33-
response = requests.post(
34-
f"{__base_url__}/sessions", # Path is relative to base_url
35-
json={
36-
"source_code": source_code,
37-
"additional_instructions": additional_instructions,
38-
"objective": {"evaluation_command": evaluation_command, "metric_name": metric_name, "maximize": maximize},
39-
"optimizer": {
40-
"steps": steps,
41-
"code_generator": code_generator_config,
42-
"evaluator": evaluator_config,
43-
"search_policy": search_policy_config,
44-
},
45-
"metadata": {"client_name": "cli", "client_version": __pkg_version__, **api_keys},
31+
response = requests.post(
32+
f"{__base_url__}/sessions", # Path is relative to base_url
33+
json={
34+
"source_code": source_code,
35+
"additional_instructions": additional_instructions,
36+
"objective": {"evaluation_command": evaluation_command, "metric_name": metric_name, "maximize": maximize},
37+
"optimizer": {
38+
"steps": steps,
39+
"code_generator": code_generator_config,
40+
"evaluator": evaluator_config,
41+
"search_policy": search_policy_config,
4642
},
47-
headers=auth_headers, # Add headers
48-
timeout=timeout,
49-
)
50-
response.raise_for_status()
51-
return response.json()
52-
except requests.exceptions.HTTPError as e:
53-
handle_api_error(e=e, console=console)
43+
"metadata": {"client_name": "cli", "client_version": __pkg_version__, **api_keys},
44+
},
45+
headers=auth_headers, # Add headers
46+
timeout=timeout,
47+
)
48+
response.raise_for_status()
49+
return response.json()
5450

5551

5652
def evaluate_feedback_then_suggest_next_solution(
57-
console: rich.console.Console,
5853
session_id: str,
5954
execution_output: str,
6055
additional_instructions: str = None,
@@ -63,41 +58,29 @@ def evaluate_feedback_then_suggest_next_solution(
6358
timeout: int = 800,
6459
) -> Dict[str, Any]:
6560
"""Evaluate the feedback and suggest the next solution."""
66-
try:
67-
# __base_url__ already contains /v1
68-
response = requests.post(
69-
f"{__base_url__}/sessions/{session_id}/suggest", # Path is relative to base_url
70-
json={
71-
"execution_output": execution_output,
72-
"additional_instructions": additional_instructions,
73-
"metadata": {**api_keys},
74-
},
75-
headers=auth_headers, # Add headers
76-
timeout=timeout,
77-
)
78-
response.raise_for_status()
79-
return response.json()
80-
except requests.exceptions.HTTPError as e:
81-
handle_api_error(e=e, console=console)
61+
response = requests.post(
62+
f"{__base_url__}/sessions/{session_id}/suggest", # Path is relative to base_url
63+
json={
64+
"execution_output": execution_output,
65+
"additional_instructions": additional_instructions,
66+
"metadata": {**api_keys},
67+
},
68+
headers=auth_headers, # Add headers
69+
timeout=timeout,
70+
)
71+
response.raise_for_status()
72+
return response.json()
8273

8374

8475
def get_optimization_session_status(
85-
console: rich.console.Console,
86-
session_id: str,
87-
include_history: bool = False,
88-
auth_headers: dict = {},
89-
timeout: int = 800, # Add auth_headers
76+
session_id: str, include_history: bool = False, auth_headers: dict = {}, timeout: int = 800
9077
) -> Dict[str, Any]:
9178
"""Get the current status of the optimization session."""
92-
try:
93-
# __base_url__ already contains /v1
94-
response = requests.get(
95-
f"{__base_url__}/sessions/{session_id}", # Path is relative to base_url
96-
params={"include_history": include_history},
97-
headers=auth_headers, # Add headers
98-
timeout=timeout,
99-
)
100-
response.raise_for_status()
101-
return response.json()
102-
except requests.exceptions.HTTPError as e:
103-
handle_api_error(e=e, console=console)
79+
response = requests.get(
80+
f"{__base_url__}/sessions/{session_id}", # Path is relative to base_url
81+
params={"include_history": include_history},
82+
headers=auth_headers,
83+
timeout=timeout,
84+
)
85+
response.raise_for_status()
86+
return response.json()

weco/cli.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def main() -> None:
258258
code_generator_config = {"model": args.model}
259259
evaluator_config = {
260260
"model": args.model,
261-
"include_analysis": False, # NOTE: False for now
261+
"include_analysis": True,
262262
}
263263
search_policy_config = {
264264
"num_drafts": max(1, math.ceil(0.15 * steps)),
@@ -388,7 +388,6 @@ def main() -> None:
388388

389389
# Send feedback and get next suggestion
390390
eval_and_next_solution_response = evaluate_feedback_then_suggest_next_solution(
391-
console=console,
392391
session_id=session_id,
393392
execution_output=term_out,
394393
additional_instructions=current_additional_instructions, # Pass current instructions
@@ -408,11 +407,7 @@ def main() -> None:
408407
# Get the optimization session status for
409408
# the best solution, its score, and the history to plot the tree
410409
status_response = get_optimization_session_status(
411-
console=console,
412-
session_id=session_id,
413-
include_history=True,
414-
timeout=timeout,
415-
auth_headers=auth_headers,
410+
session_id=session_id, include_history=True, timeout=timeout, auth_headers=auth_headers
416411
)
417412

418413
# Update the step of the progress bar
@@ -493,7 +488,6 @@ def main() -> None:
493488

494489
# Ensure we pass evaluation results for the last step's generated solution
495490
eval_and_next_solution_response = evaluate_feedback_then_suggest_next_solution(
496-
console=console,
497491
session_id=session_id,
498492
execution_output=term_out,
499493
additional_instructions=current_additional_instructions,
@@ -510,7 +504,7 @@ def main() -> None:
510504
# Get the optimization session status for
511505
# the best solution, its score, and the history to plot the tree
512506
status_response = get_optimization_session_status(
513-
console=console, session_id=session_id, include_history=True, timeout=timeout, auth_headers=auth_headers
507+
session_id=session_id, include_history=True, timeout=timeout, auth_headers=auth_headers
514508
)
515509
# Build the metric tree
516510
tree_panel.build_metric_tree(nodes=status_response["history"])
@@ -575,7 +569,11 @@ def main() -> None:
575569
console.print(end_optimization_layout)
576570

577571
except Exception as e:
578-
console.print(Panel(f"[bold red]Error: {str(e)}", title="[bold red]Error", border_style="red"))
572+
try:
573+
error_message = e.response.json()["detail"]
574+
except Exception:
575+
error_message = str(e)
576+
console.print(Panel(f"[bold red]Error: {error_message}", title="[bold red]Error", border_style="red"))
579577
# Print traceback for debugging
580-
console.print_exception(show_locals=True)
578+
# console.print_exception(show_locals=False)
581579
sys.exit(1)

weco/panels.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def __init__(
121121
self.metric = metric
122122
self.is_buggy = is_buggy
123123
self.evaluated = True
124+
self.name = ""
124125

125126

126127
class MetricTree:
@@ -181,16 +182,17 @@ def build_metric_tree(self, nodes: List[dict]):
181182
nodes.sort(key=lambda x: x["step"])
182183

183184
# Finally build the new tree
184-
for node in nodes:
185-
self.metric_tree.add_node(
186-
Node(
187-
id=node["solution_id"],
188-
parent_id=node["parent_id"],
189-
code=node["code"],
190-
metric=node["metric_value"],
191-
is_buggy=node["is_buggy"],
192-
)
185+
for i, node in enumerate(nodes):
186+
node = Node(
187+
id=node["solution_id"],
188+
parent_id=node["parent_id"],
189+
code=node["code"],
190+
metric=node["metric_value"],
191+
is_buggy=node["is_buggy"],
193192
)
193+
if i == 0:
194+
node.name = "baseline"
195+
self.metric_tree.add_node(node)
194196

195197
def set_unevaluated_node(self, node_id: str):
196198
"""Set the unevaluated node."""
@@ -232,12 +234,15 @@ def append_rec(node: Node, tree: Tree):
232234
style = None
233235
text = f"{node.metric:.3f}"
234236

237+
# add the node name info
238+
text = f"{node.name} {text}".strip()
239+
235240
s = f"[{f'{style} ' if style is not None else ''}{color}]● {text}"
236241
subtree = tree.add(s)
237242
for child in node.children:
238243
append_rec(child, subtree)
239244

240-
tree = Tree("🌳")
245+
tree = Tree("", hide_root=True)
241246
for n in self.metric_tree.get_draft_nodes():
242247
append_rec(n, tree)
243248

0 commit comments

Comments
 (0)