Skip to content

Commit fff7aa2

Browse files
committed
update circuit synthesizer UX
1 parent 7239c74 commit fff7aa2

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

src/axiomatic/magic.py

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
from IPython import get_ipython # type: ignore
2+
from IPython.core.magic import register_line_cell_magic, register_line_magic # type: ignore
3+
from IPython.display import HTML, display # type: ignore
4+
15
import platformdirs # type: ignore
26
import os
3-
4-
7+
import sys
58
from . import Axiomatic
69

710

@@ -18,6 +21,7 @@ def __init__(self):
1821
with open(f"{self.folder}/api_key", "r") as f:
1922
self.api_key = f.read()
2023
self.ax = Axiomatic(api_key=self.api_key)
24+
self.query = ""
2125

2226
def ax_api(self, query):
2327
folder = platformdirs.user_config_dir("axiomatic")
@@ -27,53 +31,63 @@ def ax_api(self, query):
2731
self.ax = Axiomatic(api_key=self.api)
2832
print("API key set.")
2933

30-
def ax_query(self, query, cell=None):
31-
from IPython import get_ipython # type: ignore
32-
from IPython.core.magic import register_line_cell_magic, register_line_magic # type: ignore
33-
from IPython.display import HTML, display # type: ignore
34-
34+
def axquery(self, query, cell=None):
3535
if self.api_key:
3636
if cell:
3737
# REFINE
38-
feedback = "" # TODO: add feedback according to interface
39-
result = self.ax.pic.refine(query=query, code=cell, feedback=feedback)
38+
try:
39+
exec(cell)
40+
feedback = "Code executed successfully."
41+
except Exception as e:
42+
feedback = f"Errors:\n{e}"
43+
print(feedback)
44+
current_query = f"{self.query}\n{query}"
45+
result = self.ax.pic.refine(
46+
query=current_query, code=cell, feedback=feedback
47+
)
4048
else:
4149
# GENERATE FROM SCRATCH
50+
self.query = query
4251
result = self.ax.pic.generate(query=query)
4352

4453
# Process output
45-
output = result.thought_text.replace("\n", "<br>")
46-
html_content = (
47-
"<div style='font-family: Arial, sans-serif; line-height: 1.5;'>"
48-
)
49-
html_content += (
50-
f"<div style='color: #6EB700;'><strong>AX:</strong> {output}</div>"
51-
)
54+
pre_thought = result.raw_content.split("<thought>")[0]
55+
thought = result.thought_text.replace("\n", "<br>")
56+
if not thought:
57+
output = result.raw_content.replace("\n", "<br>")
58+
else:
59+
output = pre_thought + "<br>" + thought
60+
html_content = f"""<div style='font-family: Arial, sans-serif; line-height: 1.5;'>"
61+
<div style='color: #6EB700;'><strong>AX:</strong> {output}</div>"""
5262
display(HTML(html_content))
5363

5464
# Process code
5565
# remove last three lines (saving file)
5666
if result.code:
5767
code = "\n".join(result.code.split("\n")[:-3] + ["c"])
58-
try:
68+
if "google.colab" in sys.modules:
5969
# When running in colab
6070
from google.colab import _frontend # type: ignore
6171

6272
_frontend.create_scratch_cell(
63-
f"""# {query}\n{code}""", bottom_pane=True
73+
f"""# {query}\n# %%ax_fix\n{code}""", bottom_pane=True
6474
)
65-
except Exception as e:
75+
else:
6676
# When running in jupyter
67-
get_ipython().set_next_input(f"{code}", replace=False)
77+
get_ipython().set_next_input(f"# %%ax_fix\n{code}", replace=False)
6878

6979
else:
7080
print(
7181
"Please set your Axiomatic API key first with the command %ax_api API_KEY and restart the kernel. Request the api key at our Customer Service."
7282
)
73-
83+
def ax_query(self, query, cell=None):
84+
# Updates the target circuit query
85+
self.query=query
86+
return self.axquery(query, cell)
87+
7488
def ax_fix(self, query, cell=None):
75-
# Just dummy at the moment
76-
return self.ax_query(query, cell)
89+
# Runs again without updating the query
90+
return self.axquery(query, cell)
7791

7892

7993
def ax_help(value: str):

0 commit comments

Comments
 (0)