From d6a4bd76be7737e073d62df03e42a886a40b0867 Mon Sep 17 00:00:00 2001 From: Rajan Date: Tue, 21 May 2024 15:46:10 -0400 Subject: [PATCH] [CAP] Refactor: Better Names for classes and methods (#2734) * Bug fix * Refactor: Better class names, method names * pypi version * pre-commit fixes --- samples/apps/cap/py/autogencap/Actor.py | 12 +++---- .../apps/cap/py/autogencap/ActorConnector.py | 4 +-- ...alActorNetwork.py => ComponentEnsemble.py} | 16 ++++----- samples/apps/cap/py/autogencap/DebugLog.py | 2 +- .../apps/cap/py/autogencap/DirectorySvc.py | 20 +++++------ .../cap/py/autogencap/ag_adapter/AG2CAP.py | 10 +++--- .../autogencap/ag_adapter/AutoGenConnector.py | 2 +- .../cap/py/autogencap/ag_adapter/CAP2AG.py | 34 +++++++++---------- .../py/autogencap/ag_adapter/CAPGroupChat.py | 6 ++-- .../ag_adapter/CAPGroupChatManager.py | 12 +++---- .../cap/py/autogencap/ag_adapter/CAPPair.py | 2 +- samples/apps/cap/py/demo/AppAgents.py | 16 ++++----- .../apps/cap/py/demo/CAPAutGenGroupDemo.py | 10 +++--- .../apps/cap/py/demo/CAPAutoGenPairDemo.py | 8 ++--- samples/apps/cap/py/demo/ComplexActorDemo.py | 20 +++++------ samples/apps/cap/py/demo/SimpleActorDemo.py | 31 ++++++++--------- samples/apps/cap/py/demo/list_agents.py | 14 ++++---- .../apps/cap/py/demo/standalone/Assistant.py | 10 +++--- .../apps/cap/py/demo/standalone/UserProxy.py | 12 +++---- .../apps/cap/py/demo/standalone/user_proxy.py | 14 ++++---- samples/apps/cap/py/pyproject.toml | 2 +- 21 files changed, 128 insertions(+), 129 deletions(-) rename samples/apps/cap/py/autogencap/{LocalActorNetwork.py => ComponentEnsemble.py} (87%) diff --git a/samples/apps/cap/py/autogencap/Actor.py b/samples/apps/cap/py/autogencap/Actor.py index 7057ddc81e47..87c3d7ea8c2e 100644 --- a/samples/apps/cap/py/autogencap/Actor.py +++ b/samples/apps/cap/py/autogencap/Actor.py @@ -15,16 +15,16 @@ def __init__(self, agent_name: str, description: str): self.run = False self._start_event = threading.Event() - def connect_network(self, network): + def on_connect(self, network): Debug(self.actor_name, f"is connecting to {network}") Debug(self.actor_name, "connected") - def _process_txt_msg(self, msg: str, msg_type: str, topic: str, sender: str) -> bool: + def on_txt_msg(self, msg: str, msg_type: str, receiver: str, sender: str) -> bool: Info(self.actor_name, f"InBox: {msg}") return True - def _process_bin_msg(self, msg: bytes, msg_type: str, topic: str, sender: str) -> bool: - Info(self.actor_name, f"Msg: topic=[{topic}], msg_type=[{msg_type}]") + def on_bin_msg(self, msg: bytes, msg_type: str, receiver: str, sender: str) -> bool: + Info(self.actor_name, f"Msg: receiver=[{receiver}], msg_type=[{msg_type}]") return True def _recv_thread(self): @@ -51,12 +51,12 @@ def _recv_thread(self): continue if msg_type == "text": msg = msg.decode("utf-8") # Convert bytes to string - if not self._process_txt_msg(msg, msg_type, topic, sender_topic): + if not self.on_txt_msg(msg, msg_type, topic, sender_topic): msg = "quit" if msg.lower() == "quit": break else: - if not self._process_bin_msg(msg, msg_type, topic, sender_topic): + if not self.on_bin_msg(msg, msg_type, topic, sender_topic): break except Exception as e: Debug(self.actor_name, f"recv thread encountered an error: {e}") diff --git a/samples/apps/cap/py/autogencap/ActorConnector.py b/samples/apps/cap/py/autogencap/ActorConnector.py index e2ddbfa4fb69..588d4c463b62 100644 --- a/samples/apps/cap/py/autogencap/ActorConnector.py +++ b/samples/apps/cap/py/autogencap/ActorConnector.py @@ -120,7 +120,7 @@ def send_txt_msg(self, msg): def send_bin_msg(self, msg_type: str, msg): self._sender.send_bin_msg(msg_type, msg) - def binary_request(self, msg_type: str, msg, num_attempts=5): + def send_recv_msg(self, msg_type: str, msg, num_attempts=5): original_timeout: int = 0 if num_attempts == -1: original_timeout = self._resp_socket.getsockopt(zmq.RCVTIMEO) @@ -148,5 +148,5 @@ def binary_request(self, msg_type: str, msg, num_attempts=5): return None, None, None def close(self): - self._pub_socket.close() + self._sender.close() self._resp_socket.close() diff --git a/samples/apps/cap/py/autogencap/LocalActorNetwork.py b/samples/apps/cap/py/autogencap/ComponentEnsemble.py similarity index 87% rename from samples/apps/cap/py/autogencap/LocalActorNetwork.py rename to samples/apps/cap/py/autogencap/ComponentEnsemble.py index 336555368356..3c726bcb2d17 100644 --- a/samples/apps/cap/py/autogencap/LocalActorNetwork.py +++ b/samples/apps/cap/py/autogencap/ComponentEnsemble.py @@ -14,7 +14,7 @@ # TODO: remove time import -class LocalActorNetwork: +class ComponentEnsemble: def __init__(self, name: str = "Local Actor Network", start_broker: bool = True): self.local_actors = {} self.name: str = name @@ -49,7 +49,7 @@ def register(self, actor: Actor): def connect(self): self._init_runtime() for actor in self.local_actors.values(): - actor.connect_network(self) + actor.on_connect(self) def disconnect(self): for actor in self.local_actors.values(): @@ -59,22 +59,22 @@ def disconnect(self): if self._broker: self._broker.stop() - def actor_connector_by_topic(self, topic: str) -> ActorConnector: + def find_by_topic(self, topic: str) -> ActorConnector: return ActorConnector(self._context, topic) - def lookup_actor(self, name: str) -> ActorConnector: + def find_by_name(self, name: str) -> ActorConnector: actor_info: ActorInfo = self._directory_svc.lookup_actor_by_name(name) if actor_info is None: Warn("Local_Actor_Network", f"{name}, not found in the network.") return None Debug("Local_Actor_Network", f"[{name}] found in the network.") - return self.actor_connector_by_topic(name) + return self.find_by_topic(name) - def lookup_termination(self) -> ActorConnector: + def find_termination(self) -> ActorConnector: termination_topic: str = Termination_Topic - return self.actor_connector_by_topic(termination_topic) + return self.find_by_topic(termination_topic) - def lookup_actor_info(self, name_regex) -> List[ActorInfo]: + def find_by_name_regex(self, name_regex) -> List[ActorInfo]: actor_info: ActorInfoCollection = self._directory_svc.lookup_actor_info_by_name(name_regex) if actor_info is None: Warn("Local_Actor_Network", f"{name_regex}, not found in the network.") diff --git a/samples/apps/cap/py/autogencap/DebugLog.py b/samples/apps/cap/py/autogencap/DebugLog.py index d3be81fe24e6..c3d6ca421276 100644 --- a/samples/apps/cap/py/autogencap/DebugLog.py +++ b/samples/apps/cap/py/autogencap/DebugLog.py @@ -38,7 +38,7 @@ def __init__(self): super().__init__() def WriteLog(self, level, context, msg): - timestamp = colored(datetime.datetime.now().strftime("%m/%d/%y %H:%M:%S"), "pink") + timestamp = colored(datetime.datetime.now().strftime("%m/%d/%y %H:%M:%S"), "dark_grey") # Translate level number to name and color level_name = colored(LEVEL_NAMES[level], LEVEL_COLOR[level]) # Left justify the context and color it blue diff --git a/samples/apps/cap/py/autogencap/DirectorySvc.py b/samples/apps/cap/py/autogencap/DirectorySvc.py index edd7506ba3b7..66ff07f5f03b 100644 --- a/samples/apps/cap/py/autogencap/DirectorySvc.py +++ b/samples/apps/cap/py/autogencap/DirectorySvc.py @@ -35,25 +35,25 @@ def __init__(self, topic: str, name: str): self._registered_actors = {} self._network_prefix = "" - def _process_bin_msg(self, msg: bytes, msg_type: str, topic: str, sender: str) -> bool: + def on_bin_msg(self, msg: bytes, msg_type: str, topic: str, sender: str) -> bool: if msg_type == ActorRegistration.__name__: - self._actor_registration_msg_handler(topic, msg_type, msg, sender) + self._on_actor_registration_msg(topic, msg_type, msg, sender) elif msg_type == ActorLookup.__name__: - self._actor_lookup_msg_handler(topic, msg_type, msg, sender) + self._on_actor_lookup_msg(topic, msg_type, msg, sender) elif msg_type == Ping.__name__: - self._ping_msg_handler(topic, msg_type, msg, sender) + self._on_ping_msg(topic, msg_type, msg, sender) else: Error("DirectorySvc", f"Unknown message type: {msg_type}") return True - def _ping_msg_handler(self, topic: str, msg_type: str, msg: bytes, sender_topic: str): + def _on_ping_msg(self, topic: str, msg_type: str, msg: bytes, sender_topic: str): Info("DirectorySvc", f"Ping received: {sender_topic}") pong = Pong() serialized_msg = pong.SerializeToString() sender_connection = ActorSender(self._context, sender_topic) sender_connection.send_bin_msg(Pong.__name__, serialized_msg) - def _actor_registration_msg_handler(self, topic: str, msg_type: str, msg: bytes, sender_topic: str): + def _on_actor_registration_msg(self, topic: str, msg_type: str, msg: bytes, sender_topic: str): actor_reg = ActorRegistration() actor_reg.ParseFromString(msg) Info("DirectorySvc", f"Actor registration: {actor_reg.actor_info.name}") @@ -71,7 +71,7 @@ def _actor_registration_msg_handler(self, topic: str, msg_type: str, msg: bytes, serialized_msg = err.SerializeToString() sender_connection.send_bin_msg(ErrorMsg.__name__, serialized_msg) - def _actor_lookup_msg_handler(self, topic: str, msg_type: str, msg: bytes, sender_topic: str): + def _on_actor_lookup_msg(self, topic: str, msg_type: str, msg: bytes, sender_topic: str): actor_lookup = ActorLookup() actor_lookup.ParseFromString(msg) Debug("DirectorySvc", f"Actor lookup: {actor_lookup.actor_info.name}") @@ -111,7 +111,7 @@ def _no_other_directory(self) -> bool: Debug("DirectorySvc", "Pinging existing DirectorySvc") ping = Ping() serialized_msg = ping.SerializeToString() - _, _, resp = self._directory_connector.binary_request(Ping.__name__, serialized_msg, retry=1) + _, _, resp = self._directory_connector.send_recv_msg(Ping.__name__, serialized_msg, num_attempts=1) if resp is None: return True return False @@ -138,7 +138,7 @@ def register_actor(self, actor_info: ActorInfo): actor_reg = ActorRegistration() actor_reg.actor_info.CopyFrom(actor_info) serialized_msg = actor_reg.SerializeToString() - _, _, resp = self._directory_connector.binary_request(ActorRegistration.__name__, serialized_msg) + _, _, resp = self._directory_connector.send_recv_msg(ActorRegistration.__name__, serialized_msg) report_error_msg(resp, "DirectorySvc") def register_actor_by_name(self, actor_name: str): @@ -149,7 +149,7 @@ def _lookup_actors_by_name(self, name_regex: str): actor_info = ActorInfo(name=name_regex) actor_lookup = ActorLookup(actor_info=actor_info) serialized_msg = actor_lookup.SerializeToString() - _, _, resp = self._directory_connector.binary_request(ActorLookup.__name__, serialized_msg) + _, _, resp = self._directory_connector.send_recv_msg(ActorLookup.__name__, serialized_msg) actor_lookup_resp = ActorLookupResponse() actor_lookup_resp.ParseFromString(resp) return actor_lookup_resp diff --git a/samples/apps/cap/py/autogencap/ag_adapter/AG2CAP.py b/samples/apps/cap/py/autogencap/ag_adapter/AG2CAP.py index 5ce12a538cd0..1854d219e7d1 100644 --- a/samples/apps/cap/py/autogencap/ag_adapter/AG2CAP.py +++ b/samples/apps/cap/py/autogencap/ag_adapter/AG2CAP.py @@ -3,7 +3,7 @@ from autogen import Agent, ConversableAgent -from ..LocalActorNetwork import LocalActorNetwork +from ..ComponentEnsemble import ComponentEnsemble from .AutoGenConnector import AutoGenConnector @@ -14,13 +14,13 @@ class AG2CAP(ConversableAgent): def __init__( self, - network: LocalActorNetwork, + ensemble: ComponentEnsemble, agent_name: str, agent_description: Optional[str] = None, ): super().__init__(name=agent_name, description=agent_description, llm_config=False) self._agent_connector: AutoGenConnector = None - self._network: LocalActorNetwork = network + self._ensemble: ComponentEnsemble = ensemble self._recv_called = False def reset_receive_called(self): @@ -38,8 +38,8 @@ def set_name(self, name: str): def _check_connection(self): if self._agent_connector is None: - self._agent_connector = AutoGenConnector(self._network.lookup_actor(self.name)) - self._terminate_connector = AutoGenConnector(self._network.lookup_termination()) + self._agent_connector = AutoGenConnector(self._ensemble.find_by_name(self.name)) + self._terminate_connector = AutoGenConnector(self._ensemble.find_termination()) def receive( self, diff --git a/samples/apps/cap/py/autogencap/ag_adapter/AutoGenConnector.py b/samples/apps/cap/py/autogencap/ag_adapter/AutoGenConnector.py index ce81e7e945d3..47b70ea7a130 100644 --- a/samples/apps/cap/py/autogencap/ag_adapter/AutoGenConnector.py +++ b/samples/apps/cap/py/autogencap/ag_adapter/AutoGenConnector.py @@ -38,7 +38,7 @@ def send_gen_reply_req(self): # Setting retry to -1 to keep trying until a response is received # This normal AutoGen behavior but does not handle the case when an AutoGen agent # is not running. In that case, the connector will keep trying indefinitely. - _, _, resp = self._can_channel.binary_request(type(msg).__name__, serialized_msg, num_attempts=-1) + _, _, resp = self._can_channel.send_recv_msg(type(msg).__name__, serialized_msg, num_attempts=-1) gen_reply_resp = GenReplyResp() gen_reply_resp.ParseFromString(resp) return gen_reply_resp.data diff --git a/samples/apps/cap/py/autogencap/ag_adapter/CAP2AG.py b/samples/apps/cap/py/autogencap/ag_adapter/CAP2AG.py index 25cd7093ba79..789ebd9bf4b8 100644 --- a/samples/apps/cap/py/autogencap/ag_adapter/CAP2AG.py +++ b/samples/apps/cap/py/autogencap/ag_adapter/CAP2AG.py @@ -4,8 +4,8 @@ from autogen import ConversableAgent +from ..ComponentEnsemble import ComponentEnsemble from ..DebugLog import Debug, Error, Info, Warn, shorten -from ..LocalActorNetwork import LocalActorNetwork from ..proto.Autogen_pb2 import GenReplyReq, GenReplyResp, PrepChat, ReceiveReq, Terminate from .AG2CAP import AG2CAP from .AGActor import AGActor @@ -27,26 +27,26 @@ def __init__(self, ag_agent: ConversableAgent, the_other_name: str, init_chat: b self.STATE = self.States.INIT self._can2ag_name: str = self.actor_name + ".can2ag" self._self_recursive: bool = self_recursive - self._network: LocalActorNetwork = None + self._ensemble: ComponentEnsemble = None self._connectors = {} - def connect_network(self, network: LocalActorNetwork): + def on_connect(self, ensemble: ComponentEnsemble): """ Connect to the AutoGen system. """ - self._network = network - self._ag2can_other_agent = AG2CAP(self._network, self._other_agent_name) - Debug(self._can2ag_name, "connected to {network}") + self._ensemble = ensemble + self._ag2can_other_agent = AG2CAP(self._ensemble, self._other_agent_name) + Debug(self._can2ag_name, "connected to {ensemble}") - def disconnect_network(self, network: LocalActorNetwork): + def disconnect_network(self, ensemble: ComponentEnsemble): """ Disconnect from the AutoGen system. """ - super().disconnect_network(network) + super().disconnect_network(ensemble) # self._the_other.close() Debug(self.actor_name, "disconnected") - def _process_txt_msg(self, msg: str, msg_type: str, topic: str, sender: str): + def on_txt_msg(self, msg: str, msg_type: str, topic: str, sender: str): """ Process a text message received from the AutoGen system. """ @@ -83,7 +83,7 @@ def _call_agent_receive(self, receive_params: ReceiveReq): self._the_ag_agent.receive(data, self._ag2can_other_agent, request_reply, silent) self._ag2can_other_agent.set_name(save_name) - def receive_msgproc(self, msg: bytes): + def on_receive_msg(self, msg: bytes): """ Process a ReceiveReq message received from the AutoGen system. """ @@ -117,11 +117,11 @@ def get_actor_connector(self, topic: str): if topic in self._connectors: return self._connectors[topic] else: - connector = self._network.actor_connector_by_topic(topic) + connector = self._ensemble.find_by_topic(topic) self._connectors[topic] = connector return connector - def generate_reply_msgproc(self, msg: GenReplyReq, sender_topic: str): + def on_generate_reply_msg(self, msg: GenReplyReq, sender_topic: str): """ Process a GenReplyReq message received from the AutoGen system and generate a reply. """ @@ -137,23 +137,23 @@ def generate_reply_msgproc(self, msg: GenReplyReq, sender_topic: str): connector.send_bin_msg(type(reply_msg).__name__, serialized_msg) return True - def prepchat_msgproc(self, msg, sender_topic): + def on_prepchat_msg(self, msg, sender_topic): prep_chat = PrepChat() prep_chat.ParseFromString(msg) self._the_ag_agent._prepare_chat(self._ag2can_other_agent, prep_chat.clear_history, prep_chat.prepare_recipient) return True - def _process_bin_msg(self, msg: bytes, msg_type: str, topic: str, sender: str): + def on_bin_msg(self, msg: bytes, msg_type: str, topic: str, sender: str): """ Process a binary message received from the AutoGen system. """ Info(self._can2ag_name, f"proc_bin_msg: topic=[{topic}], msg_type=[{msg_type}]") if msg_type == ReceiveReq.__name__: - return self.receive_msgproc(msg) + return self.on_receive_msg(msg) elif msg_type == GenReplyReq.__name__: - return self.generate_reply_msgproc(msg, sender) + return self.on_generate_reply_msg(msg, sender) elif msg_type == PrepChat.__name__: - return self.prepchat_msgproc(msg, sender) + return self.on_prepchat_msg(msg, sender) elif msg_type == Terminate.__name__: Warn(self._can2ag_name, f"TERMINATE received: topic=[{topic}], msg_type=[{msg_type}]") return False diff --git a/samples/apps/cap/py/autogencap/ag_adapter/CAPGroupChat.py b/samples/apps/cap/py/autogencap/ag_adapter/CAPGroupChat.py index 5d0a152a5d15..5fad7b359e16 100644 --- a/samples/apps/cap/py/autogencap/ag_adapter/CAPGroupChat.py +++ b/samples/apps/cap/py/autogencap/ag_adapter/CAPGroupChat.py @@ -3,7 +3,7 @@ from autogen import Agent, AssistantAgent, GroupChat from autogencap.ag_adapter.AG2CAP import AG2CAP from autogencap.ag_adapter.CAP2AG import CAP2AG -from autogencap.LocalActorNetwork import LocalActorNetwork +from autogencap.ComponentEnsemble import ComponentEnsemble class CAPGroupChat(GroupChat): @@ -13,10 +13,10 @@ def __init__( messages: List[str], max_round: int, chat_initiator: str, - network: LocalActorNetwork, + ensemble: ComponentEnsemble, ): self.chat_initiator: str = chat_initiator - self._cap_network: LocalActorNetwork = network + self._cap_network: ComponentEnsemble = ensemble self._cap_proxies: List[CAP2AG] = [] self._ag_proxies: List[AG2CAP] = [] self._ag_agents: List[Agent] = agents diff --git a/samples/apps/cap/py/autogencap/ag_adapter/CAPGroupChatManager.py b/samples/apps/cap/py/autogencap/ag_adapter/CAPGroupChatManager.py index 65823d75afd2..85a746d7c661 100644 --- a/samples/apps/cap/py/autogencap/ag_adapter/CAPGroupChatManager.py +++ b/samples/apps/cap/py/autogencap/ag_adapter/CAPGroupChatManager.py @@ -4,12 +4,12 @@ from autogencap.ActorConnector import ActorConnector from autogencap.ag_adapter.CAP2AG import CAP2AG from autogencap.ag_adapter.CAPGroupChat import CAPGroupChat -from autogencap.LocalActorNetwork import LocalActorNetwork +from autogencap.ComponentEnsemble import ComponentEnsemble class CAPGroupChatManager: - def __init__(self, groupchat: CAPGroupChat, llm_config: dict, network: LocalActorNetwork): - self._network: LocalActorNetwork = network + def __init__(self, groupchat: CAPGroupChat, llm_config: dict, network: ComponentEnsemble): + self._ensemble: ComponentEnsemble = network self._cap_group_chat: CAPGroupChat = groupchat self._ag_group_chat_manager: GroupChatManager = GroupChatManager( groupchat=self._cap_group_chat, llm_config=llm_config @@ -20,11 +20,11 @@ def __init__(self, groupchat: CAPGroupChat, llm_config: dict, network: LocalActo init_chat=False, self_recursive=True, ) - self._network.register(self._cap_proxy) + self._ensemble.register(self._cap_proxy) def initiate_chat(self, txt_msg: str) -> None: - self._network.connect() - user_proxy_conn: ActorConnector = self._network.lookup_actor(self._cap_group_chat.chat_initiator) + self._ensemble.connect() + user_proxy_conn: ActorConnector = self._ensemble.find_by_name(self._cap_group_chat.chat_initiator) user_proxy_conn.send_txt_msg(txt_msg) self._wait_for_user_exit() diff --git a/samples/apps/cap/py/autogencap/ag_adapter/CAPPair.py b/samples/apps/cap/py/autogencap/ag_adapter/CAPPair.py index 7cdeb12603f5..4a87a742b5dd 100644 --- a/samples/apps/cap/py/autogencap/ag_adapter/CAPPair.py +++ b/samples/apps/cap/py/autogencap/ag_adapter/CAPPair.py @@ -27,7 +27,7 @@ def initiate_chat(self, message: str): self._network.connect() # Send a message to the user_proxy - agent_connection = self._network.lookup_actor(self._first_ag_agent.name) + agent_connection = self._network.find_by_name(self._first_ag_agent.name) agent_connection.send_txt_msg(message) def running(self): diff --git a/samples/apps/cap/py/demo/AppAgents.py b/samples/apps/cap/py/demo/AppAgents.py index 1a035b751012..d2a11e4e3823 100644 --- a/samples/apps/cap/py/demo/AppAgents.py +++ b/samples/apps/cap/py/demo/AppAgents.py @@ -6,8 +6,8 @@ from autogencap.Actor import Actor from autogencap.ActorConnector import ActorConnector +from autogencap.ComponentEnsemble import ComponentEnsemble from autogencap.DebugLog import Debug, Info, shorten -from autogencap.LocalActorNetwork import LocalActorNetwork class GreeterAgent(Actor): @@ -135,7 +135,7 @@ def __init__( self.quant: ActorConnector = None self.risk_manager: ActorConnector = None - def connect_network(self, network: LocalActorNetwork): + def on_connect(self, network: ComponentEnsemble): """ Connects the personal assistant to the specified local actor network. @@ -143,13 +143,13 @@ def connect_network(self, network: LocalActorNetwork): network (LocalActorNetwork): The local actor network to connect to. """ Debug(self.actor_name, f"is connecting to {network}") - self.fidelity = network.lookup_actor("Fidelity") - self.financial_planner = network.lookup_actor("Financial Planner") - self.quant = network.lookup_actor("Quant") - self.risk_manager = network.lookup_actor("Risk Manager") + self.fidelity = network.find_by_name("Fidelity") + self.financial_planner = network.find_by_name("Financial Planner") + self.quant = network.find_by_name("Quant") + self.risk_manager = network.find_by_name("Risk Manager") Debug(self.actor_name, "connected") - def disconnect_network(self, network: LocalActorNetwork): + def disconnect_network(self, network: ComponentEnsemble): """ Disconnects the personal assistant from the specified local actor network. @@ -163,7 +163,7 @@ def disconnect_network(self, network: LocalActorNetwork): self.risk_manager.close() Debug(self.actor_name, "disconnected") - def _process_txt_msg(self, msg, msg_type, topic, sender): + def on_txt_msg(self, msg, msg_type, topic, sender): """ Processes a text message received by the personal assistant. diff --git a/samples/apps/cap/py/demo/CAPAutGenGroupDemo.py b/samples/apps/cap/py/demo/CAPAutGenGroupDemo.py index ec925086739a..4c3aa1b80305 100644 --- a/samples/apps/cap/py/demo/CAPAutGenGroupDemo.py +++ b/samples/apps/cap/py/demo/CAPAutGenGroupDemo.py @@ -1,7 +1,7 @@ from autogencap.ag_adapter.CAPGroupChat import CAPGroupChat from autogencap.ag_adapter.CAPGroupChatManager import CAPGroupChatManager +from autogencap.ComponentEnsemble import ComponentEnsemble from autogencap.DebugLog import Info -from autogencap.LocalActorNetwork import LocalActorNetwork from autogen import AssistantAgent, UserProxyAgent, config_list_from_json @@ -31,11 +31,11 @@ def cap_ag_group_demo(): system_message="Creative in software product ideas.", llm_config=gpt4_config, ) - network = LocalActorNetwork() + ensemble = ComponentEnsemble() cap_groupchat = CAPGroupChat( - agents=[user_proxy, coder, pm], messages=[], max_round=12, network=network, chat_initiator=user_proxy.name + agents=[user_proxy, coder, pm], messages=[], max_round=12, ensemble=ensemble, chat_initiator=user_proxy.name ) - manager = CAPGroupChatManager(groupchat=cap_groupchat, llm_config=gpt4_config, network=network) + manager = CAPGroupChatManager(groupchat=cap_groupchat, llm_config=gpt4_config, network=ensemble) manager.initiate_chat("Find a latest paper about gpt-4 on arxiv and find its potential applications in software.") - network.disconnect() + ensemble.disconnect() Info("App", "App Exit") diff --git a/samples/apps/cap/py/demo/CAPAutoGenPairDemo.py b/samples/apps/cap/py/demo/CAPAutoGenPairDemo.py index 84aad4c8f388..732bfecad17b 100644 --- a/samples/apps/cap/py/demo/CAPAutoGenPairDemo.py +++ b/samples/apps/cap/py/demo/CAPAutoGenPairDemo.py @@ -1,8 +1,8 @@ import time from autogencap.ag_adapter.CAPPair import CAPPair +from autogencap.ComponentEnsemble import ComponentEnsemble from autogencap.DebugLog import Info -from autogencap.LocalActorNetwork import LocalActorNetwork from autogen import AssistantAgent, UserProxyAgent, config_list_from_json @@ -17,9 +17,9 @@ def cap_ag_pair_demo(): ) # Composable Agent Platform AutoGen Pair adapter - network = LocalActorNetwork() + ensemble = ComponentEnsemble() - pair = CAPPair(network, user_proxy, assistant) + pair = CAPPair(ensemble, user_proxy, assistant) pair.initiate_chat("Plot a chart of MSFT daily closing prices for last 1 Month.") # Wait for the pair to finish @@ -31,5 +31,5 @@ def cap_ag_pair_demo(): except KeyboardInterrupt: print("Interrupted by user, shutting down.") - network.disconnect() + ensemble.disconnect() Info("App", "App Exit") diff --git a/samples/apps/cap/py/demo/ComplexActorDemo.py b/samples/apps/cap/py/demo/ComplexActorDemo.py index 29ce2b31d60d..6f6215daece8 100644 --- a/samples/apps/cap/py/demo/ComplexActorDemo.py +++ b/samples/apps/cap/py/demo/ComplexActorDemo.py @@ -1,7 +1,7 @@ import time from AppAgents import FidelityAgent, FinancialPlannerAgent, PersonalAssistant, QuantAgent, RiskManager -from autogencap.LocalActorNetwork import LocalActorNetwork +from autogencap.ComponentEnsemble import ComponentEnsemble from termcolor import colored @@ -14,17 +14,17 @@ def complex_actor_demo(): sends them to the personal assistant agent, and terminates when the user enters "quit". """ - network = LocalActorNetwork() + ensemble = ComponentEnsemble() # Register agents - network.register(PersonalAssistant()) - network.register(FidelityAgent()) - network.register(FinancialPlannerAgent()) - network.register(RiskManager()) - network.register(QuantAgent()) + ensemble.register(PersonalAssistant()) + ensemble.register(FidelityAgent()) + ensemble.register(FinancialPlannerAgent()) + ensemble.register(RiskManager()) + ensemble.register(QuantAgent()) # Tell agents to connect to other agents - network.connect() + ensemble.connect() # Get a channel to the personal assistant agent - pa = network.lookup_actor(PersonalAssistant.cls_agent_name) + pa = ensemble.find_by_name(PersonalAssistant.cls_agent_name) info_msg = """ This is an imaginary personal assistant agent scenario. Five actors are connected in a self-determined graph. The user @@ -48,4 +48,4 @@ def complex_actor_demo(): # Cleanup pa.close() - network.disconnect() + ensemble.disconnect() diff --git a/samples/apps/cap/py/demo/SimpleActorDemo.py b/samples/apps/cap/py/demo/SimpleActorDemo.py index c0d753a9d6e2..0e60e0daeaab 100644 --- a/samples/apps/cap/py/demo/SimpleActorDemo.py +++ b/samples/apps/cap/py/demo/SimpleActorDemo.py @@ -1,8 +1,9 @@ import time from AppAgents import GreeterAgent +from autogencap.ComponentEnsemble import ComponentEnsemble from autogencap.DebugLog import Error -from autogencap.LocalActorNetwork import LocalActorNetwork +from autogencap.proto.CAP_pb2 import Ping def simple_actor_demo(): @@ -11,19 +12,17 @@ def simple_actor_demo(): sending a message, and performing cleanup operations. """ # CAP Platform + ensemble = ComponentEnsemble() + agent = GreeterAgent() + ensemble.register(agent) + ensemble.connect() + greeter_link = ensemble.find_by_name("Greeter") + ensemble.disconnect() - network = LocalActorNetwork() - # Register an actor - network.register(GreeterAgent()) - # Tell actor to connect to other actors - network.connect() - # Get a channel to the actor - greeter_link = network.lookup_actor("Greeter") - if greeter_link: - # Send a message to the actor - greeter_link.send_txt_msg("Hello World!") - # Cleanup - greeter_link.close() - else: - Error("simple_actor_demo", "Could not find Greeter") - network.disconnect() + ping = Ping() + # Serialize and send the message + msg_type_str = Ping.__name__ + msg_bytes = ping.SerializeToString() + greeter_link.send_txt_msg("Hello World!") + greeter_link.send_bin_msg(msg_type_str, msg_bytes) + _, resp_type, resp_msg_bytes = greeter_link.send_recv_msg(msg_type_str, msg_bytes) diff --git a/samples/apps/cap/py/demo/list_agents.py b/samples/apps/cap/py/demo/list_agents.py index 05002094667c..3a93dda29422 100644 --- a/samples/apps/cap/py/demo/list_agents.py +++ b/samples/apps/cap/py/demo/list_agents.py @@ -2,8 +2,8 @@ from typing import List from AppAgents import FidelityAgent, GreeterAgent +from autogencap.ComponentEnsemble import ComponentEnsemble from autogencap.DebugLog import Info -from autogencap.LocalActorNetwork import LocalActorNetwork from autogencap.proto.CAP_pb2 import ActorInfo @@ -14,15 +14,15 @@ def list_agents(): """ # CAP Platform - network = LocalActorNetwork() + ensemble = ComponentEnsemble() # Register an actor - network.register(GreeterAgent()) + ensemble.register(GreeterAgent()) # Register an actor - network.register(FidelityAgent()) + ensemble.register(FidelityAgent()) # Tell actor to connect to other actors - network.connect() + ensemble.connect() # Get a list of actors - actor_infos: List[ActorInfo] = network.lookup_actor_info(name_regex=".*") + actor_infos: List[ActorInfo] = ensemble.find_by_name_regex(name_regex=".*") # Print out all actors found Info("list_agents", f"{len(actor_infos)} actors found:") for actor_info in actor_infos: @@ -31,4 +31,4 @@ def list_agents(): f"Name: {actor_info.name}, Namespace: {actor_info.namespace}, Description: {actor_info.description}", ) # Cleanup - network.disconnect() + ensemble.disconnect() diff --git a/samples/apps/cap/py/demo/standalone/Assistant.py b/samples/apps/cap/py/demo/standalone/Assistant.py index 9d3d497fd0d0..789482e48881 100644 --- a/samples/apps/cap/py/demo/standalone/Assistant.py +++ b/samples/apps/cap/py/demo/standalone/Assistant.py @@ -2,8 +2,8 @@ import _paths from autogencap.ag_adapter.CAP2AG import CAP2AG +from autogencap.ComponentEnsemble import ComponentEnsemble from autogencap.DebugLog import Info -from autogencap.LocalActorNetwork import LocalActorNetwork from autogen import AssistantAgent, config_list_from_json @@ -18,10 +18,10 @@ def run(self): config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST") assistant = AssistantAgent("assistant", llm_config={"config_list": config_list}) # Composable Agent Network adapter - network = LocalActorNetwork() + ensemble = ComponentEnsemble() assistant_adptr = CAP2AG(ag_agent=assistant, the_other_name="user_proxy", init_chat=False, self_recursive=True) - network.register(assistant_adptr) - network.connect() + ensemble.register(assistant_adptr) + ensemble.connect() # Hang around for a while try: @@ -29,7 +29,7 @@ def run(self): time.sleep(0.5) except KeyboardInterrupt: print("Interrupted by user, shutting down.") - network.disconnect() + ensemble.disconnect() Info("StandaloneAssistant", "App Exit") diff --git a/samples/apps/cap/py/demo/standalone/UserProxy.py b/samples/apps/cap/py/demo/standalone/UserProxy.py index 691b55d219c3..981198072e6f 100644 --- a/samples/apps/cap/py/demo/standalone/UserProxy.py +++ b/samples/apps/cap/py/demo/standalone/UserProxy.py @@ -2,9 +2,9 @@ import _paths from autogencap.ag_adapter.CAP2AG import CAP2AG +from autogencap.ComponentEnsemble import ComponentEnsemble from autogencap.Config import IGNORED_LOG_CONTEXTS from autogencap.DebugLog import Info -from autogencap.LocalActorNetwork import LocalActorNetwork from autogen import UserProxyAgent, config_list_from_json @@ -23,13 +23,13 @@ def run(self): is_termination_msg=lambda x: "TERMINATE" in x.get("content"), ) # Composable Agent Network adapter - network = LocalActorNetwork() + ensemble = ComponentEnsemble() user_proxy_adptr = CAP2AG(ag_agent=user_proxy, the_other_name="assistant", init_chat=True, self_recursive=True) - network.register(user_proxy_adptr) - network.connect() + ensemble.register(user_proxy_adptr) + ensemble.connect() # Send a message to the user_proxy - user_proxy_conn = network.lookup_actor("user_proxy") + user_proxy_conn = ensemble.find_by_name("user_proxy") example = "Plot a chart of MSFT daily closing prices for last 1 Month." print(f"Example: {example}") try: @@ -44,7 +44,7 @@ def run(self): time.sleep(0.5) except KeyboardInterrupt: print("Interrupted by user, shutting down.") - network.disconnect() + ensemble.disconnect() Info("StandaloneUserProxy", "App Exit") diff --git a/samples/apps/cap/py/demo/standalone/user_proxy.py b/samples/apps/cap/py/demo/standalone/user_proxy.py index 3ce4dac79276..4859361bb41b 100644 --- a/samples/apps/cap/py/demo/standalone/user_proxy.py +++ b/samples/apps/cap/py/demo/standalone/user_proxy.py @@ -2,8 +2,8 @@ import _paths from autogencap.ag_adapter.agent import Agent +from autogencap.ComponentEnsemble import ComponentEnsemble from autogencap.Config import IGNORED_LOG_CONTEXTS -from autogencap.LocalActorNetwork import LocalActorNetwork from autogen import UserProxyAgent @@ -22,21 +22,21 @@ def main(): # Wrap AutoGen Agent in CAP cap_user_proxy = Agent(user_proxy, counter_party_name="assistant", init_chat=True) # Create the message bus - network = LocalActorNetwork() + ensemble = ComponentEnsemble() # Add the user_proxy to the message bus - cap_user_proxy.register(network) + cap_user_proxy.register(ensemble) # Start message processing - network.connect() + ensemble.connect() # Wait for the user_proxy to finish - interact_with_user(network, cap_user_proxy) + interact_with_user(ensemble, cap_user_proxy) # Cleanup - network.disconnect() + ensemble.disconnect() # Starts the Broker and the Assistant. The UserProxy is started separately. def interact_with_user(network, cap_assistant): - user_proxy_conn = network.lookup_actor("user_proxy") + user_proxy_conn = network.find_by_name("user_proxy") example = "Plot a chart of MSFT daily closing prices for last 1 Month." print(f"Example: {example}") try: diff --git a/samples/apps/cap/py/pyproject.toml b/samples/apps/cap/py/pyproject.toml index 51024bb8b279..c52a59a0950f 100644 --- a/samples/apps/cap/py/pyproject.toml +++ b/samples/apps/cap/py/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "autogencap_rajan.jedi" -version = "0.0.7" +version = "0.0.9" authors = [ { name="Rajan Chari", email="rajan.jedi@gmail.com" }, ]