Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Add global silent param for ConversableAgent #3244

Merged
merged 12 commits into from
Aug 2, 2024
21 changes: 21 additions & 0 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __init__(
default_auto_reply: Union[str, Dict] = "",
description: Optional[str] = None,
chat_messages: Optional[Dict[Agent, List[Dict]]] = None,
silent: Optional[bool] = None,
):
"""
Args:
Expand Down Expand Up @@ -126,6 +127,8 @@ def __init__(
chat_messages (dict or None): the previous chat messages that this agent had in the past with other agents.
Can be used to give the agent a memory by providing the chat history. This will allow the agent to
resume previous had conversations. Defaults to an empty chat history.
silent (bool or None): (Experimental) whether to print the message sent. If None, will use the value of
silent in each function.
"""
# we change code_execution_config below and we have to make sure we don't change the input
# in case of UserProxyAgent, without this we could even change the default value {}
Expand All @@ -147,6 +150,7 @@ def __init__(
if is_termination_msg is not None
else (lambda x: content_str(x.get("content")) == "TERMINATE")
)
self.silent = silent
# Take a copy to avoid modifying the given dict
if isinstance(llm_config, dict):
try:
Expand Down Expand Up @@ -263,6 +267,9 @@ def _validate_llm_config(self, llm_config):
)
self.client = None if self.llm_config is False else OpenAIWrapper(**self.llm_config)

def _is_silent(self, silent: Optional[bool] = False) -> bool:
return self.silent if self.silent is not None else silent

@property
def name(self) -> str:
"""Get the name of the agent."""
Expand Down Expand Up @@ -604,6 +611,8 @@ def _process_message_before_send(
self, message: Union[Dict, str], recipient: Agent, silent: bool
) -> Union[Dict, str]:
"""Process the message before sending it to the recipient."""
silent = self._is_silent(silent)

hook_list = self.hook_lists["process_message_before_send"]
for hook in hook_list:
message = hook(sender=self, message=message, recipient=recipient, silent=silent)
Zizo-Vi marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -648,6 +657,8 @@ def send(
Raises:
ValueError: if the message can't be converted into a valid ChatCompletion message.
"""
silent = self._is_silent(silent)

message = self._process_message_before_send(message, recipient, silent)
Zizo-Vi marked this conversation as resolved.
Show resolved Hide resolved
# When the agent composes and sends the message, the role of the message is "assistant"
# unless it's "function".
Expand Down Expand Up @@ -698,6 +709,8 @@ async def a_send(
Raises:
ValueError: if the message can't be converted into a valid ChatCompletion message.
"""
silent = self._is_silent(silent)

message = self._process_message_before_send(message, recipient, silent)
# When the agent composes and sends the message, the role of the message is "assistant"
# unless it's "function".
Expand Down Expand Up @@ -771,6 +784,8 @@ def _print_received_message(self, message: Union[Dict, str], sender: Agent):
iostream.print("\n", "-" * 80, flush=True, sep="")

def _process_received_message(self, message: Union[Dict, str], sender: Agent, silent: bool):
silent = self._is_silent(silent)

# When the agent receives a message, the role of the message is "user". (If 'role' exists and is 'function', it will remain unchanged.)
valid = self._append_oai_message(message, "user", sender)
if logging_enabled():
Expand Down Expand Up @@ -813,6 +828,8 @@ def receive(
Raises:
ValueError: if the message can't be converted into a valid ChatCompletion message.
"""
silent = self._is_silent(silent)

self._process_received_message(message, sender, silent)
if request_reply is False or request_reply is None and self.reply_at_receive[sender] is False:
return
Expand Down Expand Up @@ -850,6 +867,8 @@ async def a_receive(
Raises:
ValueError: if the message can't be converted into a valid ChatCompletion message.
"""
silent = self._is_silent(silent)

self._process_received_message(message, sender, silent)
if request_reply is False or request_reply is None and self.reply_at_receive[sender] is False:
return
Expand Down Expand Up @@ -990,6 +1009,7 @@ def my_message(sender: ConversableAgent, recipient: ConversableAgent, context: d
Returns:
ChatResult: an ChatResult object.
"""
silent = self._is_silent(silent)
_chat_info = locals().copy()
_chat_info["sender"] = self
consolidate_chat_info(_chat_info, uniform_sender=self)
Expand Down Expand Up @@ -1057,6 +1077,7 @@ async def a_initiate_chat(
Returns:
ChatResult: an ChatResult object.
"""
silent = self._is_silent(silent)
_chat_info = locals().copy()
_chat_info["sender"] = self
consolidate_chat_info(_chat_info, uniform_sender=self)
Expand Down
Loading