-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholp_lib.py
1277 lines (987 loc) · 46.8 KB
/
olp_lib.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
############################################# Imports #####################################################
import sys
import os
import re
import pickle
import json
import tiktoken
import numpy as np
import pandas as pd
from datetime import datetime as dt
FOON_API_path = './foon_to_pddl/foon_api'
if FOON_API_path not in sys.path:
sys.path.append(FOON_API_path)
from foon_to_pddl.foon_api import FOON_graph_analyser as fga
from foon_to_pddl.foon_api import FOON_parser as fpa
# NOTE: import for Google's Gemini API:
# -- check more here: https://ai.google.dev/gemini-api/docs/quickstart?lang=python
try:
import google.generativeai as genai
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
except ImportError:
pass
# NOTE: imports for OpenAI's API:
from openai import OpenAI
from pathlib import Path
from sklearn.metrics.pairwise import cosine_similarity
from typing import Type
##############################################################################################################
# NOTE: LLM INTERFACER OBJECTS BELOW:
##############################################################################################################
class OpenAIInterfacer(object):
def __init__(
self,
model_embed: str = "text-embedding-3-small",
model_text_gen: str = "gpt-4-turbo"
) -> None:
self.model_text_gen = model_text_gen
self.model_embed = model_embed
self.client = OpenAI()
self.encoding = tiktoken.encoding_for_model(model_embed)
def prompt(
self,
chat_history: str,
args: dict = {},
verbose: bool = False
) -> tuple:
if verbose:
print("*" * 75)
print(f"Model: {self.model_text_gen}\nComplete prompt:\n{json.dumps(chat_history, indent=4)}")
# -- make sure we add default arguments:
if 'temperature' not in args: args['temperature'] = 0.1
if 'max_tokens' not in args: args['max_tokens'] = 2500
if 'frequency_penalty' not in args: args['frequency_penalty'] = 0
if 'presence_penalty' not in args: args['presence_penalty'] = 0
# -- create a completion request:
completion = self.client.chat.completions.create(
model=self.model_text_gen,
messages=chat_history,
temperature=args['temperature'],
max_tokens=args['max_tokens'],
top_p=1,
frequency_penalty=args['frequency_penalty'],
presence_penalty=args['presence_penalty'],
)
if verbose:
print("*" * 75)
# -- extract the message component from the returned completion object:
response = completion.choices[0].message
return (response.role, response.content)
def embed(
self,
text: str,
verbose: bool = False
) -> list:
if verbose:
print("*" * 75)
print(f"Model: {self.model_embed}\nText to embed: '{text}'")
return self.client.embeddings.create(
input=text, model=self.model_embed).data[0].embedding
def num_tokens(
self,
text: str,
) -> int:
return len(self.encoding.encode(text))
class GeminiInterfacer(object):
def __init__(
self,
model_embed: str = "text-embedding-004",
model_text_gen: str = "gemini-1.5-flash"
) -> None:
self.model_text_gen = model_text_gen
self.model_embed = model_embed
def prompt(
self,
prompt: str,
chat_history: list = [],
args: dict = {'temperature': 0.1, 'max_tokens': 2000},
verbose: bool = False,
) -> str:
if verbose:
print("*" * 75)
print(f"Model: {self.model_text_gen}\nComplete prompt:\n{json.dumps(prompt, indent=4)}")
# -- create a completion request:
chat = genai.GenerativeModel(model_name=self.model_text_gen).start_chat(history=chat_history)
response = chat.send_message(prompt)
if verbose:
print("*" * 75)
# -- extract the message component from the returned completion object:
return response.text
def embed(
self,
text: str,
verbose: bool = False
) -> list:
if verbose:
print("*" * 75)
print(f"Model: {self.model_embed}\nText to embed: '{text}'")
return genai.embed_content(model="models/text-embedding-004", content=text)
##############################################################################################################
def cos_similarity(
openai_obj: Type[OpenAIInterfacer],
str_1: str = None,
str_2: str = None,
vec_1: Type[np.array] = None,
vec_2: Type[np.array] = None,
) -> float:
# NOTE: this is a helper function that will make cosine similarity easier:
vector_1, vector_2 = vec_1, vec_2
if not vector_1:
# -- embed the strings provided to the function:
vector_1 = openai_obj.embed(text=str_1)
if not vector_2:
# -- embed the strings provided to the function:
vector_2 = openai_obj.embed(text=str_2)
return cosine_similarity(
np.array(vector_1).reshape(1, -1),
np.array(vector_2).reshape(1, -1))[0]
def chat_with_llm(
openai_obj: Type[OpenAIInterfacer],
chat_history: list,
args: dict = {},
):
while True:
prompt = input("Type your new prompt for the LLM or press ENTER to end >> ")
if not bool(prompt): break
chat_history.extend([{"role": "user", "content": prompt}])
_, response = openai_obj.prompt(chat_history, args)
chat_history.extend([{"role": "assistant", "content": response}])
print(f"\n{'*' * 50}\nuser: {prompt}\nGPT: {response}\n{'*' * 50}\n")
return chat_history
def parse_llm_code(
llm_output: str,
separator: str = " ",
) -> str:
if "```" not in llm_output:
return llm_output
valid_lines = []
for line in llm_output.split('\n'):
if "`" not in line:
valid_lines.append(line)
return separator.join(valid_lines)
def llm_grounding_sim_objects(
openai_obj: Type[OpenAIInterfacer],
objects_in_OLP: list[str],
objects_in_sim: list[str],
state_as_text: str = None,
task: str = None,
verbose: bool = False,
) -> dict:
# -- prompt LLMs to perform object grounding
# (remove any objects that do not require grounding -- these are handled by the task planner system):
objects_in_OLP = list(set(objects_in_OLP) - set(['hand', 'air', 'nothing', 'robot', 'table', 'work surface']))
if verbose:
print("\t-> objects in OLP:", objects_in_OLP)
print("\t-> objects in scene:", objects_in_sim)
print()
real_objects, sim_objects = list(objects_in_OLP), list(objects_in_sim)
object_mapping = dict()
interaction = []
assert bool(real_objects), "empty objects?"
# -- we only prompt the LLM if there are objects we haven't gotten groundings for:
if state_as_text and task:
prompt_with_state = (
f"Your task is to map object names to simulation objects for the task \"{task}\"). "
f"Create a mapping that will reduce the number of actions needed by the robot. "
"Example 1:"
"\n- Action: \"Put first block on second block\""
"\n- Object names: [\"first block\", \"second block\"]"
"\n- Simulated objects: [\"block_1\", \"block_2\"]"
"\n- Environment State:"
"\n - \"block_1\" is on \"block_2\""
"\n - \"block_2\" is under \"block_1\""
"\n- Object mapping: \{\"first block\": \"block_1\", \"second block\": \"block_2\"\} (since \"block_1\" on \"block_2\" satisfies the action)"
"\n\nExample 2:"
"\n- Action: \"Put first block on second block\""
"\n- Object names: [\"first block\", \"second block\"]"
"\n- Simulated objects: [\"block_1\", \"block_2\"]"
"\n- Environment State:"
"\n - \"block_2\" is on \"block_1\""
"\n - \"block_1\" is under \"block_2\""
"\n- Object mapping: \{\"first block\": \"block_2\", \"second block\": \"block_1\"\} (since \"block_1\" on \"block_2\" satisfies the action)"
)
interaction.extend([{"role": "system", "content": prompt_with_state}])
prompt_for_mapping = (
"Map every object name to the best simulation object candidate from the provided list. "
"Do not assign each object more than once. "
"Format your answer as a Python dictionary without any explanation. "
f"\nObject names: {real_objects}"
f"\nSimulation objects: {sim_objects}"
f"\nEnvironment State:\n{state_as_text}" if state_as_text else ""
)
interaction.extend([{"role": "user", "content": prompt_for_mapping}])
while True:
_, response = openai_obj.prompt(interaction)
regex_matches = re.findall(r'\{(?<={)[^}]*\}', str(parse_llm_code(response)))
if verbose:
print(json.dumps(interaction, indent=4))
if bool(regex_matches):
# -- this means that the LLM has proposed some object groundings for us:
# -- now we will consolidate all object groundings into an updated dictionary:
object_mapping.update(eval(regex_matches.pop()))
interaction.extend([{"role": "assistant", "content": response}])
break
return object_mapping, interaction
def llm_grounding_pddl_types(
openai_obj: Type[OpenAIInterfacer],
objects_in_sim: list[str],
pddl_types: list = ['container'],
verbose: bool = False,
) -> dict:
# -- we have special object types for task-level planning;
# we will use the LLM to identify objects of special types.
groundings = {}
interaction = []
for T in pddl_types:
interaction = [{
"role": "user",
"content": (f"Which objects listed below can be classified as a \"{T}\" based on their names alone?"
" Format your answer as a Python list, and do not include any explanation."
f"\nSimulated objects: {objects_in_sim}")
}]
_, response = openai_obj.prompt(interaction)
interaction.extend([{
"role": "assistant",
"content": response,
}])
groundings[T] = eval(parse_llm_code(response))
if verbose:
print(groundings)
return groundings
def codify_FOON(
FOON: list[fga.FOON.FunctionalUnit],
verbose: bool = False,
):
# NOTE: this function will "codify" functional units as a JSON structure, which will make it easier for a LLM to process.
all_objects = []
encoded_FOON = []
for N in range(len(FOON)):
# -- get a single functional unit from the FOON graph we've provided as a list:
functional_unit = FOON[N]
# -- first, let's populate this dictionary mapping object names to their preconditions/effects:
object_states = {}
for O in list(functional_unit.getInputNodes() + functional_unit.getOutputNodes()):
obj_name = O.getObjectLabel()
if obj_name not in object_states:
object_states[obj_name] = {'preconditions': [], 'effects': []}
all_objects.extend(list(object_states.keys()))
# -- now we will go through all the input nodes to extract preconditions:
for state_type in ['preconditions', 'effects']:
# -- one loop to rule them all...
nodes_to_iterate = functional_unit.getInputNodes()
if state_type != 'preconditions':
nodes_to_iterate = functional_unit.getOutputNodes()
for O in nodes_to_iterate:
for x in range(O.getNumberOfStates()):
if O.getStateLabel(x) != 'contains':
state = f"{O.getStateLabel(x)}{' 'if not bool(O.getRelatedObject(x)) else f' {O.getRelatedObject(x)}'}"
object_states[O.getObjectLabel()][state_type].append(state)
else:
for ingredient in O.getIngredients():
object_states[O.getObjectLabel()][state_type].append(f"contains {ingredient}")
# -- now let's put it all together:
encoded_FOON.append({
'step': N+1,
'action': functional_unit.getMotionNode().getMotionLabel(),
'required_objects': list(object_states.keys()),
'object_states': object_states,
'instruction': functional_unit.toSentence(),
})
if verbose:
print(json.dumps(encoded_FOON[-1], indent=4))
return {
"all_objects": all_objects,
"plan": encoded_FOON,
}
def llm_summarize_FOON(
openai_obj: Type[OpenAIInterfacer],
FOON: list[fga.FOON.FunctionalUnit],
verbose: bool = False,
) -> list[str]:
interaction = [{
"role": "user",
"content": (
"Below is a plan given as a JSON: a plan consists of a list of actions to complete a task. "
"Summarize each action into a concise set of instructions. "
f"Use only one sentence per action.\n\n{codify_FOON(FOON, verbose)}"
)
}]
_, task_steps = openai_obj.prompt(interaction)
interaction.extend([{
"role": "assistant",
"content": task_steps,
}])
interaction.extend([{
"role": "user",
"content": "Write a concise sentence describing the plan's objective or task."
}])
_, task_description = openai_obj.prompt(interaction)
interaction.extend([{
"role": "assistant",
"content": task_description,
}])
if verbose:
print(interaction)
return task_description, list(filter(None, task_steps.split('\n')))
def generate_from_FOON(
openai_obj: Type[OpenAIInterfacer],
query: str,
FOON_samples: list,
scenario: dict = None,
system_prompt_file: str = "./llm_prompts/foon_system_prompt.txt",
human_feedback: bool = False,
use_embedding: bool = True,
top_k: int = 3,
verbose: bool = True,
) -> tuple[dict, list]:
# -- we need to make sure that we are passing a valid object for interacting with the OpenAI platform:
assert openai_obj != None, "WARNING: OpenAIInterfacer object has not been created!"
######################################################################################
# NOTE: Priming stage:
######################################################################################
# -- first, we are gonna give LLM context about the FOON generation task as a system prompt:
system_prompt = open(system_prompt_file, 'r').read()
# -- we will keep track of the entire interaction for context:
interaction = [
{"role": "system", "content": system_prompt},
{
"role": "user",
"content": f"Your task will be to create a step-by-step plan for the following prompt: {query}."
+ (f" The following objects are available in the scene: {scenario['objects']}. " if scenario['objects'] else " ")
+ "Say 'Okay!' if you understand the task."
}
]
_, prelims_response = openai_obj.prompt(interaction, verbose=verbose)
if "okay" not in prelims_response.lower():
return None
interaction.extend([
{"role": "assistant", "content": prelims_response},
])
######################################################################################
# NOTE: Stage 1 prompting:
######################################################################################
if verbose:
print(f"{'*' * 75}\nStage 1 Prompting:\n{'*' * 75}")
encoded_FOONs = []
for x in range(len(FOON_samples)):
# -- use the LLM to summarize the FOON samples we have in our repository:
task, steps = llm_summarize_FOON(openai_obj, FOON_samples[x]['foon'])
encoded_FOONs.append({
'task_description': task,
'language_plan': "\n".join(steps),
'json': codify_FOON(FOON_samples[x]['foon']),
})
top_k_foons = None
if not use_embedding:
# -- first, we are gonna give LLM context about the FOON generation task as a system prompt:
stage1a_prompt = (
"Below are a list of prototype task descriptions."
f" Pick no more than {top_k} tasks that are most similar to the new task prompt."
" Give your answer as a Python list without explanation like \"[<num>, <num>, <num>]\", where <num> refers to the tasks below."
"\n\nPrototype tasks:"
)
for x in range(len(encoded_FOONs)):
stage1a_prompt = f"{stage1a_prompt}\n- Task Description #{x+1}:\n{encoded_FOONs[x]['task_description']}"
if verbose:
print(stage1a_prompt)
interaction.extend([{"role": "user", "content": stage1a_prompt}, ])
_, stage1a_response = openai_obj.prompt(interaction, verbose=verbose)
if verbose:
print(f"{'*' * 75}\nStage 1 Response:\n{stage1a_response}\n{'*' * 75}")
interaction.extend([{"role": "assistant", "content": stage1a_response}, ])
top_k_foons = eval(stage1a_response)
else:
# -- we will use text embedding to decide upon the top 3 most similar task descriptions:
task_relevance_scores = []
query_vec = openai_obj.embed(query, verbose=verbose)
for x in range(len(encoded_FOONs)):
# -- find text similarity using cosine similarity:
score = cos_similarity(
openai_obj=openai_obj,
vec_1=query_vec,
vec_2=openai_obj.embed(encoded_FOONs[x]['task_description'], verbose=verbose))
task_relevance_scores.append((x+1, score))
# -- sort them in descending order:
task_relevance_scores.sort(key=lambda x: x[1], reverse=True)
top_k_foons = [x[0] for x in task_relevance_scores[:min(len(task_relevance_scores), top_k)]]
# -- now that we've identified similar task descriptions, we will now ask the LLM to implicitly pick the FOON that closely resembles this current task:
stage1b_prompt = (
"Out of the following prototype task plans, select the one closest to the new task prompt."
" Give your answer as \"Prototype #<num>\", where <num> refers to a task number."
)
for x in top_k_foons:
stage1b_prompt = f"{stage1b_prompt}\n\nPrototype Task #{x}:\n{encoded_FOONs[x-1]['language_plan']}"
if verbose:
print(f"Prototype Task #{x}:\n{encoded_FOONs[x-1]['language_plan']}\n")
interaction.extend([{"role": "user", "content": stage1b_prompt}, ])
_, stage1b_response = openai_obj.prompt(interaction, verbose=verbose)
if verbose:
print(f"{'*' * 75}\nStage 1 Response:\n{stage1b_response}\n{'*' * 75}")
interaction.extend([{"role": "assistant", "content": stage1b_response}, ])
selected_example = int(stage1b_response.split("#")[-1]) - 1
if verbose:
print(selected_example)
######################################################################################
# NOTE: Stage 2 prompting:
######################################################################################
if verbose:
print(f"{'*' * 75}\nStage 2 Prompting:\n{'*' * 75}")
# -- get a list of instructions that satisfies the given prompt:
stage2a_prompt = (
f"Generate a concise plan using the prototype as inspiration for the task: {query}{'.' if '.' not in query else ''}"
" Follow all guidelines. "
" Give evidence to support your plan logic."
)
interaction.extend([{"role": "user", "content": stage2a_prompt}])
_, stage2a_response = openai_obj.prompt(interaction, args={'temperature': 0.2}, verbose=verbose)
if verbose:
print(f"{'*' * 75}\nStage 2a Response:\n{stage2a_response}\n{'*' * 75}")
interaction.extend([
{"role": "assistant", "content": stage2a_response}
])
if human_feedback:
input(f"Give your opinion on the plan sketch given by the LLM: {stage2a_response}")
interaction = chat_with_llm(openai_obj, interaction, {"temperature": 0.2})
stage2b_prompt = (
"Make a Python list of used objects in the following format: [\"object_1\", \"object_2\", ...]'. "
"If there are several instances of an object type, list them individually (e.g., ['first apple', 'second apple'] if two apples are used). "
"Do not add any explanation."
)
interaction.extend([
{"role": "user", "content": stage2b_prompt}
])
_, stage2b_response = openai_obj.prompt(interaction, verbose=verbose,)
if verbose:
print(f"{'*' * 75}\nStage 2b Response:\n{stage2b_response}\n{'*' * 75}")
required_objects = eval(parse_llm_code(stage2b_response))
stage2c_prompt = (
"Format your generated plan as a JSON dictionary. "
"List as many states as possible when describing each object's preconditions and effects. "
"Each required object should match a key in \"object_states\": Be consistent with object names across actions. "
f"Use this JSON prototype as reference:\n\n{codify_FOON(FOON_samples[selected_example]['foon'])}"
)
interaction.extend([
{"role": "assistant", "content": stage2b_response},
{"role": "user", "content": stage2c_prompt}
])
# -- check if there is some sort of break between responses given by the LLM:
plan_as_json = None
entire_response = []
while True:
_, stage2c_response = openai_obj.prompt(
interaction,
verbose=verbose)
entire_response.append(stage2c_response)
try:
# -- use eval() function to parse response obtained from as a dictionary:
plan_as_json = eval(parse_llm_code("".join(entire_response)))
except SyntaxError as err:
print(f"--warning: EOL (overflow): {err.msg}")
interaction.extend([{"role": "assistant", "content": stage2c_response}])
else: break
assert bool(plan_as_json), "Something went wrong here?"
if verbose:
print(plan_as_json)
interaction.extend([{"role": "assistant", "content": "".join(entire_response)}])
language_plan = []
for action in plan_as_json['plan']:
language_plan.append(f"{action['step']}. {action['instruction']}")
# -- returning parsed content as well as chat history for further interaction (and testing):
llm_output = {
"task_prompt": query,
"all_objects": required_objects,
"language_plan": language_plan,
"object_level_plan": plan_as_json,
}
return llm_output, interaction
###################################################################################################################
# NOTE: COMPREHENSIVE OBJECT-LEVEL PLANNING METHODS:
##############################################################################################################
def top_fewshot_examples(
openai_obj: Type[OpenAIInterfacer],
fewshot_examples: dict,
query: str,
method: list = ['olp', 'blocks'], # NOTE: ['olp'], ['llm+p', 'blocks'|'packing'|'llm+p-cocktail']
verbose: bool = False,
) -> dict:
if 'olp' in method:
# -- we only find the closest examples for stage 2 prompting for OLP:
select_examples = fewshot_examples[f'{method[0]}_examples']['stage2']
elif 'llm+p' in method or 'delta' in method:
# -- we will provide a few examples per task type:
select_examples = fewshot_examples[f'{method[0]}_examples'][method[1]]
else: return None
sample_queries = [I['task_prompt'] for I in select_examples]
# -- use text embedding and cosine similarity to score and find the most similar examples:
scores = [cos_similarity(openai_obj, query, I) for I in sample_queries]
# NOTE: 'score_mapping' is a dictionary that can be used to see the score mapped to its query text:
score_mapping = {sample_queries[I]: scores[I] for I in range(len(sample_queries))}
if verbose:
print(scores)
print(score_mapping)
# -- sort examples in descending order of scoring and add them to the prompt:
sorted_examples = [x for _, x in sorted(zip(scores, select_examples), reverse=True)]
return sorted_examples
def embed_olp(
openai_obj: Type[OpenAIInterfacer],
llm_output: dict,
func_units: Type[fga.FOON.FunctionalUnit],
embedding_fpath: str = 'olp_embeddings.pkl',
verbose: bool = False
) -> None:
# NOTE: this function will be used to create a storage file with embeddings that will be used for retrieval
embeds = []
# -- check if there is an existing embedding file:
if os.path.exists(embedding_fpath):
with open(embedding_fpath, 'rb') as ef:
while True:
try:
embeds.extend(pickle.load(ef))
except EOFError:
break
text_to_embed = (
f"Task Prompt: {llm_output['task_prompt']}"
f"\nTask Plan:\n{llm_output['language_plan']}"
f"\nRequired Objects:{llm_output['all_objects']}"
)
embeds.append({
'llm_output': llm_output,
'functional_units': func_units,
'embedding': openai_obj.embed(text_to_embed, verbose=verbose),
})
if verbose: print(embeds)
pickle.dump(embeds, open(embedding_fpath, 'wb'))
def find_similar_olp(
openai_obj: Type[OpenAIInterfacer],
query: str,
embedding_fpath: str = 'olp_embeddings.pkl',
verbose: bool = False,
top_k: int = 3,
) -> dict:
# NOTE: this function will iterate through all of the
embeds = []
# -- check if there is an existing embedding file:
if os.path.exists(embedding_fpath):
with open(embedding_fpath, 'rb') as ef:
while True:
try:
embeds.extend(pickle.load(ef))
except EOFError:
break
else: return None
if not bool(embeds):
return None
task_scores = []
for E in range(len(embeds)):
score = cos_similarity(
openai_obj=openai_obj,
vec_1=embeds[E]['embedding'],
str_2=query)
task_scores.append((embeds[E], score))
task_scores.sort(key=lambda x: x[1])
return task_scores[:min(len(task_scores), top_k)]
def generate_olp(
openai_obj: Type[OpenAIInterfacer],
query: str,
stage1_sys_prompt_file: str,
stage2_sys_prompt_file: str,
stage3_sys_prompt_file: str,
stage4_sys_prompt_file: str,
scenario: dict = None,
fewshot_examples: dict = None,
num_fewshot: int = 1,
verbose: bool = True,
) -> tuple[dict, list]:
# -- we need to make sure that we are passing a valid object for interacting with the OpenAI platform:
assert openai_obj != None, "WARNING: OpenAIInterfacer object has not been created!"
def get_stage1_objects(response: str) -> str:
# NOTE: this function will parse the output generated from Stage 1 prompting to identify all objects needed for a task:
# -- we will look for the line in the output indicat
all_objects = []
for line in response.lower().split("\n"):
if "all_objects" in line:
try:
# -- we use eval() to remove any quotations that indicate some type of string:
all_objects = eval(line.split(":")[1].strip())
except Exception:
# -- if for whatever reason the LLM does not list objects enclosed in quotations,
# then we just split like normal:
objects = line.split(":")[1].strip().split(",")
all_objects = [x.strip().replace(".", "")
for x in objects]
return all_objects
# enddef
######################################################################################
# NOTE: Priming stage:
######################################################################################
# NOTE: here is the list of instructions given to the LLM for generating a high-level plan as part of stage 1:
system_prompt = open(stage1_sys_prompt_file, 'r').read()
system_prompt = system_prompt.replace("<insert_examples>", fewshot_examples["olp_examples"]["stage1"][scenario['name']])
# -- we will keep track of the entire interaction for context:
interaction = [
{"role": "system", "content": system_prompt},
{
"role": "user",
"content": f"Your task will be to create a step-by-step plan for the following prompt: {query}."
+ (f" The following objects are available in the scene: {scenario['objects']}. " if scenario['objects'] else " ")
+ "Say 'Okay!' if you understand the task."
}
]
_, prelims_response = openai_obj.prompt(interaction, verbose=verbose)
if "okay" not in prelims_response.lower():
return None
interaction.extend([
{"role": "assistant", "content": prelims_response},
])
######################################################################################
# NOTE: Stage 1 prompting:
######################################################################################
if verbose:
print(f"{'*' * 75}\nStage 1 Prompting:\n{'*' * 75}")
# -- get a list of instructions that satisfies the given prompt:
stage1a_prompt = f"Generate a plan for the task. Think step by step and follow all instructions."
interaction.extend([{"role": "user", "content": stage1a_prompt}, ])
_, stage1a_response = openai_obj.prompt(interaction, verbose=verbose)
if verbose:
print(f"{'*' * 75}\nStage 1 Response:\n{stage1a_response}\n{'*' * 75}")
if not scenario['objects']:
# -- get the list of all objects used in the generated plan:
stage1b_prompt = "List all objects created by or needed in the high-level plan."\
" Follow the format: 'all_objects': [\"object_1\", \"object_2\", \"object_3\", ...]'"
else:
# -- ask the LLM to see if the list of objects is complete:
# stage1b_prompt = "Are there any objects missing from the above list?"\
# " Output a corrected list with the following format: 'all_objects: [\"object_1\", \"object_2\", \"object_3\", ...]'"
stage1b_prompt = "Make a list of atomic objects used for this task in the following format: 'all_objects: [\"object_1\", \"object_2\", \"object_3\", ...]'. "\
"If there are several instances of an object type, list them individually (e.g., 'first apple', 'second apple' if two apples are used)."
interaction.extend([
{"role": "assistant", "content": stage1a_response},
{"role": "user", "content": stage1b_prompt}
])
_, stage1b_response = openai_obj.prompt(interaction, verbose=verbose)
all_objects = get_stage1_objects(stage1b_response)
if verbose:
print(f"\nExtracted all_objects:", all_objects)
######################################################################################
# NOTE: Stage 2 prompting:
######################################################################################
if verbose:
print(f"{'*' * 75}\nStage 2 Prompting:\n{'*' * 75}")
stage2_user_msg = open(stage2_sys_prompt_file, 'r').read().replace(
"<obj_set>", str(all_objects))
stage2_prompt = f"{stage2_user_msg}\n\nFormat your output as a JSON dictionary."
# NOTE: we will be selecting a random example from a JSON file containing examples:
# -- sort examples in descending order of scoring and add them to the prompt:
sorted_examples = top_fewshot_examples(
openai_obj,
fewshot_examples,
query,
verbose=verbose
)
for x in range(num_fewshot): # NOTE: change the argument for more few-shot examples
stage2_prompt = f"{stage2_prompt}\n\nExample #{x+1}:\n{json.dumps(sorted_examples[x], indent=4)}"
if verbose:
print(stage2_prompt)
interaction.extend([
{"role": "assistant", "content": stage1b_response},
{"role": "user", "content": stage2_prompt}
])
# -- this is to make sure we continue prompting in the case where the response from the LLM was cut off:
entire_response = []
while True:
_, stage2_response = openai_obj.prompt(
interaction,
verbose=verbose)
interaction.extend([{"role": "assistant", "content": stage2_response}])
entire_response.append(stage2_response)
try:
# -- use eval() function to parse through the Stage 2 prompt response obtained from LLM:
object_level_plan = eval(parse_llm_code("".join(entire_response)))
except SyntaxError as err:
print(f"--warning: EOL (overflow): {err.msg}")
else: break
assert bool(object_level_plan), "Something went wrong here?"
if verbose:
print(f"{'*' * 75}\nStage 2 Response:\n{stage2_response}\n{'*' * 75}")
print(f" -- total number of tokens: {openai_obj.num_tokens(stage2_response)}")
######################################################################################
# NOTE: Stage 3 prompting:
# -- this involves asking the LLM about the key step(s) that
# will represent the final state for the entire object-level plan
######################################################################################
if verbose:
print(f"{'*' * 75}\nStage 3 Prompting:\n{'*' * 75}")
stage3_prompt = open(stage3_sys_prompt_file, 'r').read()
interaction.extend([{"role": "user", "content": stage3_prompt}])
_, stage3_response = openai_obj.prompt(interaction, verbose=verbose)
if verbose:
print(f"{'*' * 75}\nStage 3 Response:\n{stage3_response}\n{'*' * 75}")
print(f" -- total number of tokens: {openai_obj.num_tokens(stage3_response)}")
stage3_terminalSteps = eval(re.findall(r'\[.+?\]', stage3_response)[0])
######################################################################################
# NOTE: Stage 4 prompting:
# -- this involves getting a state summary dictionary
# for archiving the generated OLP and making it easy to retrieve from cache
######################################################################################
if verbose:
print(f"{'*' * 75}\nStage 4 Prompting:\n{'*' * 75}")
stage4_prompt = open(stage4_sys_prompt_file, 'r').read()
interaction.extend([
{"role": "assistant", "content": stage3_response},
{"role": "user", "content": stage4_prompt},
])
_, stage4_response = openai_obj.prompt(interaction, verbose=verbose)
if verbose:
print(f"{'*' * 75}\nStage 4 Response:\n{stage4_response}\n{'*' * 75}")
interaction.extend([{"role": "assistant", "content": stage4_response}, ])
language_plan = []
for action in object_level_plan:
language_plan.append(f"{action['step']}. {action['instruction']}")
# -- returning parsed content as well as chat history for further interaction (and testing):
llm_output = {
'task_prompt': query,
'all_objects': all_objects,
'language_plan': language_plan,
'plan': object_level_plan,
'final_state': eval(parse_llm_code(stage4_response)),
'termination_steps': stage3_terminalSteps,
}
return llm_output, interaction
def repair_olp(
openai_obj: Type[OpenAIInterfacer],
query: str,
available_objects: list,
embedding_fpath: str = 'olp_embeddings.pkl',
threshold: float = 0.8,
skip_feedback: bool = True,
verbose: bool = True,
) -> dict:
"""
This function reviews all embedded object-level plans (if any exist) and modifies the closest one
to achieve the user-specified task while also accounting for the objects in the scene.
:param openai_obj: OpenAIInterfacer object for interacting with GPT
:param query: a string containing the task prompt given by a human user
:param available_objects: a list of strings referring to objects from the scene
:param embedding_fpath: a string referring to the path of the embedding file
:param threshold: a float value referring to the percentage similarity needed (between 0 and 1.0)
:param verbose: a boolean value to set verbose comments
:return -1: insufficient object set (missing objects prevent plan completion)
:return 0: no cached plans are available for modification