Skip to content

Commit

Permalink
[CAP] Convenience methods for protobuf and some minor refactoring (mi…
Browse files Browse the repository at this point in the history
…crosoft#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]>
  • Loading branch information
rajan-chari and qingyun-wu authored Jul 23, 2024
1 parent fa88646 commit b7bdbe1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
10 changes: 10 additions & 0 deletions samples/apps/cap/py/autogencap/ActorConnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ def send_txt_msg(self, msg):
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:
Expand Down
18 changes: 12 additions & 6 deletions samples/apps/cap/py/autogencap/DebugLog.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,25 @@ def WriteLog(self, level, context, msg):


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}")


Expand Down
10 changes: 8 additions & 2 deletions samples/apps/cap/py/demo/CAPAutoGenPairDemo.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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:
Expand Down
15 changes: 10 additions & 5 deletions samples/apps/cap/py/demo/single_threaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions samples/apps/cap/py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]" },
]
Expand Down

0 comments on commit b7bdbe1

Please sign in to comment.