-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathAndyTools.py
82 lines (75 loc) · 3.35 KB
/
AndyTools.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
from autogen import GroupChatManager, GroupChat
from dataclasses import dataclass
import sys
from typing import Dict, List, Optional, Union
from autogen import Agent
from autogen import ConversableAgent
# make the selector which selects the speaker the Judge every time
class ManualManager(GroupChatManager):
def run_andy_chat(
self,
messages: Optional[List[Dict]] = None,
sender: Optional[Agent] = None,
config: Optional[GroupChat] = None,
) -> Union[str, Dict, None]:
"""Run a group chat."""
if messages is None:
messages = self._oai_messages[sender]
message = messages[-1]
speaker = sender
groupchat = config
for i in range(groupchat.max_round):
# set the name to speaker's name if the role is not function
if message["role"] != "function":
message["name"] = speaker.name
groupchat.messages.append(message)
# broadcast the message to all agents except the speaker
for agent in groupchat.agents:
if agent != speaker:
self.send(message, agent, request_reply=False, silent=True)
if i == groupchat.max_round - 1:
# the last round
break
try:
# select the next speaker
speaker = groupchat.select_speaker(speaker, self, groupchat.agent_by_name("Manager"))
# let the speaker speak
reply = speaker.generate_reply(sender=self)
except KeyboardInterrupt:
# let the admin agent speak if interrupted
if groupchat.admin_name in groupchat.agent_names:
# admin agent is one of the participants
speaker = groupchat.agent_by_name(groupchat.admin_name)
reply = speaker.generate_reply(sender=self)
else:
# admin agent is not found in the participants
raise
if reply is None:
break
# The speaker sends the message without requesting a reply
speaker.send(reply, self, request_reply=False)
message = self.last_message(speaker)
return True, None
# Make the select_speaker call handle Andy the Judge making the call opposed to an openAI call
class ManualGroupChat(GroupChat):
def select_speaker(self, last_speaker: Agent, selector: ConversableAgent):
"""Select the next speaker."""
# selector.update_system_message(self.select_speaker_msg())
# final, name = selector.generate_oai_reply(
# self.messages
# + [
# {
# "role": "system",
# "content": f"Read the above conversation. Then select the next role from {self.agent_names} to play. Only return the role.",
# }
# ]
# )
final = True
name = input("Who speaks next:")
if not final:
# i = self._random.randint(0, len(self._agent_names) - 1) # randomly pick an id
return self.next_agent(last_speaker)
try:
return self.agent_by_name(name)
except ValueError:
return self.next_agent(last_speaker)