forked from humboldt123/reproducing-consensus-llm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (50 loc) · 2.33 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
from openai import OpenAI
import asyncio
import random
import json
from agents import *
import prompts
async def main():
agents = []
rounds = 10
# we create our list of agents and add them to the list
for i in range(3):
# 1d --> 2d
agents.append(Agent2D(i, [random.randint(0, 100), random.randint(0, 100)]))
# todo: prettyprint!
# todo: better err. handling
for r in range(rounds):
if not all(agent.pos == agents[0].pos for agent in agents):
print("===ROUND {} ===".format(r))
coroutines = []
for agent in agents:
# give our agent game description on the first round
# and the updated text after that
# two_dimensional -> one_dimensional for 1d arrays
message = prompts.two_dimensional.round_description if r > 0 else prompts.two_dimensional.game_description
props = message.format(
agent.pos, "[{}]".format(
", ".join(map(lambda a : str(a.pos), filter(lambda a: a.identifier != agent.identifier, agents)))
)
)
print("---------") # debug line
print("AGENT", agent.identifier) # debug line
# you guessed it (debug)
print("Position: {}\nPeers: {}".format(agent.pos, "[{}]".format(", ".join(map(lambda a : str(a.pos), filter(lambda a: a.identifier != agent.identifier, agents))))))
# ask agent where to move (coroutine)
coroutines.append(agent.prompt(props + " " + prompts.agent_output_form))
print(agent.latest) # debug line
print("---------\n") # debug line
try:
# wait for coroutines to finish
await asyncio.gather(*coroutines)
# update each agents location!
list(map(lambda agent: agent.update(), agents))
except:
print("Error in an agent's response format or failed to move agent!")
else:
print("Consensus reached on round {}", r - 1)
for agent in agents:
print("{}: {}".format(agent.identifier, agent.position_history))
if __name__ == '__main__':
asyncio.run(main())