diff --git a/autogen/agentchat/chat.py b/autogen/agentchat/chat.py index d5e127e971f3..a50885cb9e64 100644 --- a/autogen/agentchat/chat.py +++ b/autogen/agentchat/chat.py @@ -102,7 +102,7 @@ def __find_async_chat_order(chat_ids: Set[int], prerequisites: List[Prerequisite return chat_order -def __post_carryover_processing(chat_info: Dict[str, Any]): +def __post_carryover_processing(chat_info: Dict[str, Any]) -> None: if "message" not in chat_info: warnings.warn( "message is not provided in a chat_queue entry. input() will be called to get the initial message.", @@ -125,11 +125,14 @@ def __post_carryover_processing(chat_info: Dict[str, Any]): print(colored("\n" + "*" * 80, "blue"), flush=True, sep="") print( colored( - "Starting a new chat....\n\nMessage:\n" + print_message + "\n\nCarryover: \n" + print_carryover, + "Starting a new chat....", "blue", ), flush=True, ) + if chat_info.get("verbose", False): + print(colored("Message:\n" + print_message, "blue"), flush=True) + print(colored("Carryover:\n" + print_carryover, "blue"), flush=True) print(colored("\n" + "*" * 80, "blue"), flush=True, sep="") diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index 54206b550521..3c8f9e79d899 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -868,7 +868,7 @@ def initiate_chat( summary_method: Optional[Union[str, Callable]] = DEFAULT_SUMMARY_METHOD, summary_args: Optional[dict] = {}, message: Optional[Union[Dict, str, Callable]] = None, - **context, + **kwargs, ) -> ChatResult: """Initiate a chat with the recipient agent. @@ -944,10 +944,11 @@ def my_message(sender: ConversableAgent, recipient: ConversableAgent, context: d final_msg["context"] = {"prefix": "Today I feel"} return final_msg ``` - **context: any context information. It has the following reserved fields: + **kwargs: any additional information. It has the following reserved fields: - "carryover": a string or a list of string to specify the carryover information to be passed to this chat. If provided, we will combine this carryover (by attaching a "context: " string and the carryover content after the message content) with the "message" content when generating the initial chat message in `generate_init_message`. + - "verbose": a boolean to specify whether to print the message and carryover in a chat. Default is False. Raises: RuntimeError: if any async reply functions are registered and not ignored in sync chat. @@ -955,8 +956,7 @@ def my_message(sender: ConversableAgent, recipient: ConversableAgent, context: d Returns: ChatResult: an ChatResult object. """ - _chat_info = context.copy() - _chat_info["recipient"] = recipient + _chat_info = locals().copy() _chat_info["sender"] = self consolidate_chat_info(_chat_info, uniform_sender=self) for agent in [self, recipient]: @@ -968,9 +968,9 @@ def my_message(sender: ConversableAgent, recipient: ConversableAgent, context: d for _ in range(max_turns): if _ == 0: if isinstance(message, Callable): - msg2send = message(_chat_info["sender"], _chat_info["recipient"], context) + msg2send = message(_chat_info["sender"], _chat_info["recipient"], kwargs) else: - msg2send = self.generate_init_message(message, **context) + msg2send = self.generate_init_message(message, **kwargs) else: msg2send = self.generate_reply(messages=self.chat_messages[recipient], sender=recipient) if msg2send is None: @@ -979,9 +979,9 @@ def my_message(sender: ConversableAgent, recipient: ConversableAgent, context: d else: self._prepare_chat(recipient, clear_history) if isinstance(message, Callable): - msg2send = message(_chat_info["sender"], _chat_info["recipient"], context) + msg2send = message(_chat_info["sender"], _chat_info["recipient"], kwargs) else: - msg2send = self.generate_init_message(message, **context) + msg2send = self.generate_init_message(message, **kwargs) self.send(msg2send, recipient, silent=silent) summary = self._summarize_chat( summary_method, @@ -1010,7 +1010,7 @@ async def a_initiate_chat( summary_method: Optional[Union[str, Callable]] = DEFAULT_SUMMARY_METHOD, summary_args: Optional[dict] = {}, message: Optional[Union[str, Callable]] = None, - **context, + **kwargs, ) -> ChatResult: """(async) Initiate a chat with the recipient agent. @@ -1023,8 +1023,7 @@ async def a_initiate_chat( Returns: ChatResult: an ChatResult object. """ - _chat_info = context.copy() - _chat_info["recipient"] = recipient + _chat_info = locals().copy() _chat_info["sender"] = self consolidate_chat_info(_chat_info, uniform_sender=self) for agent in [self, recipient]: @@ -1035,9 +1034,9 @@ async def a_initiate_chat( for _ in range(max_turns): if _ == 0: if isinstance(message, Callable): - msg2send = message(_chat_info["sender"], _chat_info["recipient"], context) + msg2send = message(_chat_info["sender"], _chat_info["recipient"], kwargs) else: - msg2send = await self.a_generate_init_message(message, **context) + msg2send = await self.a_generate_init_message(message, **kwargs) else: msg2send = await self.a_generate_reply(messages=self.chat_messages[recipient], sender=recipient) if msg2send is None: @@ -1046,9 +1045,9 @@ async def a_initiate_chat( else: self._prepare_chat(recipient, clear_history) if isinstance(message, Callable): - msg2send = message(_chat_info["sender"], _chat_info["recipient"], context) + msg2send = message(_chat_info["sender"], _chat_info["recipient"], kwargs) else: - msg2send = await self.a_generate_init_message(message, **context) + msg2send = await self.a_generate_init_message(message, **kwargs) await self.a_send(msg2send, recipient, silent=silent) summary = self._summarize_chat( summary_method, @@ -2217,13 +2216,13 @@ async def a_execute_function(self, func_call): "content": str(content), } - def generate_init_message(self, message: Union[Dict, str, None], **context) -> Union[str, Dict]: + def generate_init_message(self, message: Union[Dict, str, None], **kwargs) -> Union[str, Dict]: """Generate the initial message for the agent. If message is None, input() will be called to get the initial message. Args: message (str or None): the message to be processed. - **context: any context information. It has the following reserved fields: + **kwargs: any additional information. It has the following reserved fields: "carryover": a string or a list of string to specify the carryover information to be passed to this chat. It can be a string or a list of string. If provided, we will combine this carryover with the "message" content when generating the initial chat message. @@ -2233,17 +2232,17 @@ def generate_init_message(self, message: Union[Dict, str, None], **context) -> U if message is None: message = self.get_human_input(">") if isinstance(message, str): - return self._process_carryover(message, context) + return self._process_carryover(message, kwargs) elif isinstance(message, dict): message = message.copy() # TODO: Do we need to do the following? # if message.get("content") is None: # message["content"] = self.get_human_input(">") - message["content"] = self._process_carryover(message.get("content", ""), context) + message["content"] = self._process_carryover(message.get("content", ""), kwargs) return message - def _process_carryover(self, message: str, context: dict) -> str: - carryover = context.get("carryover") + def _process_carryover(self, message: str, kwargs: dict) -> str: + carryover = kwargs.get("carryover") if carryover: # if carryover is string if isinstance(carryover, str): @@ -2256,7 +2255,7 @@ def _process_carryover(self, message: str, context: dict) -> str: ) return message - async def a_generate_init_message(self, message: Union[Dict, str, None], **context) -> Union[str, Dict]: + async def a_generate_init_message(self, message: Union[Dict, str, None], **kwargs) -> Union[str, Dict]: """Generate the initial message for the agent. If message is None, input() will be called to get the initial message. @@ -2269,10 +2268,10 @@ async def a_generate_init_message(self, message: Union[Dict, str, None], **conte if message is None: message = await self.a_get_human_input(">") if isinstance(message, str): - return self._process_carryover(message, context) + return self._process_carryover(message, kwargs) elif isinstance(message, dict): message = message.copy() - message["content"] = self._process_carryover(message["content"], context) + message["content"] = self._process_carryover(message["content"], kwargs) return message def register_function(self, function_map: Dict[str, Union[Callable, None]]): diff --git a/notebook/agentchat_nested_sequential_chats.ipynb b/notebook/agentchat_nested_sequential_chats.ipynb index 2f591c2530a2..1b6ff0ea62f3 100644 --- a/notebook/agentchat_nested_sequential_chats.ipynb +++ b/notebook/agentchat_nested_sequential_chats.ipynb @@ -811,7 +811,7 @@ "front_matter": { "description": "Solve complex tasks with one or more sequence chats nested as inner monologue.", "tags": [ - "nested chat" + "nested chat", "sequential chat" ] }, "kernelspec": { diff --git a/test/agentchat/test_chats.py b/test/agentchat/test_chats.py index 153e5cd3bc93..b160520176b1 100755 --- a/test/agentchat/test_chats.py +++ b/test/agentchat/test_chats.py @@ -232,6 +232,7 @@ def my_summary_method(recipient, sender, summary_args): "message": financial_tasks[0], "silent": False, "summary_method": my_summary_method, + "verbose": True, "max_turns": 1, }, { @@ -240,6 +241,7 @@ def my_summary_method(recipient, sender, summary_args): "silent": False, "max_turns": 1, "summary_method": "reflection_with_llm", + "verbose": True, }, { "recipient": financial_assistant_1,