Skip to content

Commit

Permalink
Allow different senders in nested chat (microsoft#2028)
Browse files Browse the repository at this point in the history
* allow sender in nested chat

* description

* update example list

* doc format

* meta data

* add test
  • Loading branch information
qingyun-wu authored Mar 15, 2024
1 parent dc91cb1 commit da4ddb4
Show file tree
Hide file tree
Showing 7 changed files with 1,577 additions and 500 deletions.
4 changes: 3 additions & 1 deletion autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ def _summary_from_nested_chats(
chat_to_run = []
for i, c in enumerate(chat_queue):
current_c = c.copy()
if current_c.get("sender") is None:
current_c["sender"] = recipient
message = current_c.get("message")
# If message is not provided in chat_queue, we by default use the last message from the original chat history as the first message in this nested chat (for the first chat in the chat queue).
# NOTE: This setting is prone to change.
Expand All @@ -377,7 +379,7 @@ def _summary_from_nested_chats(
chat_to_run.append(current_c)
if not chat_to_run:
return True, None
res = recipient.initiate_chats(chat_to_run)
res = initiate_chats(chat_to_run)
return True, res[-1].summary

def register_nested_chats(
Expand Down
1,210 changes: 1,210 additions & 0 deletions notebook/agentchat_nested_sequential_chats.ipynb

Large diffs are not rendered by default.

825 changes: 338 additions & 487 deletions notebook/agentchat_nestedchat.ipynb

Large diffs are not rendered by default.

Binary file added notebook/nested_chat_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added notebook/nested_chat_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 24 additions & 11 deletions test/agentchat/test_nested.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_nested():
llm_config = {"config_list": config_list}

tasks = [
"""What's Microsoft's Stock price today?""",
"""What's the date today?""",
"""Make a pleasant joke about it.""",
]

Expand Down Expand Up @@ -60,6 +60,12 @@ def test_nested():
# is_termination_msg=lambda x: x.get("content", "") == "",
)

assistant_2 = autogen.AssistantAgent(
name="Assistant",
llm_config={"config_list": config_list},
# is_termination_msg=lambda x: x.get("content", "") == "",
)

user = autogen.UserProxyAgent(
name="User",
human_input_mode="NEVER",
Expand All @@ -71,6 +77,17 @@ def test_nested():
}, # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.
)

user_2 = autogen.UserProxyAgent(
name="User",
human_input_mode="NEVER",
is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
code_execution_config={
"last_n_messages": 1,
"work_dir": "tasks",
"use_docker": False,
}, # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.
)

writer = autogen.AssistantAgent(
name="Writer",
llm_config={"config_list": config_list},
Expand All @@ -82,7 +99,7 @@ def test_nested():
""",
)

reviewer = autogen.AssistantAgent(
autogen.AssistantAgent(
name="Reviewer",
llm_config={"config_list": config_list},
system_message="""
Expand All @@ -98,23 +115,19 @@ def test_nested():
)

def writing_message(recipient, messages, sender, config):
return f"Polish the content to make an engaging and nicely formatted blog post. \n\n {recipient.chat_messages_for_summary(sender)[-1]['content']}"
return f"Make a one-sentence comment. \n\n {recipient.chat_messages_for_summary(sender)[-1]['content']}"

nested_chat_queue = [
{"recipient": manager, "summary_method": "reflection_with_llm"},
{"sender": user_2, "recipient": manager, "summary_method": "reflection_with_llm"},
{"recipient": writer, "message": writing_message, "summary_method": "last_msg", "max_turns": 1},
{
"recipient": reviewer,
"message": "Review the content provided.",
"summary_method": "last_msg",
"max_turns": 1,
},
]
assistant.register_nested_chats(
nested_chat_queue,
trigger=user,
)
user.initiate_chats([{"recipient": assistant, "message": tasks[0]}, {"recipient": assistant, "message": tasks[1]}])
user.initiate_chats(
[{"recipient": assistant, "message": tasks[0], "max_turns": 1}, {"recipient": assistant_2, "message": tasks[1]}]
)


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion website/docs/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Links to notebook examples:

1. **Nested Chats**
- Solving Complex Tasks with Nested Chats - [View Notebook](/docs/notebooks/agentchat_nestedchat)
- OptiGuide for Solving a Supply Chain Optimization Problem with Nested Chats with a Coding Agent and a Safeguard Agent - [View Notebook](/docs/notebooks/agentchat_nestedchat_optiguide)
- Solving Complex Tasks with A Sequence of Nested Chats - [View Notebook](/docs/notebooks/agentchat_nested_sequential_chats)
- OptiGuide for Solving a Supply Chain Optimization Problem with Nested Chats with a Coding Agent and a Safeguard Agent - [View Notebook](/docs/notebooks/agentchat_nestedchat_optiguide)

1. **Applications**

Expand Down

0 comments on commit da4ddb4

Please sign in to comment.