-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot_tools.py
377 lines (289 loc) · 13.2 KB
/
bot_tools.py
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# from langchain.chat_models import ChatOpenAI
import requests
from langchain.tools import BaseTool, StructuredTool#, Tool, tool
from pydantic import Extra
from typing import Optional, Type
from langchain.callbacks.manager import (
AsyncCallbackManagerForToolRun,
CallbackManagerForToolRun,
)
import pexpect
import os
import subprocess
import params
import params
"""
===============================
Python Execution Tools
===============================
"""
def exec_cmd(py_str: str):
"""
Placeholder for the function. While in testing, just keeping it as a print statement
"""
print(py_str)
return "Command Executed"
def lint_cmd(py_str: str, lint_fp, py_pfx = None): # = 'agent_scripts/tmp_lint.py'
"""
Helper function to enable linting.
Creates a file, prepends text to it, lints it, then removes the file.
py_str: string to lint
py_pfx: prefix to add to string. Used if appending py_str to an existing python file
"""
with open(lint_fp, 'w') as lint_file:
if py_pfx is not None:
lint_file.write(py_pfx)
lint_file.write("\n")
lint_file.write(py_str)
# Pylint's internal reporter API fails on so just use subprocess which seesm to be more reliable
result = subprocess.run([r"c:/Users/Public/robot/polybot-env/python.exe", "-m", "pylint", lint_fp, "-d R,C,W"], stdout=subprocess.PIPE)
#"C:\Users\cnmuser\.conda\envs\calms\python.exe"
result_str = result.stdout.decode('utf-8')
with open(lint_fp, 'w') as lint_file:
pass
# os.remove(lint_fp)
result_str_split = result_str.split('\n')
result_str = '\n'.join(result_str_split[1:])
return result_str
def filter_pylint_lines(lint_output, start_ln):
"""
Filter out the pylint lines that are not needed for the output
"""
filtered_ouput = []
for line in lint_output.split('\n'):
if line.startswith("*********"):
filtered_ouput.append(line)
line_split = line.split(':')
if len(line_split) > 1:
if line_split[1].isdigit():
if int(line.split(':')[1]) > start_ln:
filtered_ouput.append(line)
return '\n'.join(filtered_ouput)
"""
===============================
Polybot Tools
===============================
"""
def polybot_exec_cmd(py_str: str):
file_path = POLYBOT_RUN_FILE_PATH
# Write the command to the file
with open(file_path, 'a') as file:
file.write(py_str + '\n')
return "Command Executed and Saved"
def python_exec_cmd(py_str: str):
"""function to execute simple python commands"""
print(py_str)
return "Command Executed and Saved"
# with open('polybot_experiment.py', 'r') as polybot_file:
# POLYBOT_FILE = ''.join(polybot_file.readlines())
# POLYBOT_FILE_FILTER = POLYBOT_FILE.replace("{", "")
# POLYBOT_FILE_FILTER = POLYBOT_FILE_FILTER.replace("}", "")
with open('polybot_experiment.py', 'r') as polybot_file:
POLYBOT_FILE = ''.join(polybot_file.readlines())
POLYBOT_FILE_FILTER = POLYBOT_FILE.replace("{", "")
POLYBOT_FILE_FILTER = POLYBOT_FILE_FILTER.replace("}", "")
POLYBOT_FILE_LINES = len(POLYBOT_FILE.split('\n'))
POLYBOT_RUN_FILE_PATH = "C:/Users/Public/robot/N9_demo_3d/polybot_screenshots/polybot_screenshots.py"
if os.path.exists(POLYBOT_RUN_FILE_PATH):
POLYBOT_RUN_FILE = ''.join(open(POLYBOT_RUN_FILE_PATH).readlines())
else:
POLYBOT_RUN_FILE = ''
POLYBOT_RUN_FILE_FILTER = POLYBOT_RUN_FILE.replace("{", "").replace("}", "")
POLYBOT_RUN_FILE_LINES = len(POLYBOT_RUN_FILE.split('\n'))
exec_polybot_tool = StructuredTool.from_function(polybot_exec_cmd,
name="WritePython",
description="Takes in a python string and execs it in the environment described by the script."
+ "The script will contain objects and functions used to interact with the instrument. "
+ "Here are some rules to follow: \n"
+ "Before running the experiment create a new python file with all the library imports (robotics, loca, rack_status, proc, pandas, etc.) or any other list that is required."
+ "Check if the requested polymer is available in the rack_status and then directly proceed with the experimental excecution"
+ "Some useful commands and instructions are provided below \n\n" + POLYBOT_FILE_FILTER)
def polybot_linter(py_str: str):
"""
Linting tool for Polybot. Prepends the Polybot file.
"""
print("running linter......")
lint_fp = POLYBOT_RUN_FILE_PATH # 'agent_scripts/tmp_lint.py' #POLYBOT_RUN_FILE_PATH
lint_output = lint_cmd(py_str, lint_fp, py_pfx=POLYBOT_RUN_FILE_FILTER)
# lint_output = filter_pylint_lines(lint_output, POLYBOT_RUN_FILE_LINES)
if ':' not in lint_output:
lint_output += '\nNo errors.'
return lint_output
exec_polybot_lint_tool = StructuredTool.from_function(
polybot_linter,
name="LintPython",
description="Takes in a python string and lints it."
+ " Always run the linter to check the code before running it."
+ " The output will provide suggestions on how to improve the code."
+ " Attempt to correct the code based on the linter output."
+ " Rewrite the code until there are no errors. "
+ " Otherwise, fix the code and check again using linter."
)
"""
===============================
S26 Tools
===============================
"""
from langchain_community.utilities.wolfram_alpha import WolframAlphaAPIWrapper
MP_API_KEY = open('keys/MP_API_KEY').read().strip()
with open('S26_commandline.py', 'r') as s26_file:
S26_FILE = ''.join(s26_file.readlines())
# Filters for langchain seems to be parsing the description as a fstring
S26_FILE = S26_FILE.replace("{", "")
S26_FILE = S26_FILE.replace("}", "")
if params.use_wolfram:
wolfram = WolframAlphaAPIWrapper()
wolfram_tool = StructuredTool.from_function(wolfram.run,
name="Calculator",
description="When performing an arithmatic operation don't assume, run them through this tool as a seperate action. Examples may include addition, subtraction, multiplicaiton, and divison.")
exec_cmd_tool = StructuredTool.from_function(exec_cmd,
name="ExecPython",
description="Takes in a python string and execs it in the envionment described by the script."
+ "The script will contain objects and functions used to interact with the instrument. "
+ "Here are some rules to follow: \n"
+ "unlock_hybrid() and lock_hybrid() must be called before and after all motor movements"
+ " and scans."
+ " The script is described below \n\n" + S26_FILE)
"""
===============================
Diffrac Tools
===============================
"""
def get_lattice(material: str):
try:
lattice = mp_get_lattice(material, MP_API_KEY)
except KeyError:
return f"Unable to find material {material}"
return f"{lattice['a']}, {lattice['b']}, {lattice['c']}, {lattice['alpha']}, {lattice['beta']}, {lattice['gamma']}"
def set_diffractometer(a: float, b: float, c: float,
alpha: float, beta: float, gamma: float, peak:list[int]):
if len(peak) != 3:
return "Peak parameters were incorrect. Instrument was NOT set"
print(a, b, c, alpha, beta, gamma)
print(peak[0], peak[1], peak[2])
return "Diffractometer Set"
lattice_tool = StructuredTool.from_function(get_lattice,
name="GetLattice",
description="Gets the lattice parameters for the specified material")
diffractometer_tool = StructuredTool.from_function(set_diffractometer,
name="SetInstrument",
description="Sets the instrument to a material's lattice. Requires the 6 lattice parameters: a,b,c,alp,bet,gam."
+ " Do not assume these parameters. Use the GetLattice tool to retrieve them."
+ " The peak parameters are supplied by the user. They are 3 integers.")
class DiffractometerAIO(BaseTool, extra=Extra.allow):
"""
Tool to query the lattice parameters from the materials project
and set the diffractometer to the retrieved position.
To disable the connection to to spec, the init_spec_ext parameter can be set to false.
"""
name: str = "setdetector"
description: str = "tool to set the diffractometer based on the material being analyzed, the parameters are first the material then the peak sepearted by a space"
def __init__(self, init_spec_ext):
super().__init__()
self.init_spec = init_spec_ext
if self.init_spec:
self.spec_session = pexpect.spawn("sixcsim", timeout=3)
def _run(
self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None
) -> str:
query_params = query.split(' ')
# TODO: Make more rhobust
material = query_params[0]
peak = query_params[-3:]
print(peak)
try:
print(query)
lattice = mp_get_lattice(material, MP_API_KEY)
except KeyError:
return f"Unable to find material {material}"
print(f'\nDEBUG: --- get lattice from Materials Project ---\n{lattice}')
spec_lattice = [lattice['a'], lattice['b'], lattice['c'],
lattice['alpha'], lattice['beta'], lattice['gamma']]
print(f'DEBUG: Setting SPEC: {spec_lattice}')
self.spec_session.sendline(f"ubr {peak[0]} {peak[1]} {peak[2]}")
self.spec_session.sendline("wh")
while(1):
try:
print(self.spec_session.readline())
except:
break
if self.init_spec:
self.spec_session.sendline("setlat")
self.spec_session.expect("real space lattice")
self.spec_session.readline()
# lats has 6 numbers, a,b,c,alf,bet,gam
for i in range(6):
self.spec_session.sendline("{0}".format(spec_lattice[i]))
self.spec_session.readline().decode()
self.spec_session.sendline("p LAMBDA")
while(1):
try:
print(self.spec_session.readline().decode())
except:
break
wh_output = []
self.spec_session.sendline("wh")
while(1):
try:
wh_output.append(self.spec_session.readline().decode())
except:
break
return ' '.join(wh_output)
async def _arun(
self, query: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None
) -> str:
"""Use the tool asynchronously."""
raise NotImplementedError("custom_search does not support async")
def mp_get_lattice(material_formula: str, apikey: str) -> dict:
"""get lattice parameters from Materials Project"""
url = "https://api.materialsproject.org/materials/summary/"
kwargs = {
'headers': {"X-API-KEY": apikey},
'params': {
# see https://api.materialsproject.org/docs#/Materials%20Summary
'formula': material_formula,
'deprecated': False,
'_fields': ','.join(
[
'material_id',
'formula_pretty',
'energy_above_hull',
'is_stable',
'theoretical',
'structure',
'symmetry',
]
),
},
}
response = requests.get(url, **kwargs)
results = response.json()['data']
energy_sorted = sorted(
[
(
# sorted by energy_above_hull
mat['energy_above_hull'],
# prefer stable and experimentally observed structures
int(not mat['is_stable']) + int(mat['theoretical']),
# original index in results
ix,
)
for ix, mat in enumerate(results)
]
)
selected = results[energy_sorted[0][2]]
symmetry = selected['symmetry']
lattice = selected['structure']['lattice']
info = {
'id': selected['material_id'],
'formula': selected['formula_pretty'],
'symmetry': symmetry['symbol'],
'crystal': symmetry['crystal_system'],
'a': lattice['a'],
'b': lattice['b'],
'c': lattice['c'],
'alpha': lattice['alpha'],
'beta': lattice['beta'],
'gamma': lattice['gamma'],
}
return info