-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
237 lines (221 loc) · 5.7 KB
/
main.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
from collections import defaultdict
from managers import ChatEnvironment, LLM, Assistant, Agent, FakeLLM
from NICOL.nicol_env import NICOLKnockingEnv
import argparse
import wandb
import numpy as np
import yaml
def log(prompt):
print("=" * 80)
print(prompt)
print("=" * 80)
return
parser = argparse.ArgumentParser()
parser.add_argument(
"-a",
"--sound_use_adjective",
action="store_true",
help="Use adjective instead of specific material feedback.",
)
parser.add_argument(
# "-g", "--engine", default="text-davinci-003", help="OpenAI LLM engines."
"-g",
"--engine",
default="Vicuna-13b",
help="OpenAI LLM engines."
# 'text-davinci-003', 'text-curie-001', 'text-babbage-001', "text-ada-001"
)
parser.add_argument(
"-o",
"--render_mode",
default="None",
help="Render mode. Optional: human, None, ...",
)
parser.add_argument("-e", "--episodes", default=30, type=int, help="Episodes to run.")
parser.add_argument(
"-r", "--rounds", default=15, type=int, help="Maximum rounds in every episode."
)
parser.add_argument(
"-m",
"--mode",
default="test",
help="train or test, the source that sound comes from.",
)
parser.add_argument(
"-d",
"--debug",
action="store_true",
help="Whether to set everything fixed for debugging.",
)
parser.add_argument(
"-l",
"--headless",
action="store_true",
help="Whether to run CoppeliaSim in headless mode, i.e. without GUI.",
)
parser.add_argument(
"-t",
"--max_tokens",
default=128,
help="Max tokens allowed for OpenAI GPT generations.",
)
parser.add_argument(
"-T",
"--temperature",
default=0.7,
help="Max tokens allowed for OpenAI GPT generations.",
)
parser.add_argument(
"-p",
"--plot_on",
action="store_true",
help="Plot on to show detections.",
)
parser.add_argument(
"-w",
"--use_wandb",
action="store_true",
help="Whether use wandb.ai to record.",
)
parser.add_argument(
"-f",
"--fake_llm",
action="store_true",
help="Whether use a fake (local designed for debugging) LLM.",
)
parser.add_argument(
"-s",
"--seed",
default=-1,
help="Seed used for randomizations.",
)
parser.add_argument(
"--prompt_path",
default="./prompts.txt",
help="Few-shot prompts for in-context learning.",
)
args = parser.parse_args()
with open("config.yml", "r") as f:
default_configs = yaml.safe_load(f)
for k, v in default_configs.items():
setattr(args, k, v)
args.openai_api_base = args.engines[args.engine]["openai_api_base"]
args.openai_api_key = args.engines[args.engine]["openai_api_key"]
print(args)
run_name = (
f"{args.engine}_adj" if args.sound_use_adjective else f"{args.engine}_material"
)
args.use_wandb and wandb.init(project="chatenv", config=args, name=run_name)
environment = ChatEnvironment(
env_cls=NICOLKnockingEnv,
mode=args.mode,
headless=args.headless,
debug=args.debug,
render_mode=args.render_mode,
)
if args.fake_llm:
LLM = FakeLLM
llm = LLM(
engine=args.engine,
openai_api_base=args.openai_api_base,
openai_api_key=args.openai_api_key,
prompt_path=args.prompt_path,
max_tokens=int(args.max_tokens),
temperature=int(args.temperature),
)
assistant = Assistant(sound_use_adjective=args.sound_use_adjective)
agent = Agent(assistant)
def reset_everything():
environment.reset()
llm.reset()
agent.reset()
vision = environment.render()
prompt = ""
command = ""
caption = agent.vision(vision, plot_on=args.plot_on)
instruction = environment.instruct()
description = environment.instruct_with_caption(caption)
skills = defaultdict(int)
rewards = 0
rud = 0
return (
vision,
prompt,
command,
description,
instruction,
skills,
rewards,
False,
rud,
)
table_columns = [
"Episode",
"Vision",
"Instruction",
"Round",
"Diversity",
"Invalid",
"Reward",
"Conversation",
"Explanation",
]
if args.use_wandb:
major_table = wandb.Table(columns=table_columns)
for episode in range(args.episodes):
if int(args.seed) == -1:
if args.sound_use_adjective:
seed = episode + 1000
else:
seed = episode
else:
seed = args.seed
np.random.seed(seed)
(
vision,
prompt,
command,
description,
instruction,
skills,
rewards,
done,
rud,
) = reset_everything()
while True:
prompt = prompt + command + description
command = llm.feed(prompt)
print(command)
description, explaination, skill, reward, done = agent.execute(
environment, command
)
print(description)
skills[skill] += 1
rewards += reward
rud += 1
if done or rud >= args.rounds:
prompt = prompt + command + description
log(prompt)
explaination = llm.feed(prompt)
log(explaination)
prompt = prompt + explaination + "done()"
if args.use_wandb:
table_data = (
episode,
wandb.Image(vision),
instruction,
rud,
len(skills),
agent.invalid_count,
rewards,
prompt,
explaination,
)
table = wandb.Table(columns=table_columns)
table.add_data(*table_data)
major_table.add_data(*table_data)
wandb.log({f"ChatEnv {episode}": table})
break
if args.use_wandb:
wandb.log({f"ChatEnv Summary": major_table})
wandb.finish()