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

context to kwargs #2064

Merged
merged 7 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions autogen/agentchat/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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="")


Expand Down
47 changes: 23 additions & 24 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -944,19 +944,19 @@ 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.

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]:
Expand All @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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.

Expand All @@ -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]:
Expand All @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand All @@ -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.

Expand All @@ -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]]):
Expand Down
2 changes: 1 addition & 1 deletion notebook/agentchat_nested_sequential_chats.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 2 additions & 0 deletions test/agentchat/test_chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
{
Expand All @@ -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,
Expand Down
Loading