-
Notifications
You must be signed in to change notification settings - Fork 230
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
[Docs] Improved Conversation Patterns documentation #1005
Merged
Merged
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d6ea677
Fix swarm use case image links
marklysze 22e9107
Restructure and sequential chat page added
marklysze 075d086
secrets
marklysze 0e0baaa
Nested chat text and example
marklysze edb18c0
Secrets args revert
marklysze c5a6fba
revert secrets baseline
marklysze df89f65
Swarm orchestration documentation includes simple group chat example
marklysze 1627b03
resolved conflicts
davorrunje 4e3aae3
.secrets.baseline updated
davorrunje bea2eda
wip
davorrunje 19d5994
Merge branch 'main' into ag2docsconvpatterns
davorrunje 861e816
GCM ValueError test
marklysze a0ec44d
Merge remote-tracking branch 'refs/remotes/origin/ag2docsconvpatterns…
marklysze 990334b
Merge branch 'main' into ag2docsconvpatterns
marklysze 96add84
Formatting and secrets
marklysze da2edca
Missed test update, test_lmm.py
marklysze 69c245b
mock_credentials.llm_config
marklysze c3ca166
Move Reference Agents to User Guide, fix tests
marklysze cec1bc1
test_chats remove mock credentials
marklysze 01e89fa
Reference Agent links, added api_type openai to llm configs in tests …
marklysze 907d198
Revert api_type openai on a test due to llm validation bug.
marklysze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...e/advanced-concepts/custom-group-chat.mdx → ...-concepts/groupchat/custom-group-chat.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...s/user-guide/basic-concepts/groupchat.mdx → ...advanced-concepts/groupchat/groupchat.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
title: GroupChat | ||
sidebarTitle: Overview | ||
--- | ||
|
||
`GroupChat` has four built-in conversation patterns: | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
website/docs/user-guide/basic-concepts/assets/nested-chats.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
website/docs/user-guide/basic-concepts/assets/sequential-two-agent-chat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions
29
website/docs/user-guide/basic-concepts/orchestration/nested-chat.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
Title: Nested chat | ||
--- | ||
|
||
A powerful method to encapsulate a single workflow into an agent is through AG2's nested chats. | ||
|
||
Nested chats is powered by the nested chats handler, which is a pluggable component of [`ConversableAgent`](/docs/api-reference/autogen/ConversableAgent). The figure below illustrates how the nested chats handler triggers a sequence of nested chats when a message is received. | ||
|
||
 | ||
