-
Notifications
You must be signed in to change notification settings - Fork 4
/
tools.py
1265 lines (1034 loc) · 58.7 KB
/
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import os
import re
import subprocess
from LLM import complete_text, complete_text_claude
import selectors
import datetime
import shutil
import glob
import copy
import anthropic
import difflib
import numpy as np
import pandas as pd
from tqdm import tqdm
from gene import *
from achilles import download_csv
DEVICE = -1
PYTHON = "python"
USE_GPT4_EDIT_FILE = False
from get_lit_review import get_lit_review
def parse_action_input(s, entries):
s = s.split("{")[1].split("}")[0].strip()
pattern = ""
for e in entries:
pattern += f'"{e}":([\s\S]*),\s*'
pattern = pattern[:-4]
result = re.search(pattern, s, re.MULTILINE)
if result is None:
raise Exception("Invalid: " + s)
# stripe each entry
return [r.strip().strip('\"') for r in result.groups()]
# # entries is a json string, but it cannot be loaded directly because it contains unescaped things
# try:
# s = s.replace('"""', '"')
# s_processed = re.sub(r'"([^"\\]*(?:\\.[^"\\]*)*)"', lambda m: m.group(0).replace("\n", "\\n").replace("\t", "\\t").replace("\t", "\\t").replace('\\', '\\\\'), s)
# entries = json.loads(s_processed)
# except:
# raise Exception("Invalid json: " + s)
# # check if all entries are present and parse them in order
# results = []
# for e in entries:
# if e not in entries:
# raise Exception(f"Missing entry: {e}")
# results.append(entries[e])
# return results
def research_log(action_input, invalid_action_error, folder_name = ".", **kwargs):
try:
content, = parse_action_input(action_input, ["content"])
except:
return invalid_action_error
# if action == "read":
# return open(os.path.join(folder_name,"research_log.log")).read()
# elif action == "write":
# if content is None:
# return "Research Log write action requires a second content argument"
with open(os.path.join(folder_name,"research_log.log"), "a") as f:
f.write(content+"\n")
return open(os.path.join(folder_name,"research_log.log")).read()
# else:
# return "Invalid operation for Research Log. Please use one of \"read\", \"write\""
def list_files(action_input, invalid_action_error, folder_name = ".", **kwargs):
try:
path, = parse_action_input(action_input, ["dir_path"])
except:
return invalid_action_error
return subprocess.check_output(["ls", "-F", os.path.join(folder_name,path)]).decode("utf-8")
# elif operation == "cd":
# return subprocess.check_output(["cd", os.path.join(folder_name,path)]).decode("utf-8")
# elif operation == "pwd":
# return subprocess.check_output(["pwd", os.path.join(folder_name,path)]).decode("utf-8")
# else:
# return "Invalid operation"
def copy_file(action_input, invalid_action_error, folder_name = ".", **kwargs):
try:
source, destination = parse_action_input(action_input, ["source", "destination"])
except:
return invalid_action_error
shutil.copyfile(os.path.join(folder_name,source), os.path.join(folder_name,destination))
return f"File {source} copied to {destination}"
def understand_file(action_input, invalid_action_error, folder_name = ".", **kwargs):
try:
file_name, things_to_look_for = parse_action_input(action_input, ["file_name","things_to_look_for"])
except:
return invalid_action_error
try:
lines = open(os.path.join(folder_name,file_name)).readlines()
except:
return f"Error: cannot find script {file_name}"
# zip lines with line id
# lines = [f"{i+1}: {l}" for i, l in enumerate(lines)]
# group by 200 lines
blocks = ["".join(lines[i:i+200]) for i in range(0, len(lines), 200)]
descriptions = []
for idx, b in enumerate(blocks):
start_line_number = 200*idx+1
end_line_number = 200*idx+1 + len(b.split("\n"))
prompt = f"""Given this (partial) file from line {start_line_number} to line {end_line_number}:
```
{b}
```
Here is a detailed description on what to look for and what should returned: {things_to_look_for}
The description should short and also reference crtical lines in the script relevant to what is being looked for. Only describe what is objectively confirmed by the file content. Do not include guessed numbers. If you cannot find the answer to certain parts of the request, you should say "In this segment, I cannot find ...".
"""
completion = complete_text(prompt, log_file=kwargs["log_file"]+f"_{idx}")
descriptions.append(completion)
if len(descriptions) == 1:
return descriptions[0]
else:
descriptions = "\n\n".join(["Segment {idx}: \n\n" + s for s in descriptions])
prompt = f"""Given the relevant observations for each segments of a file, summarize to get a cohesive description of the entire file on what to look for and what should returned: {things_to_look_for}
{descriptions}
"""
completion = complete_text(prompt, log_file=kwargs["log_file"])
return completion
def inspect_script_lines(action_input, invalid_action_error, folder_name = ".", **kwargs):
try:
script_name, start_line_number, end_line_number = parse_action_input(action_input, ["script_name", "start_line_number", "end_line_number"])
except:
return invalid_action_error
try:
start_line_number = int(start_line_number)
end_line_number = int(end_line_number)
except:
return "Error: start_line_number and end_line_number must be integers"
if end_line_number - start_line_number > 100:
return "Error: the number of lines to display is limited to 100 lines"
try:
lines = open(os.path.join(folder_name,script_name)).readlines()
except:
return f"Error: cannot find script {script_name}"
# zip lines with line id
# lines = [f"{i+1}: {l}" for i, l in enumerate(lines)]
content = "".join(lines[max(int(start_line_number)-1, 0):int(end_line_number)])
return content
def edit_script_direct(action_input, invalid_action_error, folder_name = ".", **kwargs):
try:
script_name, start_line_number, end_line_number, edited_content = parse_action_input(action_input, ["script_name", "replace_start_line_number", "replace_end_line_number", "edited_content"])
except:
return invalid_action_error
try:
content = open(os.path.join(folder_name,script_name)).read()
except:
return f"Error: the file {script_name} does not exist"
try:
start_line_number = int(start_line_number)
end_line_number = int(end_line_number)
except:
return "Error: start_line_number and end_line_number must be integers"
lines = content.splitlines()
edited_lines = edited_content.splitlines()
# edited_lines = [ line.split(":", 1)[1] for line in edited_content.splitlines()]
new_lines = lines[:int(start_line_number)-1] + edited_lines + lines[int(end_line_number):]
new_content = "\n".join(new_lines)
# backup all old file with prefix script_name
backup_name = os.path.join(folder_name,"backup", f"{script_name}_{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}")
shutil.copyfile(os.path.join(folder_name,script_name), backup_name)
with open(os.path.join(folder_name,script_name), "w") as f:
f.write(new_content)
# new_content = "\n".join([f"{i+1}: {l}" for i, l in enumerate(new_lines)])
return "Here is the new script, please check if the edit is correct and desirable:\n\n" + new_content
def edit_script(action_input, invalid_action_error, folder_name = ".", **kwargs):
#TODO: handle long file editing
try:
script_name, instruction, save_name = parse_action_input(action_input, ["script_name", "edit_instruction", "save_name"])
except:
return invalid_action_error
try:
content = open(os.path.join(folder_name,script_name)).read()
except:
return f"Error: the file {script_name} does not exist"
# lined_content = "\n".join([f"{i+1}: {l}" for i, l in enumerate(content.splitlines())])
prompt = f"""Given this python script:
```python
{content}
```
Edit the script by following the instruction:
{instruction}
Provide the full code after the edit, making no other changes. Start the python code with "```python".
"""
# Do not edit the part marked with DO NOT EDIT, and also do not introduce new DO NOT EDIT marks. If you need to edit that part to fulfill the edit isntruction, return an error message that explains this, starting with "Error:"
if USE_GPT4_EDIT_FILE:
completion =complete_text_openai(prompt, log_file=kwargs["log_file"])
else:
completion = complete_text(prompt, log_file=kwargs["log_file"])
# detect error message
# if "Error:" in completion:
# return completion.split("Error:")[1].strip()
# parse out the new content between ```python and ```
new_content = completion.split("```python")[1].split("```")[0].strip()
# new_content = "\n".join([l.split(": ", 1)[1] for l in new_content.splitlines()])
# backup all old file with prefix script_name
backup_name = os.path.join(folder_name,"backup", f"{script_name}_{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}")
shutil.copyfile(os.path.join(folder_name,script_name), backup_name)
with open(os.path.join(folder_name,save_name), "w") as f:
f.write(new_content)
# new_lines = new_content.splitlines()
# new_content = "\n".join([f"{i+1}: {l}" for i, l in enumerate(new_lines)])
diff = list(difflib.unified_diff(content.splitlines(keepends=True), new_content.splitlines(keepends=True)))
diff = "".join(diff)
return f"The edited file is saved to {save_name}. Here is the diff, please check if the edit is correct and desirable:\n\n" + diff
def undo_edit_script(action_input, invalid_action_error, folder_name = ".", **kwargs):
try:
script_name, = parse_action_input(action_input, ["script_name"])
except:
return invalid_action_error
backup_files = glob.glob(os.path.join(folder_name,"backup", f"{script_name}_*"))
if len(backup_files) == 0:
return f"Error: cannot undo edit for {script_name}"
backup_files.sort()
backup_file = backup_files[-1]
shutil.copyfile(backup_file, os.path.join(folder_name,script_name))
# delete the backup file
os.remove(backup_file)
new_content = open(os.path.join(folder_name,script_name)).read()
# new_lines = new_content.splitlines()
# new_content = "\n".join([f"{i}: {l}" for i, l in enumerate(new_lines)])
return f"Content of {script_name} after undo the most recent edit:\n" + new_content
def run_experiment():
pass
return
def gene_search(action_input, folder_name = ".", **kwargs):
try:
gene_name = parse_action_input(action_input, ["gene_name"])[0].strip()
except:
return "Gene search failed either due to parsing, try again and follow the schema given to you on how to use this tool."
import pandas as pd
import numpy as np
return gene_search_f(gene_name, folder_name, **kwargs)
def gene_search_f(gene_name, gene_search_diverse, folder_name = ".", **kwargs):
print(gene_name)
file_path = os.path.join(folder_name, "achilles.csv")
if not os.path.exists(file_path):
download_csv(folder_name)
df = pd.read_csv(file_path, on_bad_lines="skip")
df = df.rename(lambda x : x.split(" (")[0], axis='columns')
df.drop(columns=["DepMap_ID"], inplace=True)
df.dropna(inplace=True, axis='rows')
if gene_name not in df.columns:
return f"Gene {gene_name} not found"
if gene_search_diverse:
return ", ".join((df[gene_name].dot(df) / (np.linalg.norm(df, axis=0) * np.linalg.norm(df[gene_name]))).sort_values(ascending=True)[:50].index.tolist())
else:
return ", ".join((df[gene_name].dot(df) / (np.linalg.norm(df, axis=0) * np.linalg.norm(df[gene_name]))).sort_values(ascending=False)[:11].index.tolist()[1:])
def arxiv_search(action_input, max_papers = 5, folder_name = ".", **kwargs):
try:
query = parse_action_input(action_input, ["script_name"])[0].strip()
except:
return "Arxiv search failed either due to parsing, try again and follow the schema given to you on how to use this tool."
import arxiv
search = arxiv.Search(
query = query,
id_list = [],
max_results = max_papers,
SortCriterion = SortCriterion.Relevance,
SortOrder = SortOrder.Descending
)
observation = ""
for result in arxiv.Client().results(search):
observation += "\n" + result.title + "\n\n" + result.summary + "\n"
return observation
def execute_script(action_input, invalid_action_error, folder_name = ".", **kwargs):
# TODO: handle long output
try:
script_name = parse_action_input(action_input, ["script_name"])[0].strip()
except:
return invalid_action_error
script_path = os.path.join(".",script_name)
cmd = f"CUDA_VISIBLE_DEVICES={DEVICE} {PYTHON} -u {script_path}"
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True, cwd=folder_name)
stdout_lines = []
stderr_lines = []
selector = selectors.DefaultSelector()
selector.register(process.stdout, selectors.EVENT_READ)
selector.register(process.stderr, selectors.EVENT_READ)
while process.poll() is None and selector.get_map():
events = selector.select(timeout=1)
for key, _ in events:
line = key.fileobj.readline()
if key.fileobj == process.stdout:
print("STDOUT:", line, end =" ")
stdout_lines.append(line)
else:
print("STDERR:", line, end =" ")
stderr_lines.append(line)
for line in process.stdout:
line = line
print("STDOUT:", line, end =" ")
stdout_lines.append(line)
for line in process.stderr:
line = line
print("STDERR:", line, end =" ")
stderr_lines.append(line)
return_code = process.returncode
if return_code != 0:
return "".join(stderr_lines)
else:
return "".join(stdout_lines)
def request_help(action_input, invalid_action_error, folder_name = ".", **kwargs):
try:
request, = parse_action_input(action_input, ["request"])
except:
return invalid_action_error
return input(f"AI is requesting help: {request}\n")
def reflection(action_input, invalid_action_error, folder_name = ".", research_problem = "", **kwargs):
try:
things_to_reflect_on, = parse_action_input(action_input, ["things_to_reflect_on"])
except:
return invalid_action_error
research_log_content = open(os.path.join(folder_name, "research_log.log")).read()
prompt = f"""We are trying to solve this research problem: {research_problem}
Your current research log:
```
{research_log_content}
```
Reflect on this: {things_to_reflect_on}
Give an answer in natural language paragraphs as truthfully as possible.
"""
reflection = complete_text(prompt, log_file=kwargs["log_file"])
return f"Reflection: {reflection}\n"
def summarize_action_and_observation(action, observation, **kwargs):
prompt = f"""Given your action and the observation:
{action}
[Observation]:
```
{observation}
```
Summarize your action and the observation in this format:
[Reasoning]: Summarize the reasoning behind the action
[Action]: Summarize all relevant details of the action objectively
[Observation]: Summarize all relevant details in the observation objectively
Do not include additional information or suggestions.
"""
summary = "[Reasoning]:" + complete_text(prompt, log_file=kwargs["log_file"]).split("[Reasoning]:")[1]
return summary
def retrieval_from_research_log(folder_name, research_problem, current_plan, **kwargs):
# TODO: sliding window/ vector retrieval
research_log_content = open(os.path.join(folder_name, "research_log.log")).read()
prompt = f"""We are trying to solve this research problem: {research_problem}
Your current Research Plan and Status
{current_plan}
Your current research log:
```
{research_log_content}
```
Concisely summarize and list all relevant information from the research log that will be helpful for future step in this format:
"""
retrieval = complete_text(prompt, log_file=kwargs["log_file"])
return retrieval
def update_plan_and_status(summary_of_last_step, thought_and_action, folder_name, research_problem, current_plan, **kwargs):
research_log_content = open(os.path.join(folder_name, "research_log.log")).read()
prompt = f"""We are trying to solve this research problem: {research_problem}
Current Plan and Status:
```
{current_plan}
```
Given a summary of what you did in the last step:
{summary_of_last_step}
And what you are doing in this step:
{thought_and_action}
Update the plan and status. The plan and status should be concise but informative. Do not include any result that is guessed rather than directly confirmed by the observation.
"""
plan = complete_text(prompt, log_file=kwargs["log_file"]).split("```\n")[1].split("\n```")[0]
return plan
# low_level_instructions = """
# Respond in this exact format:
# Research Log Update: New update to append to the end of the current Research Log above. Note that the previous step observation will gradually disappear after you submit the current step. So you should copy the important insights from the previous step observation to the Research Log.
# Thought: you should always think about what to do
# Action: the action to take, should be one of the name of one of the tools
# Action Input: the input to the action as a valid JSON string
# Observation:
# ```
# the result of the action
# ```
# """
def work_on_subtask(action_input, invalid_action_error, folder_name = ".", **kwargs):
try:
subtask, = parse_action_input(action_input, ["subtask"])
except:
return invalid_action_error
current_history = copy.deepcopy(kwargs["current_history"])
current_history["actions"] = []
current_history["observations"] = []
current_history["tool_names"] = current_history["low_level_tools"]
current_history["research_problem"] += f"\n\nCurrently, we decided to work on subtask: {subtask}\n"
current_history["instructions"] = low_evel_instructions
return agent_loop(current_history, steps = 20, use_gpt4 = False, log_file = kwargs["log_file"], args = kwargs["args"])
def construct_tools_prompt(tool_names):
tools_prompt = ""
tools = {}
for tool_name in tool_names:
tool = ALL_TOOLS[tool_name]
tools[tool_name] = tool
tools_prompt += f"""- {tool_name}:
{tool["description"]}
Usage:
```
{tool["usage"]}
Observation: [{tool["return"]}]
```
""".strip() + "\n\n"
return tools_prompt, tools
def parse_entries(s, entries):
pattern = ""
for e in entries:
e = e.replace("[", "\[").replace("]", "\]")
pattern += f"{e}:([\s\S]*)"
result = re.search(pattern, s, re.MULTILINE)
if result is None:
raise Exception("Invalid: " + s)
# stripe each entry
parsed = [r for r in result.groups()]
return {e: parsed[idx] for idx, e in enumerate(entries)}
def print_action(entries):
return "".join([ k + ": " + v for k,v in entries.items()])
def summarize_remaining_genes(all_genes, summary_size=20, bs=1000):
blocks = [all_genes[i:i + bs] for i in range(0, len(all_genes), bs)]
abridged_list = []
for idx, b in tqdm(enumerate(blocks), total=len(blocks)):
start_gene_number = bs * idx + 1
end_gene_number = bs * idx + 1 + bs
prompt = f"""
The full gene list is too long. Given this (partial) observation
from gene {start_gene_number} to gene {end_gene_number}:
```
{b}
```
Generate a shorter list containing {summary_size}
of these genes. Try to prioritize genes belonging to
distinct pathways such that different biological
processes and functions are comprehensively represented.
Only print out the genes separated by commans. Do not
include any additional information or explanation. Do
not include any gene that is guessed rather than
directly present in the list.
"""
completion = complete_text(prompt, model="claude-1", log_file=None)
abridged_list.append(completion)
abridged_list = ','.join(abridged_list)
abridged_list = abridged_list.split(',')
abridged_list = [x.strip(' ') for x in abridged_list]
return abridged_list
def gene_choices_prompt(prompt, num_genes_pick, remaining_genes):
index = prompt.find("Please add")
if index != -1:
prompt = prompt[:index]
else:
prompt = ''
prompt += " Please add {} more genes to this list from {}." \
"\n Remember not to include any previously tested " \
"genes. Begin this list with the word 'Solution:' " \
"".format(num_genes_pick, remaining_genes)
return prompt
def process_valid_output(gene_next_sample, curr_sample, gene_sampled,
dropped_genes, args):
new_genes_pred = list(set(gene_next_sample) -
set(gene_sampled) -
set(curr_sample))
print('New genes predicted:', len(new_genes_pred))
curr_sample = curr_sample + new_genes_pred
new_prompt = ''
if len(curr_sample) < args.num_genes:
new_prompt += "\n You have so far predicted {} out of the " \
"required {} genes for this round. These " \
"were: \n".format(len(curr_sample), args.num_genes)
new_prompt += str(curr_sample)
new_prompt += "\n Please add {} more genes to this list. " \
"Remember not to include previously " \
"tested genes including: \n ".format(args.num_genes - len(
curr_sample))
new_prompt += str(list(dropped_genes))
return curr_sample, new_prompt
else:
return curr_sample, None
def split_outside_parentheses(s, delimiter=','):
parts = []
current = []
level = 0
# Go through each character in the string
for char in s:
if char == '(':
level += 1
elif char == ')':
level -= 1
# If we find the delimiter and we are not inside parentheses
if char == delimiter and level == 0:
parts.append(''.join(current).strip())
current = []
else:
current.append(char)
# Add the last part
parts.append(''.join(current).strip())
return parts
def agent_loop(current_history, steps, use_gpt4, log_dir, args):
valid_format_entires = ["Solution"]
# valid_format_entires = ["[Reflection]", "[Research Plan and Status]","[Thought]", "[Action]","[Action Input]"]
current_plan = "Empty"
#summary_of_last_step = "Empty"
#relevant_history = ""
use_gpt4 = False
folder_name = current_history["folder_name"]
tool_names = current_history["tool_names"]
research_problem = current_history["research_problem"]
# research_problem = "Your task is to identify predict genes which important for some task. You will get some observations telling which the scores of the genes chosen by you and some of them will be hits. Your tasks is maximize hits over various rounds by taking into account the observations and prior knowledge. "
try:
if not args.combinatorial:
ground_truth = pd.read_csv('./datasets/ground_truth_' + args.data_name + '.csv',
index_col=0)
all_hit_genes = np.load('./datasets/topmovers_'+ args.data_name + '.npy')
else:
import ast # ast.literal_eval safely evaluates a string as a Python literal
ground_truth = pd.read_csv('./datasets/ground_truth_' + args.data_name + '.csv')
# Convert the string representation of the tuples back to actual tuples
ground_truth['Gene_pairs'] = ground_truth['Gene_pairs'].apply(ast.literal_eval)
ground_truth.set_index('Gene_pairs', inplace=True)
all_hit_genes = np.load('./datasets/topmovers_'+ args.data_name + '.npy')
all_hit_genes = [tuple(l) for l in all_hit_genes]
measured_genes = ground_truth.index.values
except:
print("Failed loading ground truth!!!")
measured_genes = ['TNFRSF9', 'ZAP70', 'LHX6', 'EMP3', 'CD27', 'EBF2', 'GRAP2', 'VPS29', 'CBLB', 'IL2RG', 'PLCG2', 'CD3E', 'FOXQ1', 'OTUD7A', 'LIME1', 'DEF6', 'RPL26', 'NMT1', 'NFKB2', 'SLC16A1', 'ZEB2', 'PIK3AP1', 'PI4KB', 'ITPKB', 'MUC21', 'RELA', 'IL9R', 'EIF3K', 'RIPK3', 'PSTPIP1', 'CD28', 'IL2', 'TRIM21', 'PLCG1', 'RNF40', 'MAP3K12', 'CPSF4', 'LAT2', 'CD247', 'IL1R1', 'FOXL2', 'FOSB', 'WT1', 'ARHGAP15', 'AKAP12', 'TRAF3IP2', 'CD3G', 'RPL35', 'VAV1', 'RAC2', 'MYB', 'IFNGR2', 'TSC1', 'MAP3K7', 'TNFRSF1B', 'GRAP', 'SHOC2', 'HELZ2', 'FOXL2NB', 'IRX4', 'FPR2', 'IL2RB', 'SNRPC', 'KIDINS220', 'EP400', 'RPL38', 'PSMD4', 'JAK1', 'INPPL1', 'PTPRC', 'RNF20', 'LCK', 'SPTLC2', 'CD2', 'IFNG', 'RPL19', 'MAP4K1', 'FOXF1', 'ARHGDIB', 'APOBEC3D', 'GCSAML', 'SLAMF6', 'LAT', 'FOXO4', 'EOMES', 'FOSL1', 'LTBR', 'STAT3', 'TRAF6', 'ANXA2R', 'OTUD7B', 'SRP68', 'TBX21', 'ITPKA', 'PDGFRA', 'BICDL2', 'CEACAM1', 'MCM2', 'APOL2', 'SRP19', 'RPS7', 'TAF13', 'GATA3', 'TNFRSF1A', 'EIF3D', 'CD5', 'MCM3AP', 'JMJD1C', 'CAD', 'SLA2', 'WAS', 'CDKN2C', 'MUC1', 'ITK', 'CD3D', 'EMP1', 'DGKZ', 'IKZF3', 'BRD9', 'DEPDC7', 'NRF1', 'HGS', 'MAK16', 'LCP2']
t = []
for idx, x in enumerate(measured_genes):
for y in measured_genes[idx+1:]:
t.append((x, y))
t.append((y, x))
measured_genes = t
gene_sampled = []
if not os.path.exists(log_dir):
os.makedirs(log_dir)
with open(os.path.join(log_dir, "main_log") , "w", 1) as f:
f.write("Enabled Tools:" + str(tool_names) + "\n")
tools_prompt, tools = construct_tools_prompt(tool_names)
f.write("================================Start=============================\n")
last_steps = 3
# research_log_content = open(os.path.join(folder_name, "research_log.log")).read()
f.write(current_history["initial_prompt"].format(tools_prompt=tools_prompt, tool_names=tool_names, research_problem=research_problem) + "\n")
hits_history = []
lit_review_summary = ""
for curr_step in range(steps):
#curr_step = len(current_history["actions"])
if curr_step !=0:
## Add experimental result from last run to prompt
gene_sampled = list(np.load(log_dir + '/sampled_genes_'+str(
curr_step)+'.npy'))
if args.combinatorial:
gene_sampled = [tuple(l) for l in gene_sampled]
gene_readout = ground_truth.loc[gene_sampled]
## Get list of hits
hits = list(set(gene_sampled).intersection(all_hit_genes))
print(curr_step, 'Number of cumulative hits:', str(len(hits)))
# construct prompt for the current step
#research_log_content = open(os.path.join(folder_name,
# "research_log.log")).read()
prompt = 'Step {}\n '.format(curr_step)
prompt += current_history["initial_prompt"].format(
tools_prompt=tools_prompt,
tool_names=tool_names,
research_problem=research_problem)
if args.combinatorial and args.use_single_gene:
df = pd.read_csv('./datasets/ground_truth_' + args.data_name + '.csv')
df_strong = df[(df["Gene_pairs"].str.contains("negative")) & (df["Score"].abs() < 0.01)]
df_strong["Gene"] = df_strong["Gene_pairs"].apply(lambda x: x.split(",")[0][2:-1])
prompt += "\n There are several single genes that show weak response by themselves. You should prioritize other biological factors than relying on these purely: \n"+ df_strong[["Gene","Score"]].to_string()
if args.lit_review:
lit_review_prompt = current_history["initial_prompt"].format(
tools_prompt=tools_prompt,
tool_names=tool_names,
research_problem=research_problem).split("Always respond")[0]
if curr_step > last_steps:
# prompt += "\nWe have already made some progress in this. Let's continue! \n\n"
# f.write("Research Log Update:\n" + summary)
# prompting for retrieval
log_file = os.path.join(log_dir , f"step_{curr_step}_log_retrieval.log")
# relevant_history = retrieval_from_research_log( folder_name, research_problem, current_plan, log_file=log_file)
prompt += ''
#prompt += f"""
#Here is a summary of relevant actions and observations you have done:
#```
#{relevant_history}
#```
#Here are the exact several steps you have done most recently (up to 3 steps):
# """
else:
prompt += "\nNow let's start!\n\n"
# if curr_step > last_steps:
# prompt += "Step " + str(curr_step-last_steps) + ":\n"
# else:
# prompt += "Step 0:\n"
if curr_step == 0:
pass
#prompt += current_history["instructions"]
#prompt += "\n To get you started, here is a list of " \
# "tested genes and their measured log fold change
# in INF-γ: \n" +\
# gene_readout.to_string()
#prompt += "\n Out of these, we call {} genes hits,
# since they " \
# "showed high log fold change values".format(len(hits))\
# + ground_truth.loc[hits].to_string()
else:
if len(gene_readout) < 1500:
## Append experiment results to the current prompt
prompt += "\n This is not your first round. All tested genes and " \
"their measured log fold change are: \n"\
+ gene_readout.drop(hits).to_string()
prompt += "\n You have successfully identified {} hits so " \
"far over all experiment cycles! The results for the " \
"hits are: \n".format(len(hits)) + \
ground_truth.loc[hits].to_string()
if args.lit_review:
lit_review_prompt += "\n You have successfully identified {} hits so " \
"far over all experiment cycles! The results for the " \
"hits are: \n".format(len(hits)) + \
ground_truth.loc[hits].to_string()
hits_history.append(len(hits))
else:
# summarize the results
non_hit_sum_prompt = research_problem + "\n Till now, these are all tested genes that are not hits along with their scores: \n" + gene_readout.drop(hits).to_string()
non_hit_sum_prompt += "\n Summarize this in a few lines to find some common pattern in these which will aid in the next steps of experimental design to maximize your cumulative hits."
sum_log_file = os.path.join(log_dir , f"step_{curr_step}_log_neg_sum.log")
negative_examples_summary = complete_text(non_hit_sum_prompt, model = "claude-1", log_file = sum_log_file)
hit_sum_prompt = research_problem + "\n Till now, you have identified the following genes as hits along with their scores: \n" + ground_truth.loc[hits].to_string()
hit_sum_prompt += "\n Summarize this in a few lines to find some common pattern in these which will aid in the next steps of experimental design to maximize your cumulative hits."
sum_log_file = os.path.join(log_dir , f"step_{curr_step}_log_pos_sum.log")
positive_examples_summary = complete_text(hit_sum_prompt, model = "claude-1", log_file = sum_log_file)
prompt += "\n This is not your first round. The summary of all tested genes and " \
"their measured log fold change are: \n" + negative_examples_summary
prompt += "\n The summary of these hits is the following: " + positive_examples_summary
prompt += "\n Keep this in mind while choosing genes to be perturbed for the next round as they should also have similar properties."
prompt += "\n Till now, the progression of the number of cumulative hits is as follows: " + ", ".join(map(str, hits_history))
prompt += "\n If you see the number of hits not increasing a lot over the past few rounds, rethink your design strategy to try to maximize these. One possibility could be that you are exploiting only one mode in the distribution and you might want to try some very different types of genes in order to find some different interesting possible pathways."
prompt += "\n You have successfully identified {} hits so " \
"far over all experiment cycles! You will not be shown the results until " \
"the end of all the rounds, so design your strategy accordingly. \n".format(len(hits))
hits_history.append(len(hits))
prompt += current_history["instructions"]
# prompting
f.write("Step " + str(curr_step) + ":\n")
if curr_step < 5 and args.lit_review:
print("starting literature review")
lit_review_prompt += f"\nYou might have already some literature review information as provided below. Try to gather information which is not repititive to what you have and finally helps the most in solving the research problem at hand. \n {lit_review_summary} \n "
lit_review_summary += get_lit_review(str(lit_review_prompt), model=args.model, max_number=4)
print("finished literature review")
prompt += f"\n You have done some literature review till now and have the following information at your disposal which you may use to make your predictions: \n {lit_review_summary}"
if curr_step > 0 and args.enrichment:
print("starting enrichment analysis")
pathways = get_enrichment_KEGG_pathways(hits)
candidates = get_topk_genes_in_pathways(pathways, gene_sampled)
result = "\n".join(candidates)
prompt += f"\n You have done enrichment analysis on previous hits. The genes provided have been identified as the most frequently occurring across multiple pathways in KEGG analysis. These pathways span various biological processes and maybe linked to different diseases or cellular function. \n {result}"
print("finished enrichment analysis")
if curr_step > 0 and args.reactome:
print("starting enrichment analysis")
pathways = get_enrichment(hits, database="Reactome_2022")
path_ids = []
for path in pathways:
path_ids.append(path.split()[-1])
candidates = get_topk_genes_in_reactome(path_ids, gene_sampled)
result = "\n".join(candidates)
prompt += f"\n You have done enrichment analysis on previous hits. The names provided have been identified as the candidates for most frequently occurring genes across multiple pathways in Reactome pathway analysis. These pathways span various biological processes and maybe linked to different diseases or cellular function. \n {result}"
print("finished enrichment analysis")
log_file = os.path.join(log_dir , f"step_{curr_step}_log.log")
prompt_try = str(prompt)
curr_sample = []
genes_remain_summary = None
## Help GPT count and not repeat genes
for itr in range(args.prompt_tries):
if use_gpt4:
import pdb; pdb.set_trace()
completion = complete_text_gpt4(prompt_try, stop_sequences=[
"Observation:"], log_file=log_file)
else:
completion = complete_text(prompt_try, model = args.model, log_file=log_file)
if "Gene Search:" in completion:
completion_pre = completion.split("4. Solution:")[0]
# execute action gene search
completion_pre = completion_pre + "Gene Search Result:" + gene_search_f(completion_pre.split("Gene Search:")[1].strip(), args.gene_search_diverse, args.csv_path)
completion_post = complete_text(prompt_try + anthropic.AI_PROMPT + completion_pre + "\n\n3. Solution:", model = args.model, log_file=log_file)
completion = completion_pre + "\n\n4. Solution:" + completion_post
if "Correlated Genes:" in completion:
completion_pre = completion.split("4. Solution:")[0]
# execute action gene search
completion_pre = completion_pre + "Top Correlated Genes:" + get_top_k_correlated_genes(completion_pre.split("Correlated Genes:")[1].strip())
completion_post = complete_text(prompt_try + anthropic.AI_PROMPT + completion_pre + "\n\n3. Solution:", model = args.model, ai_prompt = "", log_file=log_file)
completion = completion_pre + "\n\n4. Solution:" + completion_post
if "Active Tissues:" in completion:
completion_pre = completion.split("4. Solution:")[0]
# execute action gene search
completion_pre = completion_pre + "Active Tissues:" + get_rna_seq(completion_pre.split("Active Tissues:")[1].strip())
completion_post = complete_text(prompt_try + anthropic.AI_PROMPT + completion_pre + "\n\n3. Solution:", model = args.model, ai_prompt = "", log_file=log_file)
completion = completion_pre + "\n\n4. Solution:" + completion_post
if "Reactome Pathways:" in completion:
completion_pre = completion.split("4. Solution:")[0]
# execute action gene search
completion_pre = completion_pre + "Reactome Pathways:" + get_gene_to_reactome_pathways(completion_pre.split("Reactome Pathways:")[1].strip())
completion_post = complete_text(prompt_try + anthropic.AI_PROMPT + completion_pre + "\n\n3. Solution:", model = args.model, ai_prompt = "", log_file=log_file)
completion = completion_pre + "\n\n4. Solution:" + completion_post
# parse the action and action input
try:
entries = parse_entries(completion,
[e.strip() for e in
valid_format_entires])
valid_format = True
except:
valid_format = False
if not valid_format:
print(itr, 'Invalid output')
prompt_update = '' # this will remove prompt_update from the prompt!!
else:
## Save predicted gene list
pred_genes = entries['Solution'].replace("\n", ",").split(',')
pred_genes = [p.strip(' \n[]') for p in pred_genes]
pred_genes = [p.split('.')[-1].strip(' ') for p in
pred_genes]
if args.combinatorial:
pred_genes = [tuple(sorted(s.split(" + "))) for s in pred_genes]
gene_next_sample = list(set(pred_genes).intersection(set(
measured_genes)))
print('Dropped genes:',
len(pred_genes) - len(gene_next_sample))
dropped_genes = set(pred_genes) - set(gene_next_sample)
curr_sample, prompt_update = \
process_valid_output(gene_next_sample, curr_sample,
gene_sampled, dropped_genes, args)
if prompt_update is None:
all_sampled_so_far = gene_sampled + curr_sample[:args.num_genes]
np.save(log_dir + '/sampled_genes_' + str(curr_step + 1) +
'.npy', all_sampled_so_far)
break
if itr >= args.prompt_tries - 4:
if genes_remain_summary is None:
# Start choosing from gene list instead of random sample
num_genes_pick = args.num_genes - len(curr_sample)
genes_remain = list(set(measured_genes).difference(set(gene_sampled)))
genes_remain_summary = summarize_remaining_genes(genes_remain)
else:
genes_remain_summary = list(set(
genes_remain_summary).difference(set(curr_sample)))
prompt_update = gene_choices_prompt(prompt_update,
num_genes_pick, genes_remain_summary)
if itr == args.prompt_tries-1:
np.save(log_dir + '/sampled_genes_' + str(curr_step + 1) +
'.npy', gene_sampled)
else:
prompt_try = prompt + prompt_update
if not args.critique:
continue
log_file = os.path.join(log_dir , f"step_{curr_step}_critique_log.log")
prompt_c = f"""You are a scientist working on problems in drug discovery.
Research Problem: {research_problem}
"""
if curr_step == 0:
pass
else:
prompt_c += "\n All tested genes so far and " \
"their measured log fold change are: \n"\
+ gene_readout.drop(hits).to_string()
prompt_c += "\n The results for the hits are: \n".format(len(hits)) + \
ground_truth.loc[hits].to_string()
prompt_c += "\n\nNow for the next round of experiment your students are planning on testing the following genes: \n" + str(curr_sample[:args.num_genes])
prompt_c += f"""\n\nAs an advisor, please critique this plan and suggest some changes to it. Use this format:
1. Critique: include all relevant details of the critique.
2. Updated Solution: Give an updated selection of {args.num_genes} genes based on the critique separated by commas in this format:: 1. <Gene name 1>, 2. <Gene name 2> ... \n
Please do not critique/make changes if there is no need to make a change.
"""
prompt_try = str(prompt_c)
curr_sample = []
## Help GPT count and not repeat genes
for itr in range(args.prompt_tries):
if use_gpt4:
import pdb; pdb.set_trace()
completion = complete_text_gpt4(prompt_try, stop_sequences=[
"Observation:"], log_file=log_file)
else:
completion = complete_text(prompt_try, model = args.model, log_file=log_file)
# parse the action and action input
try:
entries = parse_entries(completion,
[e.strip() for e in valid_format_entires])
valid_format = True
except:
valid_format = False
if not valid_format:
print(itr, 'Invalid output')
prompt_update = ''
else:
## Save predicted gene list
pred_genes = entries['Solution'].split(',')