You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Actor.py 3.0 kB

Feature: Composable Actor Platform for AutoGen (#1655) * Core CAP components + Autogen adapter + Demo * Cleanup Readme * C# folder * Cleanup readme * summary_method bug fix * CAN -> CAP * pre-commit fixes * pre-commit fixes * modification of sys path should ignore E402 * fix pre-commit check issues * Updated docs * Clean up docs * more refactoring * better packaging refactor * Refactoring for package changes * Run demo app without autogencap installed or in the path * Remove debug related sleep() * removed CAP in some class names * Investigate a logging framework that supports color in windows * added type hints * remove circular dependency * fixed pre-commit issues * pre-commit ruff issues * removed circular definition * pre-commit fixes * Fix pre-commit issues * pre-commit fixes * updated for _prepare_chat signature changes * Better instructions for demo and some minor refactoring * Added details that explain CAP * Reformat Readme * More ReadMe Formatting * Readme edits * Agent -> Actor * Broker can startup on it's own * Remote AutoGen Agents * Updated docs * 1) StandaloneBroker in demo 2) Removed Autogen only demo options * 1) Agent -> Actor refactor 2) init broker as early * rename user_proxy -> user_proxy_conn * Add DirectorySvc * Standalone demo refactor * Get ActorInfo from DirectorySvc when searching for Actor * Broker cleanup * Proper cleanup and remove debug sleep() * Run one directory service only. * fix paths to run demo apps from command line * Handle keyboard interrupt * Wait for Broker and Directory to start up * Move Terminate AGActor * Accept input from the user in UserProxy * Move sleeps close to operations that bind or connect * Comments * Created an encapsulated CAP Pair for AutoGen pair communication * pre-commit checks * fix pre-commit * Pair should not make assumptions about who is first and who is second * Use task passed into InitiateChat * Standalone directory svc * Fix broken LFS files * Long running DirectorySvc * DirectorySvc does not have a status * Exit DirectorySvc Loop * Debugging Remoting * Reduce frequency of status messages * Debugging remote Actor * roll back git-lfs updates * rollback git-lfs changes * Debug network connectivity * pre-commit fixes * Create a group chat interface familiar to AutoGen GroupChat users * pre-commit fixes
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import zmq
  2. import threading
  3. import traceback
  4. import time
  5. from .DebugLog import Debug, Info
  6. from .Config import xpub_url
  7. class Actor:
  8. def __init__(self, agent_name: str, description: str):
  9. self.actor_name: str = agent_name
  10. self.agent_description: str = description
  11. self.run = False
  12. def connect_network(self, network):
  13. Debug(self.actor_name, f"is connecting to {network}")
  14. Debug(self.actor_name, "connected")
  15. def _process_txt_msg(self, msg: str, msg_type: str, topic: str, sender: str) -> bool:
  16. Info(self.actor_name, f"InBox: {msg}")
  17. return True
  18. def _process_bin_msg(self, msg: bytes, msg_type: str, topic: str, sender: str) -> bool:
  19. Info(self.actor_name, f"Msg: topic=[{topic}], msg_type=[{msg_type}]")
  20. return True
  21. def _recv_thread(self):
  22. Debug(self.actor_name, "recv thread started")
  23. self._socket: zmq.Socket = self._context.socket(zmq.SUB)
  24. self._socket.setsockopt(zmq.RCVTIMEO, 500)
  25. self._socket.connect(xpub_url)
  26. str_topic = f"{self.actor_name}"
  27. Debug(self.actor_name, f"subscribe to: {str_topic}")
  28. self._socket.setsockopt_string(zmq.SUBSCRIBE, f"{str_topic}")
  29. try:
  30. while self.run:
  31. try:
  32. topic, msg_type, sender_topic, msg = self._socket.recv_multipart()
  33. topic = topic.decode("utf-8") # Convert bytes to string
  34. msg_type = msg_type.decode("utf-8") # Convert bytes to string
  35. sender_topic = sender_topic.decode("utf-8") # Convert bytes to string
  36. except zmq.Again:
  37. continue # No message received, continue to next iteration
  38. except Exception:
  39. continue
  40. if msg_type == "text":
  41. msg = msg.decode("utf-8") # Convert bytes to string
  42. if not self._process_txt_msg(msg, msg_type, topic, sender_topic):
  43. msg = "quit"
  44. if msg.lower() == "quit":
  45. break
  46. else:
  47. if not self._process_bin_msg(msg, msg_type, topic, sender_topic):
  48. break
  49. except Exception as e:
  50. Debug(self.actor_name, f"recv thread encountered an error: {e}")
  51. traceback.print_exc()
  52. finally:
  53. self.run = False
  54. Debug(self.actor_name, "recv thread ended")
  55. def start(self, context: zmq.Context):
  56. self._context = context
  57. self.run: bool = True
  58. self._thread = threading.Thread(target=self._recv_thread)
  59. self._thread.start()
  60. time.sleep(0.01)
  61. def disconnect_network(self, network):
  62. Debug(self.actor_name, f"is disconnecting from {network}")
  63. Debug(self.actor_name, "disconnected")
  64. self.stop()
  65. def stop(self):
  66. self.run = False
  67. self._thread.join()
  68. self._socket.setsockopt(zmq.LINGER, 0)
  69. self._socket.close()