-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
77 lines (55 loc) · 2.68 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
import asyncio
import threading
from typing import List
from async_gpt import AsyncGPTAgent, AsyncAgentWrapper
from simulated.rooms import Room, Environment, TownEnvironment
from utils import debug_log, await_text_input, threadsafe_start_pygame_loop
def build_env():
town_env = TownEnvironment('Townton', ['Inn', 'Market'])
Room('Winery', town_env, connect_to={town_env.default_room, Room('Cellar', town_env)})
Room('Palace', town_env, connect_to={town_env.default_room, Room('Throne Room', town_env), Room('Dungeon', town_env)})
return town_env
def build_agents(env):
agents = [
AsyncGPTAgent('Alice', 'villager', 'have a 1-on-1 conversation with each resident of Townton', env, {'villager'}),
AsyncGPTAgent('Bob', 'villager', 'direct everyone in Townton to go to the throne room within the palace', env, {'villager'}),
AsyncGPTAgent('Eve', 'merchant', 'recruit someone in Townton to stay at the market', env, {'villager'}),
]
for agent in agents:
agent.add_message("Messages other people send you look like this: \n" +
"Person: This is a message!" +
" (Use the `say` tool to respond to these messages.)")
agent.add_message(f"You're currently in the {agent.simulation_agent.current_room.name}, but you can move to these: {', '.join(room.name for room in agent.simulation_agent.current_room.adjacent_rooms)}" +
" (Use the `move` tool to move around.)")
return agents
async def main():
data_queue = asyncio.Queue()
render_event = asyncio.Event()
update_agents_event = asyncio.Event()
update_rooms_event = asyncio.Event()
stop_event = asyncio.Event()
env = build_env()
agents = build_agents(env)
pygame_thread = threading.Thread(target=threadsafe_start_pygame_loop, args=(env, data_queue, render_event, update_agents_event, update_rooms_event, stop_event), daemon=True)
pygame_thread.start()
try:
# agents[0].simulation_agent.move_to_room('Inn')
# new = AsyncGPTAgent('New', '', '', env)
# update_agents_event.set()
render_event.set()
await asyncio.sleep(0.5)
input_ = None
while input_ != 'q':
pre_updates = [agent.update_pre() for agent in agents]
_ = await asyncio.gather(*pre_updates)
updates = [agent.update() for agent in agents]
_ = await asyncio.gather(*updates)
render_event.set()
await asyncio.sleep(0.5)
# print("Press Enter to continue...")
# input_ = await await_text_input()
finally:
stop_event.set()
pygame_thread.join(5.0)
if __name__ == '__main__':
asyncio.run(main())