|
||
When a message comes in and passes the human-in-the-loop component, the nested chats handler checks if the message should trigger a nested chat based on conditions specified by the user. | ||
|
||
If the conditions are met, the nested chats handler starts a sequence of nested chats specified using the sequential chats pattern. | ||
|
||
In each of the nested chats, the sender agent is always the same agent that triggered the nested chats. | ||
|
||
In the end, the nested chat handler uses the results of the nested chats to produce a response to the original message (by default a summary of the last chat). | ||
|
||
Here is an example of a nested chat where a single agent, `lead_teacher_agent` encapsulates a workflow involving five other agents. This inner workflow includes two-agent chats and a group chat. | ||
|
||
The end result is that when this agent is called to reply in a chat it will, internally, run through a workflow using the nested chats and a fully considered lesson plan will be returned as its reply. | ||
|
||
import Example from "/snippets/python-examples/nestedchat.mdx"; | ||
|
||
<Example/> | ||
|
||
Nested chat is a useful conversation pattern that allows you to package complex workflows into a single agent. | ||
|
||
For further information on setting up a nested chat, see the [`register_nested_chats`](/docs/api-reference/autogen/ConversableAgent#register-nested-chats) API reference and this [notebook](/docs/use-cases/notebooks/notebooks/agentchat_nested_chats_chess). |
22 changes: 22 additions & 0 deletions
22
website/docs/user-guide/basic-concepts/orchestration/orchestrations.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Orchestrating agents | ||
sidebarTitle: Overview | ||
--- | ||
|
||
Many hands make for light work and orchestrating workflows containing many agents is a strength of the AG2 framework. | ||
|
||
1. **Two-agent chat**: The simplest form of conversation pattern where two agents chat back-and-forth with each other. This has been demonstrated in the previous examples. | ||
|
||
2. **Sequential chat**: A sequence of chats, each between two agents, chained together by a carryover mechanism (which brings the summary of the previous chat to the context of the next chat). Useful for simple sequential workflows. | ||
|
||
3. **Group chat**: A chat with more than two agents with options on how agents are selected. See [GroupChat Overview](/docs/user-guide/advanced-concepts/groupchat/groupchat) for further details. | ||
|
||
4. **Nested chat**: A mechanism to package a workflow into a single agent/chat for reuse in a workflow. | ||
|
||
5. **Swarm**: A pattern based on agents with handoffs. There's a shared context and each agent has tools and the ability to transfer control to other agents. The [original swarm concept](https://github.com/openai/swarm) was created by OpenAI. | ||
|
||
Continue on to explore these. | ||
|
||
<Note> | ||
We'll refer to *conversation patterns* throughout the documentation - they are simply a structured way of organizing the flow between agents. | ||
</Note> |
37 changes: 37 additions & 0 deletions
37
website/docs/user-guide/basic-concepts/orchestration/sequential-chat.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
Title: Sequential chat | ||
--- | ||
|
||
As a sequence of chats between two agents chained together by a mechanism called *carryover*, the sequential chat pattern is useful for complex tasks that can be broken down into interdependent sub-tasks. | ||
|
||
The figure below illustrates how this pattern works. | ||
|
||
 | ||
|
||
In this pattern, a pair of agents start a two-agent chat, then the summary of the conversation becomes a *carryover* for the next two-agent chat. The next chat passes the carryover to the `carryover` parameter of the context to generate its initial message. | ||
|
||
Carryover accumulates as the conversation moves forward, so each subsequent chat starts with all the carryovers from previous chats. | ||
|
||
The figure above shows distinct recipient agents for all the chats, however, the recipient agents in the sequence are allowed to repeat. | ||
|
||
To illustrate this pattern, let's create a sequential workflow to create our lesson plan for the fourth grade class. | ||
|
||
A teacher agent will engage three agents sequentially: | ||
|
||
1. A curriculum designer will select a topic given the subject from the teacher | ||
2. A lesson planner will design the lesson with two iterations given feedback from the teacher | ||
3. A formatter will format the lesson | ||
|
||
import Example from "/snippets/python-examples/sequentialchat.mdx"; | ||
|
||
<Example/> | ||
|
||
The sequential chat is triggered by the teacher agent's [`initiate_chats`](/docs/api-reference/autogen/ConversableAgent#initiate-chats) method, which takes a list of dictionaries where each dictionary represents a chat between the teacher and the `recipient` agent. | ||
|
||
The maximum number of turns in each chat can be controlled with the `max_turns` key. Each chat can also terminate before the `max_turns`, see the [Ending a Chat](/basic-concepts/orchestration/ending-a-chat) topic for further information. | ||
|
||
The result of the [`initiate_chats`](/docs/api-reference/autogen/ConversableAgent#initiate-chats) method returns a list of [`ChatResult`](/docs/api-reference/autogen/ChatResult) objects, one for each chat in the sequence. | ||
|
||
### Different senders | ||
|
||
In the above example, the teacher agent was the sender in each chat, however you can use the high-level [`initiate_chats`](/docs/api-reference/autogen/initiate_chats) function to start a sequence of two-agent chats with different sender agents. See this [notebook](/docs/use-cases/notebooks/notebooks/agentchats_sequential_chats#example-1-solve-tasks-with-a-series-of-chats) for an example. |
112 changes: 112 additions & 0 deletions
112
website/docs/user-guide/basic-concepts/orchestration/swarm.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
--- | ||
title: Swarm | ||
--- | ||
|
||
Swarms are a versatile pattern that provide flows between agents that are determined at the swarm or agent-level. The control of the flow is managed through various mechanisms, including hand-offs, post-tool transitions, post-work transitions, or an internal group chat manager, all of which determine the next agent (or end the swarm). | ||
|
||
The simplest swarm comprises of a group of agents and utilizes the swarm's internal manager (a [`GroupChatManager`](/docs/api-reference/autogen/GroupChatManager)) to decide, based on the conversation messages and the agent's descriptions, who should be the next agent. | ||
|
||
Here's an example. | ||
|
||
```python | ||
from autogen import ConversableAgent, AfterWorkOption, initiate_swarm_chat | ||
|
||
llm_config = {"api_type": "openai", "model": "gpt-4o-mini"} | ||
|
||
# 1. Create our agents | ||
planner_message = """You are a classroom lesson planner. | ||
Given a topic, write a lesson plan for a fourth grade class. | ||
If you are given revision feedback, update your lesson plan and record it. | ||
Use the following format: | ||
<title>Lesson plan title</title> | ||
<learning_objectives>Key learning objectives</learning_objectives> | ||
<script>How to introduce the topic to the kids</script> | ||
""" | ||
|
||
lesson_planner = ConversableAgent( | ||
name="planner_agent", llm_config=llm_config, system_message=planner_message | ||
) | ||
|
||
reviewer_message = """You are a classroom lesson reviewer. | ||
You compare the lesson plan to the fourth grade curriculum | ||
and provide a maximum of 3 recommended changes for each review. | ||
Make sure you provide recommendations each time the plan is updated. | ||
""" | ||
|
||
lesson_reviewer = ConversableAgent( | ||
name="reviewer_agent", llm_config=llm_config, system_message=reviewer_message | ||
) | ||
|
||
teacher_message = """You are a classroom teacher. | ||
You decide topics for lessons and work with a lesson planner. | ||
and reviewer to create and finalise lesson plans. | ||
""" | ||
|
||
teacher = ConversableAgent( | ||
name="teacher_agent", | ||
llm_config=llm_config, | ||
system_message=teacher_message, | ||
) | ||
|
||
# 2. Initiate the swarm chat using a swarm manager who will | ||
# select agents automatically | ||
result, _, _ = initiate_swarm_chat( | ||
initial_agent=teacher, | ||
agents=[lesson_planner, lesson_reviewer, teacher], | ||
messages="Today, let's introduce our kids to the solar system.", | ||
max_rounds=10, | ||
swarm_manager_args={"llm_config": llm_config}, | ||
after_work=AfterWorkOption.SWARM_MANAGER | ||
) | ||
``` | ||
|
||
In this case, we create three agents, each with a detailed system message that will aid our swarm manager in working out the next best agent. There will also be a user agent (you) that may be transitioned to if the swarm manager thinks it needs further information or has finished. | ||
|
||
Let's examine the code: | ||
|
||
1. Create our three agents | ||
|
||
2. Initiate the chat | ||
|
||
- All our agents are passed in to `agents` | ||
- Our starting message is set in `messages` | ||
- As we're using our swarm manager to select agents, we need to give them an LLM configuration, so we pass it in `swarm_manager_args` | ||
- When each agent has spoken it will utilise the swarm's [`AfterWork`](/docs/api-reference/autogen/AfterWork) setting, in this case [`AfterWorkOption.SWARM_MANAGER`](/docs/api-reference/autogen/AfterWorkOption) set with the `after_work` parameter, indicating that the swarm manager is responsible for selecting the next agent. | ||
|
||
In this example, the swarm manager's LLM is solely responsible for deciding the next agent at each turn. | ||
|
||
### Controlling swarm transitions | ||
|
||
To gain more control over the transitions between agents, utilise hand-offs and after work transitions. | ||
|
||
When designing your swarm, think about your agents in a diagram with the lines between agents being your hand-offs. Each line will have a condition statement which an LLM will evaluate. Control stays with an agent while they execute their tools and once they've finished with their tools the conditions to transition will be evaluated. | ||
|
||
One of the unique aspects of a swarm is a shared context. ConversableAgents have a context dictionary but in a swarm that context is made common across all agents, allowing a state of the workflow to be maintained and viewed by all agents. This context can also be used within the hand off condition statements, providing more control of transitions. | ||
|
||
AG2's swarm has a number of unique capabilities, find out more in our [Swarm deep-dive](/docs/user-guide/advanced-concepts/swarm-deep-dive). | ||
|
||
Here's our lesson planner workflow using AG2's Swarm. | ||
|
||
import Example from "/snippets/python-examples/swarm.mdx"; | ||
|
||
<Example/> | ||
|
||
|
||
1. Our shared context, available in function calls and on agents. | ||
|
||
2. Functions that represent the work the agents carry out, these the update shared context and, optionally, managed transitions. | ||
|
||
3. Agents setup with their tools, `functions`, and a system message and LLM configuration. | ||
|
||
4. The important hand-offs, defining the conditions for which to transfer to other agents and what to do after their work is finished (equivalent to no longer calling tools). Transfer conditions can be turned on/off using the `available` parameter. | ||
|
||
5. Kick off the swarm with our agents and shared context. Similar to `initiate_chat`, `initiate_swarm_chat` returns the chat result (messages and summary) and the final shared context. | ||
|
||
```console | ||
Number of reviews: 2 | ||
Reviews remaining: 0 | ||
Final Lesson Plan: | ||
<title>Exploring the Solar System</title> | ||
<learning_objectives>Students will be able to identify and describe the planets in the Solar System, understand the concept of orbits, and recognize the sun as the center of our Solar System.</learning_objectives> | ||
<script>Using a large poster of the Solar System, I will introduce the planets one by one, discussing key characteristics such as size, color, and distance from the sun. I will engage the students by asking questions about their favorite planets and what they know about them. We will do a quick demo using a simple model to show how planets orbit around the sun. Students will create their own solar system models in small groups with various materials to understand the scale and distance better, fostering teamwork. We will incorporate a multimedia presentation to visualize the orbits and relative sizes of the planets. Finally, a short assessment will be conducted at the end of the lesson to gauge students' understanding, using quizzes or presentations of their models.</script> | ||
``` |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test to pass the codecov CI.
Is
llm_config
always required when this method_create_internal_agents
is called?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ultimately, yes it requires a configuration as it creates an agent internally (so there's no mechanism here for a developer to configure that agent to respond any other way).
If the GroupChatManager doesn't have
llm_config
set or the GroupChat doesn't haveselect_speaker_auto_llm_config
set, it won't be able to use an LLM.What was happening was that when no LLM config was set, this internal agent was returning a blank reply and that was resulting in it just returning the next agent in the list. This was silent for the user so it looked like it was determining the next agent with an LLM but wasn't.