mirror of
https://github.com/microsoft/autogen.git
synced 2025-11-03 11:20:35 +00:00
[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 <qingyun0327@gmail.com>
This commit is contained in:
parent
fa88646301
commit
b7bdbe1ecc
@ -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:
|
||||
|
||||
@ -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}")
|
||||
|
||||
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -19,14 +19,19 @@ def single_threaded_demo():
|
||||
greeter_link.send_txt_msg("Hello World!")
|
||||
|
||||
no_msg = 0
|
||||
while no_msg < 5:
|
||||
message = agent.get_message()
|
||||
agent.dispatch_message(message)
|
||||
if message is None:
|
||||
no_msg += 1
|
||||
|
||||
message = agent.get_message()
|
||||
agent.dispatch_message(message)
|
||||
# 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 no message is received, increment the counter otherwise reset it
|
||||
no_msg = no_msg + 1 if message is None else 0
|
||||
|
||||
ensemble.disconnect()
|
||||
|
||||
|
||||
@ -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="rajan.jedi@gmail.com" },
|
||||
]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user