forked from MineDojo/Voyager
-
Notifications
You must be signed in to change notification settings - Fork 2
/
chat_agent.py
112 lines (90 loc) · 3.13 KB
/
chat_agent.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
import openai
import time
from MultiVoyager import MultiVoyager
import argparse
from openai import OpenAI
client = OpenAI()
key_id = 0
def chat_llm(history, temperature=0, max_tokens=100, model='gpt-4', context=''):
# history = [('user', context)] + history
chat_history = []
for i in history:
if i[0] == 'user':
chat_history.append({
'role': 'user',
'content': i[1]
})
elif i[0] == 'assistant':
chat_history.append({
'role': 'assistant',
'content': i[1]
})
elif i[0] == 'system':
chat_history.append({
'role': 'system',
'content': i[1]
})
else:
raise NotImplementedError
# openai.organization = 'org-m2iXhDFphTS3ttoq3L6gNNA0'
total_trials = 0
while True:
try:
response = client.chat.completions.create(
model = model,
messages=chat_history,
temperature=temperature,
max_tokens=max_tokens
)
break
except openai.OpenAIError as e:
# print(e)
total_trials += 1
time.sleep(0.1)
return response.choices[0].message.content
def next_key():
global key_id
with open('./key.txt', 'r') as f:
all_keys = f.read().split('\n')
# all_keys = open('./key.txt', 'r').read().split('\n')
num = len(all_keys)
key_id += 1
if key_id >= num:
key_id -= num
openai.api_key = all_keys[key_id%num] #random.choice(all_keys)
if __name__ == '__main__':
args_parser = argparse.ArgumentParser(description='')
args_parser.add_argument('--port', type=int, required=True)
args = args_parser.parse_args()
port = str(args.port)
env = MultiVoyager(port, 'sk-x')
asset_file = './multi_voyager/prompt/prompt.txt'
example = open(asset_file, 'r').read().split('***\n')
example_history = []
message = """
You are allocating two agents to playing a cooking game in Minecraft. Always adhere to the following guidelines:
1. Prioritize current dishes.
2. Prioritize efficiency.
"""
example_history.append(("system", message))
for idx, exp in enumerate(example):
if idx % 2 == 0:
example_history.append(("user", exp))
else:
example_history.append(("assistant", exp))
interaction_history = []
for _ in range(60):
prompt = env.all_state()
interaction_history.append(("user", prompt))
if len(interaction_history) > 5:
interaction_history = interaction_history[2:]
print(prompt)
full_prompt = example_history + interaction_history
plan = chat_llm(full_prompt, temperature=0.0, max_tokens=100, model='gpt-4', context='')
print(plan)
example_history.append(("assistant", plan))
interaction_history.append(("assistant", plan))
plan = eval(plan)
env.step(plan)
print('accomplished tasks: ', env.accomplished_goals)
print('failed tasks: ', env.failed_goals)