-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_agent.py
60 lines (46 loc) · 1.61 KB
/
create_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
import argparse
import asyncio
import zmq
import zmq.asyncio
from agent import Agent
async def listen_for_messages(agent):
while True:
message = await agent.dealer.recv_string()
await agent.handle_message(
message
) # TODO: If you use asyncio.create_task here and not synchronously handle the message queue, many race conditions will cause duplicate work.
async def main(args):
context = zmq.asyncio.Context()
dealer = context.socket(zmq.DEALER)
dealer.identity = str(args.agent_id).encode()
dealer.connect("tcp://localhost:5555")
print(f"Agent {args.agent_id} is connected")
agent = Agent(
name=args.agent_id, prompt=args.prompt, dealer=dealer, superior=args.superior
)
# Send the initial message based on the command line argument
if args.task:
await dealer.send_multipart([dealer.identity, args.task.encode()])
# Start the listening task
await listen_for_messages(agent)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Run an agent subprocess with a specific ID, prompt, and task."
)
parser.add_argument("agent_id", type=str, help="Agent ID")
parser.add_argument(
"prompt", type=str, help="System message to prompt the agent with"
)
parser.add_argument(
"task",
type=str,
nargs="?",
help="Initial message to send to the agent containing the task",
)
parser.add_argument(
"superior",
type=str,
help="Name of the superior agent/user",
)
args = parser.parse_args()
asyncio.run(main(args))