Skip to content

A programming framework for agentic AI

[CAP] Convenience methods for protobuf and some minor refactoring (#3022)

Rajanb7bdbe1ecc1c
diff --git a/samples/apps/cap/py/autogencap/ActorConnector.py b/samples/apps/cap/py/autogencap/ActorConnector.py
index 588d4c463..1595f641f 100644
--- a/samples/apps/cap/py/autogencap/ActorConnector.py
+++ b/samples/apps/cap/py/autogencap/ActorConnector.py
@@ -120,6 +120,16 @@ class ActorConnector:
def send_bin_msg(self, msg_type: str, msg):
self._sender.send_bin_msg(msg_type, msg)
+ def send_proto_msg(self, msg):
+ bin_msg = msg.SerializeToString()
+ class_type = type(msg)
+ self._sender.send_bin_msg(class_type.__name__, bin_msg)
+
+ def send_recv_proto_msg(self, msg, num_attempts=5):
+ bin_msg = msg.SerializeToString()
+ class_type = type(msg)
+ return self.send_recv_msg(class_type.__name, bin_msg, num_attempts)
+
def send_recv_msg(self, msg_type: str, msg, num_attempts=5):
original_timeout: int = 0
if num_attempts == -1:
diff --git a/samples/apps/cap/py/autogencap/DebugLog.py b/samples/apps/cap/py/autogencap/DebugLog.py
index c3d6ca421..f8a3f209e 100644
--- a/samples/apps/cap/py/autogencap/DebugLog.py
+++ b/samples/apps/cap/py/autogencap/DebugLog.py
@@ -34,19 +34,25 @@ class BaseLogger:
class ConsoleLogger(BaseLogger):
- def __init__(self):
+ def __init__(self, use_color=True):
super().__init__()
+ self._use_color = use_color
+
+ def _colorize(self, msg, color):
+ if self._use_color:
+ return colored(msg, color)
+ return msg
def WriteLog(self, level, context, msg):
- timestamp = colored(datetime.datetime.now().strftime("%m/%d/%y %H:%M:%S"), "dark_grey")
+ timestamp = self._colorize(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])
+ level_name = self._colorize(LEVEL_NAMES[level], LEVEL_COLOR[level])
# Left justify the context and color it blue
- context = colored(context.ljust(14), "blue")
+ context = self._colorize(context.ljust(14), "blue")
# Left justify the threadid and color it blue
- thread_id = colored(str(threading.get_ident()).ljust(5), "blue")
+ thread_id = self._colorize(str(threading.get_ident()).ljust(5), "blue")
# color the msg based on the level
- msg = colored(msg, LEVEL_COLOR[level])
+ msg = self._colorize(msg, LEVEL_COLOR[level])
print(f"{thread_id} {timestamp} {level_name}: [{context}] {msg}")
diff --git a/samples/apps/cap/py/demo/CAPAutoGenPairDemo.py b/samples/apps/cap/py/demo/CAPAutoGenPairDemo.py
index 732bfecad..00ff7a892 100644
--- a/samples/apps/cap/py/demo/CAPAutoGenPairDemo.py
+++ b/samples/apps/cap/py/demo/CAPAutoGenPairDemo.py
@@ -1,13 +1,16 @@
import time
+import autogencap.DebugLog as DebugLog
from autogencap.ag_adapter.CAPPair import CAPPair
from autogencap.ComponentEnsemble import ComponentEnsemble
-from autogencap.DebugLog import Info
+from autogencap.DebugLog import ConsoleLogger, Info
from autogen import AssistantAgent, UserProxyAgent, config_list_from_json
def cap_ag_pair_demo():
+ DebugLog.LOGGER = ConsoleLogger(use_color=False)
+
config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST")
assistant = AssistantAgent("assistant", llm_config={"config_list": config_list})
user_proxy = UserProxyAgent(
@@ -20,7 +23,10 @@ def cap_ag_pair_demo():
ensemble = ComponentEnsemble()
pair = CAPPair(ensemble, user_proxy, assistant)
- pair.initiate_chat("Plot a chart of MSFT daily closing prices for last 1 Month.")
+ user_cmd = "Plot a chart of MSFT daily closing prices for last 1 Month"
+ print(f"Default: {user_cmd}")
+ user_cmd = input("Enter a command: ") or user_cmd
+ pair.initiate_chat(user_cmd)
# Wait for the pair to finish
try:
diff --git a/samples/apps/cap/py/demo/single_threaded.py b/samples/apps/cap/py/demo/single_threaded.py
index 43cffbf02..d95f67128 100644
--- a/samples/apps/cap/py/demo/single_threaded.py
+++ b/samples/apps/cap/py/demo/single_threaded.py
@@ -19,14 +19,19 @@ def single_threaded_demo():
greeter_link.send_txt_msg("Hello World!")
no_msg = 0
+
+ # This is where we process the messages in this thread
+ # instead of using a separate thread
+
+ # 5 consecutive times with no message received
+ # will break the loop
while no_msg < 5:
+ # Get the message for the actor
message = agent.get_message()
+ # Let the actor process the message
agent.dispatch_message(message)
- if message is None:
- no_msg += 1
-
- message = agent.get_message()
- agent.dispatch_message(message)
+ # If no message is received, increment the counter otherwise reset it
+ no_msg = no_msg + 1 if message is None else 0
ensemble.disconnect()
diff --git a/samples/apps/cap/py/pyproject.toml b/samples/apps/cap/py/pyproject.toml
index 8988604a3..8a0fe227e 100644
--- a/samples/apps/cap/py/pyproject.toml
+++ b/samples/apps/cap/py/pyproject.toml
@@ -3,8 +3,8 @@ requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
-name = "autogencap_rajan.jedi"
-version = "0.0.10"
+name = "autogencap"
+version = "0.0.11"
authors = [
{ name="Rajan Chari", email="[email protected]" },
]