-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (62 loc) · 3.2 KB
/
main.py
File metadata and controls
69 lines (62 loc) · 3.2 KB
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
from agent import *
import logging
import re
def init_logger():
logger = logging.getLogger()
formatter = logging.Formatter('%(asctime)s: %(message)s')
f_handler = logging.FileHandler('log.txt')
f_handler.setFormatter(formatter)
logger.addHandler(f_handler)
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
logger.setLevel(logging.INFO)
if __name__ == "__main__":
delete_all_files_in_folder("logs")
delete_all_files_in_folder("files")
git_commit("Initial commit")
if os.path.exists('log.txt'):
os.remove('log.txt')
init_logger()
begin_time = time.time()
meta_output = get_llm_response([{"role": "system", "content": config.initial_prompt}], False)['choices'][0]['message']['content']
logging.info(meta_output)
agent_pattern = re.compile(r'<agent name="(\w+)">(.*?)</agent>', re.DOTALL)
agents = agent_pattern.findall(meta_output)
for agent in agents:
if agent[0] == config.ceo_name:
agent_dict[agent[0]] = Agent(agent[0], agent[1])
for agent in agents:
if agent[0] != config.ceo_name:
agent_dict[config.ceo_name].add_subordinate(agent[0], "", agent[1])
agent_dict[config.ceo_name].enqueue("user","Now let's start the project. Please split the task and talk to your subordinates to assign the tasks.")
# Wait until all agents become idle
while True:
time.sleep(1)
if all(agent.state == "idle" for agent in agent_dict.values()):
ok = True
for agent in agent_dict.values():
try:
with git_lock:
f = open(f"files/todo_{agent.name}.txt", 'r')
content = f.read()
f.close()
except FileNotFoundError:
content = ''
if content != '':
agent.enqueue("system","Other agents have terminated. However, you still have unfinished tasks in your TODO list. Please finish them and clear it. If you are waiting for someone, chances are that they have forgotten about you. Please remind them.")
ok = False
if not ok:
continue
else:
agent_dict[config.ceo_name].enqueue("system",r"All the agents have terminated. Please use read_file to browse and proofread all the output files. Be sure to test them if needed, and check whether the project has been completed(do not leave placeholders!). If the project is completed with 100% accuracy, please call the 'terminate' function; if not, please assign the remaining tasks.")
while(agent_dict[config.ceo_name].state != "idle"):
time.sleep(1)
if all(agent.state == "idle" for agent in agent_dict.values()):
break
else:
continue
end_time = time.time()
logging.info(f"Time elapsed: {end_time-begin_time} seconds")
logging.info(f"Input tokens: {input_token}, output tokens: {output_token}")
logging.info(f"Number of agents: {len(agent_dict)}")