Skip to content

Commit

Permalink
[Feature]: Add global silent param for ConversableAgent (#3244)
Browse files Browse the repository at this point in the history
* feat: add is_silent param

* modify param name

* param doc

* fix: silent only overwrite agent own

* fix: change _is_silent to static method and receive verbose print

* fix test failure

* add kwargs for ConversableAgent subclass

---------

Co-authored-by: gongwn1 <[email protected]>
Co-authored-by: Li Jiang <[email protected]>
Co-authored-by: Umer Mansoor <[email protected]>
  • Loading branch information
4 people authored Aug 2, 2024
1 parent 466c851 commit 354dca8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
2 changes: 2 additions & 0 deletions autogen/agentchat/contrib/society_of_mind_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(
code_execution_config: Union[Dict, Literal[False]] = False,
llm_config: Optional[Union[Dict, Literal[False]]] = False,
default_auto_reply: Optional[Union[str, Dict, None]] = "",
**kwargs,
):
super().__init__(
name=name,
Expand All @@ -50,6 +51,7 @@ def __init__(
code_execution_config=code_execution_config,
llm_config=llm_config,
default_auto_reply=default_auto_reply,
**kwargs,
)

self.update_chat_manager(chat_manager)
Expand Down
2 changes: 2 additions & 0 deletions autogen/agentchat/contrib/web_surfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(
summarizer_llm_config: Optional[Union[Dict, Literal[False]]] = None,
default_auto_reply: Optional[Union[str, Dict, None]] = "",
browser_config: Optional[Union[Dict, None]] = None,
**kwargs,
):
super().__init__(
name=name,
Expand All @@ -53,6 +54,7 @@ def __init__(
code_execution_config=code_execution_config,
llm_config=llm_config,
default_auto_reply=default_auto_reply,
**kwargs,
)

self._create_summarizer_client(summarizer_llm_config, llm_config)
Expand Down
19 changes: 15 additions & 4 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,10 @@ def _validate_llm_config(self, llm_config):
)
self.client = None if self.llm_config is False else OpenAIWrapper(**self.llm_config)

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

@property
def name(self) -> str:
"""Get the name of the agent."""
Expand Down Expand Up @@ -606,7 +614,9 @@ def _process_message_before_send(
"""Process the message before sending it to the recipient."""
hook_list = self.hook_lists["process_message_before_send"]
for hook in hook_list:
message = hook(sender=self, message=message, recipient=recipient, silent=silent)
message = hook(
sender=self, message=message, recipient=recipient, silent=ConversableAgent._is_silent(self, silent)
)
return message

def send(
Expand Down Expand Up @@ -648,7 +658,7 @@ def send(
Raises:
ValueError: if the message can't be converted into a valid ChatCompletion message.
"""
message = self._process_message_before_send(message, recipient, silent)
message = self._process_message_before_send(message, recipient, ConversableAgent._is_silent(self, silent))
# When the agent composes and sends the message, the role of the message is "assistant"
# unless it's "function".
valid = self._append_oai_message(message, "assistant", recipient)
Expand Down Expand Up @@ -698,7 +708,7 @@ async def a_send(
Raises:
ValueError: if the message can't be converted into a valid ChatCompletion message.
"""
message = self._process_message_before_send(message, recipient, silent)
message = self._process_message_before_send(message, recipient, ConversableAgent._is_silent(self, silent))
# When the agent composes and sends the message, the role of the message is "assistant"
# unless it's "function".
valid = self._append_oai_message(message, "assistant", recipient)
Expand Down Expand Up @@ -780,7 +790,8 @@ def _process_received_message(self, message: Union[Dict, str], sender: Agent, si
raise ValueError(
"Received message can't be converted into a valid ChatCompletion message. Either content or function_call must be provided."
)
if not silent:

if not ConversableAgent._is_silent(sender, silent):
self._print_received_message(message, sender)

def receive(
Expand Down
4 changes: 4 additions & 0 deletions autogen/agentchat/user_proxy_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(
llm_config: Optional[Union[Dict, Literal[False]]] = False,
system_message: Optional[Union[str, List]] = "",
description: Optional[str] = None,
**kwargs,
):
"""
Args:
Expand Down Expand Up @@ -79,6 +80,8 @@ def __init__(
Only used when llm_config is not False. Use it to reprogram the agent.
description (str): a short description of the agent. This description is used by other agents
(e.g. the GroupChatManager) to decide when to call upon this agent. (Default: system_message)
**kwargs (dict): Please refer to other kwargs in
[ConversableAgent](conversable_agent#__init__).
"""
super().__init__(
name=name,
Expand All @@ -93,6 +96,7 @@ def __init__(
description=(
description if description is not None else self.DEFAULT_USER_PROXY_AGENT_DESCRIPTIONS[human_input_mode]
),
**kwargs,
)

if logging_enabled():
Expand Down
2 changes: 2 additions & 0 deletions test/agentchat/test_tool_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class FakeAgent(autogen.Agent):
def __init__(self, name):
self._name = name
self.received = []
self.silent = False

@property
def name(self):
Expand Down Expand Up @@ -303,6 +304,7 @@ class FakeAgent(autogen.Agent):
def __init__(self, name):
self._name = name
self.received = []
self.silent = False

@property
def name(self):
Expand Down

0 comments on commit 354dca8

Please sign in to comment.