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
+
1
5
import platformdirs # type: ignore
2
6
import os
3
-
4
-
7
+ import sys
5
8
from . import Axiomatic
6
9
7
10
@@ -18,6 +21,7 @@ def __init__(self):
18
21
with open (f"{ self .folder } /api_key" , "r" ) as f :
19
22
self .api_key = f .read ()
20
23
self .ax = Axiomatic (api_key = self .api_key )
24
+ self .query = ""
21
25
22
26
def ax_api (self , query ):
23
27
folder = platformdirs .user_config_dir ("axiomatic" )
@@ -27,53 +31,63 @@ def ax_api(self, query):
27
31
self .ax = Axiomatic (api_key = self .api )
28
32
print ("API key set." )
29
33
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 ):
35
35
if self .api_key :
36
36
if cell :
37
37
# 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
+ )
40
48
else :
41
49
# GENERATE FROM SCRATCH
50
+ self .query = query
42
51
result = self .ax .pic .generate (query = query )
43
52
44
53
# 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>"""
52
62
display (HTML (html_content ))
53
63
54
64
# Process code
55
65
# remove last three lines (saving file)
56
66
if result .code :
57
67
code = "\n " .join (result .code .split ("\n " )[:- 3 ] + ["c" ])
58
- try :
68
+ if "google.colab" in sys . modules :
59
69
# When running in colab
60
70
from google .colab import _frontend # type: ignore
61
71
62
72
_frontend .create_scratch_cell (
63
- f"""# { query } \n { code } """ , bottom_pane = True
73
+ f"""# { query } \n # %%ax_fix \n { code } """ , bottom_pane = True
64
74
)
65
- except Exception as e :
75
+ else :
66
76
# 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 )
68
78
69
79
else :
70
80
print (
71
81
"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."
72
82
)
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
+
74
88
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 )
77
91
78
92
79
93
def ax_help (value : str ):
0 commit comments