Skip to content

Commit e6b6a11

Browse files
rajan-chariqingyun-wu
authored andcommitted
[CAP] Convenience methods for protobuf and some minor refactoring (#3022)
* First pass: message loop in main thread * pypi version bump * Fix readme * Better example * Fixed docs * pre-commit fixes * Convenience methods for protobufs * support non-color consoles * Non-color console and allow user input * Minor update to single_threaded_demo * new pypi version * pre-commit fixes * change pypi name --------- Co-authored-by: Qingyun Wu <[email protected]>
1 parent d3ca6e4 commit e6b6a11

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed

samples/apps/cap/py/autogencap/ActorConnector.py

+10
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ def send_txt_msg(self, msg):
120120
def send_bin_msg(self, msg_type: str, msg):
121121
self._sender.send_bin_msg(msg_type, msg)
122122

123+
def send_proto_msg(self, msg):
124+
bin_msg = msg.SerializeToString()
125+
class_type = type(msg)
126+
self._sender.send_bin_msg(class_type.__name__, bin_msg)
127+
128+
def send_recv_proto_msg(self, msg, num_attempts=5):
129+
bin_msg = msg.SerializeToString()
130+
class_type = type(msg)
131+
return self.send_recv_msg(class_type.__name, bin_msg, num_attempts)
132+
123133
def send_recv_msg(self, msg_type: str, msg, num_attempts=5):
124134
original_timeout: int = 0
125135
if num_attempts == -1:

samples/apps/cap/py/autogencap/DebugLog.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,25 @@ def WriteLog(self, level, context, msg):
3434

3535

3636
class ConsoleLogger(BaseLogger):
37-
def __init__(self):
37+
def __init__(self, use_color=True):
3838
super().__init__()
39+
self._use_color = use_color
40+
41+
def _colorize(self, msg, color):
42+
if self._use_color:
43+
return colored(msg, color)
44+
return msg
3945

4046
def WriteLog(self, level, context, msg):
41-
timestamp = colored(datetime.datetime.now().strftime("%m/%d/%y %H:%M:%S"), "dark_grey")
47+
timestamp = self._colorize(datetime.datetime.now().strftime("%m/%d/%y %H:%M:%S"), "dark_grey")
4248
# Translate level number to name and color
43-
level_name = colored(LEVEL_NAMES[level], LEVEL_COLOR[level])
49+
level_name = self._colorize(LEVEL_NAMES[level], LEVEL_COLOR[level])
4450
# Left justify the context and color it blue
45-
context = colored(context.ljust(14), "blue")
51+
context = self._colorize(context.ljust(14), "blue")
4652
# Left justify the threadid and color it blue
47-
thread_id = colored(str(threading.get_ident()).ljust(5), "blue")
53+
thread_id = self._colorize(str(threading.get_ident()).ljust(5), "blue")
4854
# color the msg based on the level
49-
msg = colored(msg, LEVEL_COLOR[level])
55+
msg = self._colorize(msg, LEVEL_COLOR[level])
5056
print(f"{thread_id} {timestamp} {level_name}: [{context}] {msg}")
5157

5258

samples/apps/cap/py/demo/CAPAutoGenPairDemo.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import time
22

3+
import autogencap.DebugLog as DebugLog
34
from autogencap.ag_adapter.CAPPair import CAPPair
45
from autogencap.ComponentEnsemble import ComponentEnsemble
5-
from autogencap.DebugLog import Info
6+
from autogencap.DebugLog import ConsoleLogger, Info
67

78
from autogen import AssistantAgent, UserProxyAgent, config_list_from_json
89

910

1011
def cap_ag_pair_demo():
12+
DebugLog.LOGGER = ConsoleLogger(use_color=False)
13+
1114
config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST")
1215
assistant = AssistantAgent("assistant", llm_config={"config_list": config_list})
1316
user_proxy = UserProxyAgent(
@@ -20,7 +23,10 @@ def cap_ag_pair_demo():
2023
ensemble = ComponentEnsemble()
2124

2225
pair = CAPPair(ensemble, user_proxy, assistant)
23-
pair.initiate_chat("Plot a chart of MSFT daily closing prices for last 1 Month.")
26+
user_cmd = "Plot a chart of MSFT daily closing prices for last 1 Month"
27+
print(f"Default: {user_cmd}")
28+
user_cmd = input("Enter a command: ") or user_cmd
29+
pair.initiate_chat(user_cmd)
2430

2531
# Wait for the pair to finish
2632
try:

samples/apps/cap/py/demo/single_threaded.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ def single_threaded_demo():
1919
greeter_link.send_txt_msg("Hello World!")
2020

2121
no_msg = 0
22+
23+
# This is where we process the messages in this thread
24+
# instead of using a separate thread
25+
26+
# 5 consecutive times with no message received
27+
# will break the loop
2228
while no_msg < 5:
29+
# Get the message for the actor
2330
message = agent.get_message()
31+
# Let the actor process the message
2432
agent.dispatch_message(message)
25-
if message is None:
26-
no_msg += 1
27-
28-
message = agent.get_message()
29-
agent.dispatch_message(message)
33+
# If no message is received, increment the counter otherwise reset it
34+
no_msg = no_msg + 1 if message is None else 0
3035

3136
ensemble.disconnect()
3237

samples/apps/cap/py/pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ requires = ["hatchling"]
33
build-backend = "hatchling.build"
44

55
[project]
6-
name = "autogencap_rajan.jedi"
7-
version = "0.0.10"
6+
name = "autogencap"
7+
version = "0.0.11"
88
authors = [
99
{ name="Rajan Chari", email="[email protected]" },
1010
]

0 commit comments

Comments
 (0)