GraphFlow(工作流)#
在本节中,您将学习如何使用 GraphFlow
(简称"flow")创建_多智能体工作流_。
它采用结构化执行方式,精确控制智能体之间的交互以完成任务。
我们将首先展示如何创建和运行一个工作流。然后解释如何观察和调试工作流行为, 并讨论管理执行的重要操作。
AutoGen AgentChat 提供了面向有向图执行的团队:
备注
何时应该使用 GraphFlow
?
当您需要严格控制智能体执行顺序,或不同结果必须导向不同后续步骤时使用Graph。
如果临时对话流足够,可以从简单团队如 RoundRobinGroupChat
或 SelectorGroupChat
开始。
当任务需要确定性控制、条件分支或处理带循环的复杂多步骤流程时,再转向结构化工作流。
警告:
GraphFlow
是实验性功能。 其API、行为和功能在后续版本中可能发生变化。
创建和运行工作流#
DiGraphBuilder
是一个流畅的工具,可让您轻松构建工作流执行图。它支持构建:
顺序链
并行分支
条件分支
带安全退出条件的循环
图中的每个节点代表一个智能体,边定义了允许的执行路径。边可以选择性地基于智能体消息设置条件。
顺序工作流#
我们将从创建一个简单工作流开始,其中作者起草段落,审阅者提供反馈。该图在审阅者对作者评论后终止。
注意,工作流会自动计算图中的所有源节点和叶节点,执行从所有源节点开始,当没有剩余节点可执行时完成。
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import DiGraphBuilder, GraphFlow
from autogen_ext.models.openai import OpenAIChatCompletionClient
# 创建OpenAI模型客户端
client = OpenAIChatCompletionClient(model="gpt-4.1-nano")
# 创建作者智能体
writer = AssistantAgent("writer", model_client=client, system_message="Draft a short paragraph on climate change.")
# 创建评审员代理
reviewer = AssistantAgent("reviewer", model_client=client, system_message="Review the draft and suggest improvements.")
# 构建图结构
builder = DiGraphBuilder()
builder.add_node(writer).add_node(reviewer)
builder.add_edge(writer, reviewer)
# 构建并验证图结构
graph = builder.build()
# 创建流程
flow = GraphFlow([writer, reviewer], graph=graph)
# 在脚本中运行时,使用 `asyncio.run(...)` 并将以下内容包装在异步函数中。
stream = flow.run_stream(task="Write a short paragraph about climate change.")
async for event in stream: # type: ignore
print(event)
# 使用 Console(flow.run_stream(...)) 可在控制台获得更好的格式化效果。
source='user' models_usage=None metadata={} content='Write a short paragraph about climate change.' type='TextMessage'
source='writer' models_usage=RequestUsage(prompt_tokens=28, completion_tokens=95) metadata={} content='Climate change refers to long-term shifts in temperature, precipitation, and other atmospheric patterns, largely driven by human activities such as burning fossil fuels, deforestation, and industrial processes. These changes contribute to rising global temperatures, melting ice caps, more frequent and severe weather events, and adverse impacts on ecosystems and human communities. Addressing climate change requires global cooperation to reduce greenhouse gas emissions, transition to renewable energy sources, and implement sustainable practices to protect the planet for future generations.' type='TextMessage'
source='reviewer' models_usage=RequestUsage(prompt_tokens=127, completion_tokens=144) metadata={} content="The paragraph provides a clear overview of climate change, its causes, and its impacts. To enhance clarity and engagement, consider adding specific examples or emphasizing the urgency of action. Here's a revised version:\n\nClimate change is a long-term alteration of Earth's climate patterns caused primarily by human activities such as burning fossil fuels, deforestation, and industrial emissions. These actions increase greenhouse gases in the atmosphere, leading to rising global temperatures, melting ice caps, and more frequent extreme weather events like hurricanes and droughts. The effects threaten ecosystems, disrupt agriculture, and endanger communities worldwide. Addressing this crisis requires urgent, coordinated global efforts to reduce emissions, adopt renewable energy, and promote sustainable practices to safeguard the planet for future generations." type='TextMessage'
source='DiGraphStopAgent' models_usage=None metadata={} content='Digraph execution is complete' type='StopMessage'
messages=[TextMessage(source='user', models_usage=None, metadata={}, content='Write a short paragraph about climate change.', type='TextMessage'), TextMessage(source='writer', models_usage=RequestUsage(prompt_tokens=28, completion_tokens=95), metadata={}, content='Climate change refers to long-term shifts in temperature, precipitation, and other atmospheric patterns, largely driven by human activities such as burning fossil fuels, deforestation, and industrial processes. These changes contribute to rising global temperatures, melting ice caps, more frequent and severe weather events, and adverse impacts on ecosystems and human communities. Addressing climate change requires global cooperation to reduce greenhouse gas emissions, transition to renewable energy sources, and implement sustainable practices to protect the planet for future generations.', type='TextMessage'), TextMessage(source='reviewer', models_usage=RequestUsage(prompt_tokens=127, completion_tokens=144), metadata={}, content="The paragraph provides a clear overview of climate change, its causes, and its impacts. To enhance clarity and engagement, consider adding specific examples or emphasizing the urgency of action. Here's a revised version:\n\nClimate change is a long-term alteration of Earth's climate patterns caused primarily by human activities such as burning fossil fuels, deforestation, and industrial emissions. These actions increase greenhouse gases in the atmosphere, leading to rising global temperatures, melting ice caps, and more frequent extreme weather events like hurricanes and droughts. The effects threaten ecosystems, disrupt agriculture, and endanger communities worldwide. Addressing this crisis requires urgent, coordinated global efforts to reduce emissions, adopt renewable energy, and promote sustainable practices to safeguard the planet for future generations.", type='TextMessage'), StopMessage(source='DiGraphStopAgent', models_usage=None, metadata={}, content='Digraph execution is complete', type='StopMessage')] stop_reason='Stop message received'
并行流程与合并#
我们现在创建一个稍复杂的流程:
写手起草一个段落
两位编辑分别进行语法和风格修改(并行分支)
终审整合他们的修改(合并)
执行从写手开始,同时分发给编辑1和编辑2,然后两者都输入到终审环节。
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import DiGraphBuilder, GraphFlow
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
# 创建 OpenAI 模型客户端
client = OpenAIChatCompletionClient(model="gpt-4.1-nano")
# 创建写手代理
writer = AssistantAgent("writer", model_client=client, system_message="Draft a short paragraph on climate change.")
# 创建两个编辑代理
editor1 = AssistantAgent("editor1", model_client=client, system_message="Edit the paragraph for grammar.")
editor2 = AssistantAgent("editor2", model_client=client, system_message="Edit the paragraph for style.")
# 创建最终审阅代理
final_reviewer = AssistantAgent(
"final_reviewer",
model_client=client,
system_message="Consolidate the grammar and style edits into a final version.",
)
# 构建工作流图
builder = DiGraphBuilder()
builder.add_node(writer).add_node(editor1).add_node(editor2).add_node(final_reviewer)
# 从写手到编辑1和编辑2的分发
builder.add_edge(writer, editor1)
builder.add_edge(writer, editor2)
# 将两个编辑汇聚到最终审阅
builder.add_edge(editor1, final_reviewer)
builder.add_edge(editor2, final_reviewer)
# 构建并验证图结构
graph = builder.build()
# 创建流程
flow = GraphFlow(
participants=builder.get_participants(),
graph=graph,
)
# 运行工作流
await Console(flow.run_stream(task="Write a short paragraph about climate change."))
---------- TextMessage (user) ----------
Write a short paragraph about climate change.
---------- TextMessage (writer) ----------
Climate change refers to long-term shifts in weather patterns and global temperatures, largely driven by human activities such as burning fossil fuels, deforestation, and industrial processes. These activities increase concentrations of greenhouse gases like carbon dioxide and methane in the atmosphere, leading to global warming. The impacts of climate change include more frequent and severe weather events, rising sea levels, melting glaciers, and disruptions to ecosystems and agriculture. Addressing this urgent issue requires international cooperation, significant shifts toward renewable energy sources, and sustainable practices to reduce our carbon footprint and protect the planet for future generations.
---------- TextMessage (editor1) ----------
Climate change refers to long-term shifts in weather patterns and global temperatures, largely driven by human activities such as burning fossil fuels, deforestation, and industrial processes. These activities increase concentrations of greenhouse gases like carbon dioxide and methane in the atmosphere, leading to global warming. The impacts of climate change include more frequent and severe weather events, rising sea levels, melting glaciers, and disruptions to ecosystems and agriculture. Addressing this urgent issue requires international cooperation, significant shifts toward renewable energy sources, and sustainable practices to reduce our carbon footprint and protect the planet for future generations.
---------- TextMessage (editor2) ----------
Climate change involves long-term alterations in weather patterns and global temperatures, primarily caused by human activities like burning fossil fuels, deforestation, and industrial processes. These actions elevate levels of greenhouse gases such as carbon dioxide and methane, resulting in global warming. Its consequences are widespread, including more frequent and intense storms, rising sea levels, melting glaciers, and disturbances to ecosystems and agriculture. Combating this crisis demands international collaboration, a swift transition to renewable energy, and sustainable practices to cut carbon emissions, ensuring a healthier planet for future generations.
---------- TextMessage (final_reviewer) ----------
Climate change involves long-term alterations in weather patterns and global temperatures, primarily caused by human activities such as burning fossil fuels, deforestation, and industrial processes. These actions increase levels of greenhouse gases like carbon dioxide and methane, leading to global warming. Its consequences include more frequent and intense storms, rising sea levels, melting glaciers, and disruptions to ecosystems and agriculture. Addressing this crisis requires international collaboration, a swift transition to renewable energy, and sustainable practices to reduce carbon emissions, ensuring a healthier planet for future generations.
---------- StopMessage (DiGraphStopAgent) ----------
Digraph execution is complete
TaskResult(messages=[TextMessage(source='user', models_usage=None, metadata={}, content='Write a short paragraph about climate change.', type='TextMessage'), TextMessage(source='writer', models_usage=RequestUsage(prompt_tokens=28, completion_tokens=113), metadata={}, content='Climate change refers to long-term shifts in weather patterns and global temperatures, largely driven by human activities such as burning fossil fuels, deforestation, and industrial processes. These activities increase concentrations of greenhouse gases like carbon dioxide and methane in the atmosphere, leading to global warming. The impacts of climate change include more frequent and severe weather events, rising sea levels, melting glaciers, and disruptions to ecosystems and agriculture. Addressing this urgent issue requires international cooperation, significant shifts toward renewable energy sources, and sustainable practices to reduce our carbon footprint and protect the planet for future generations.', type='TextMessage'), TextMessage(source='editor1', models_usage=RequestUsage(prompt_tokens=144, completion_tokens=113), metadata={}, content='Climate change refers to long-term shifts in weather patterns and global temperatures, largely driven by human activities such as burning fossil fuels, deforestation, and industrial processes. These activities increase concentrations of greenhouse gases like carbon dioxide and methane in the atmosphere, leading to global warming. The impacts of climate change include more frequent and severe weather events, rising sea levels, melting glaciers, and disruptions to ecosystems and agriculture. Addressing this urgent issue requires international cooperation, significant shifts toward renewable energy sources, and sustainable practices to reduce our carbon footprint and protect the planet for future generations.', type='TextMessage'), TextMessage(source='editor2', models_usage=RequestUsage(prompt_tokens=263, completion_tokens=107), metadata={}, content='Climate change involves long-term alterations in weather patterns and global temperatures, primarily caused by human activities like burning fossil fuels, deforestation, and industrial processes. These actions elevate levels of greenhouse gases such as carbon dioxide and methane, resulting in global warming. Its consequences are widespread, including more frequent and intense storms, rising sea levels, melting glaciers, and disturbances to ecosystems and agriculture. Combating this crisis demands international collaboration, a swift transition to renewable energy, and sustainable practices to cut carbon emissions, ensuring a healthier planet for future generations.', type='TextMessage'), TextMessage(source='final_reviewer', models_usage=RequestUsage(prompt_tokens=383, completion_tokens=104), metadata={}, content='Climate change involves long-term alterations in weather patterns and global temperatures, primarily caused by human activities such as burning fossil fuels, deforestation, and industrial processes. These actions increase levels of greenhouse gases like carbon dioxide and methane, leading to global warming. Its consequences include more frequent and intense storms, rising sea levels, melting glaciers, and disruptions to ecosystems and agriculture. Addressing this crisis requires international collaboration, a swift transition to renewable energy, and sustainable practices to reduce carbon emissions, ensuring a healthier planet for future generations.', type='TextMessage'), StopMessage(source='DiGraphStopAgent', models_usage=None, metadata={}, content='Digraph execution is complete', type='StopMessage')], stop_reason='Stop message received')
消息过滤#
执行图 vs 消息图#
在 GraphFlow
中,执行图使用
DiGraph
定义,用于控制代理的执行顺序。
但执行图并不控制代理从其他代理接收哪些消息。
默认情况下,所有消息都会被发送给图中的所有代理。
消息过滤是一个独立功能,允许你过滤 每个代理接收的消息,将其模型上下文限制为仅相关信息。 消息过滤器集合定义了流程中的消息图。
指定消息图有助于:
减少幻觉
控制内存负载
让代理只关注相关信息
你可以结合使用 MessageFilterAgent
、MessageFilterConfig
和 PerSourceFilter
来定义这些规则。
from autogen_agentchat.agents import AssistantAgent, MessageFilterAgent, MessageFilterConfig, PerSourceFilter
from autogen_agentchat.teams import DiGraphBuilder, GraphFlow
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
# 模型客户端
client = OpenAIChatCompletionClient(model="gpt-4.1-nano")
# 创建代理
researcher = AssistantAgent(
"researcher", model_client=client, system_message="Summarize key facts about climate change."
)
analyst = AssistantAgent("analyst", model_client=client, system_message="Review the summary and suggest improvements.")
presenter = AssistantAgent(
"presenter", model_client=client, system_message="Prepare a presentation slide based on the final summary."
)
# 应用消息过滤
filtered_analyst = MessageFilterAgent(
name="analyst",
wrapped_agent=analyst,
filter=MessageFilterConfig(per_source=[PerSourceFilter(source="researcher", position="last", count=1)]),
)
filtered_presenter = MessageFilterAgent(
name="presenter",
wrapped_agent=presenter,
filter=MessageFilterConfig(per_source=[PerSourceFilter(source="analyst", position="last", count=1)]),
)
# 构建流程
builder = DiGraphBuilder()
builder.add_node(researcher).add_node(filtered_analyst).add_node(filtered_presenter)
builder.add_edge(researcher, filtered_analyst).add_edge(filtered_analyst, filtered_presenter)
# 创建流程
flow = GraphFlow(
participants=builder.get_participants(),
graph=builder.build(),
)
# 运行流程
await Console(flow.run_stream(task="Summarize key facts about climate change."))
---------- TextMessage (user) ----------
Summarize key facts about climate change.
---------- TextMessage (researcher) ----------
Certainly! Here are some key facts about climate change:
1. **Global Warming**: Earth's average surface temperature has increased significantly over the past century, primarily due to human activities.
2. **Greenhouse Gas Emissions**: The main contributors are carbon dioxide (CO₂), methane (CH₄), and nitrous oxide (N₂O), resulting from burning fossil fuels, deforestation, and industrial processes.
3. **Impacts on Weather and Climate**: Climate change leads to more frequent and severe heatwaves, storms, droughts, and heavy rainfall.
4. **Rising Sea Levels**: Melting polar ice caps and glaciers, along with thermal expansion of seawater, are causing sea levels to rise.
5. **Effects on Ecosystems**: Altered habitats threaten plant and animal species, leading to biodiversity loss.
6. **Human Health and Societies**: Climate change contributes to health issues, food and water insecurity, and displacement of populations.
7. **Global Response**: International efforts like the Paris Agreement aim to limit temperature rise, promote renewable energy, and reduce emissions.
8. **Urgency**: Addressing climate change requires immediate, concerted actions to mitigate further damage and adapt to changes.
Let me know if you want more detailed information on any of these points!
---------- TextMessage (analyst) ----------
Your summary effectively covers the fundamental aspects of climate change and presents them clearly. Here are some suggestions to improve clarity, depth, and engagement:
1. Enhance structure with subheadings: Organize points into thematic sections (e.g., Causes, Effects, Responses) for easier navigation.
2. Add recent context or data: Incorporate the latest statistics or notable recent events to emphasize urgency.
3. Emphasize solutions: Briefly mention specific mitigation and adaptation strategies beyond international agreements.
4. Use more precise language: For example, specify the amount of temperature increase globally (~1.2°C since pre-industrial times).
5. Incorporate the importance of individual actions: Highlight how personal choices contribute to climate efforts.
6. Mention climate feedback loops: Briefly note how certain effects (like melting ice) can accelerate warming.
**Improved Version:**
---
**Overview of Climate Change**
**Causes:**
- Human activities, especially burning fossil fuels, deforestation, and industrial processes, have led to increased concentrations of greenhouse gases such as carbon dioxide (CO₂), methane (CH₄), and nitrous oxide (N₂O).
- Since the late 19th century, Earth's average surface temperature has risen by approximately 1.2°C, with the past decade being the warmest on record.
**Impacts:**
- The changing climate causes more frequent and intense heatwaves, storms, droughts, and heavy rainfall events.
- Melting polar ice caps and glaciers, along with thermal expansion, are raising sea levels, threatening coastal communities.
- Ecosystems are shifting, leading to habitat loss and risking biodiversity, with some species facing extinction.
- Human health and societies are affected through increased heat-related illnesses, food and water insecurity, and displacement due to extreme weather events.
**Global Response and Solutions:**
- International agreements like the Paris Agreement aim to limit global temperature rise well below 2°C.
- Strategies include transitioning to renewable energy sources, increasing energy efficiency, reforestation, and sustainable land use.
- Community and individual actions—reducing carbon footprints, supporting sustainable policies, and raising awareness—are essential components.
**Urgency and Call to Action:**
- Immediate, coordinated efforts are critical to mitigate irreversible damage and adapt to ongoing changes.
- Every sector, from government to individual, has a role to play in creating a sustainable future.
---
Let me know if you'd like a more detailed explanation of any section or additional statistical data!
---------- TextMessage (presenter) ----------
**Slide Title:**
**Climate Change: Causes, Impacts & Solutions**
**Causes:**
- Emissions from burning fossil fuels, deforestation, industrial activities
- Greenhouse gases (CO₂, CH₄, N₂O) have increased significantly
- Global temperature has risen by ~1.2°C since pre-industrial times
**Impacts:**
- More frequent heatwaves, storms, droughts, and heavy rainfall
- Melting ice caps and rising sea levels threaten coastal areas
- Habitat loss and decreased biodiversity
- Health risks and societal disruptions
**Responses & Solutions:**
- International efforts like the Paris Agreement aim to limit warming
- Transitioning to renewable energy, energy efficiency, reforestation
- Community and individual actions: reducing carbon footprints and raising awareness
**Urgency:**
- Immediate, coordinated action is essential to prevent irreversible damage
- Everyone has a role in building a sustainable future
---------- StopMessage (DiGraphStopAgent) ----------
Digraph execution is complete
TaskResult(messages=[TextMessage(source='user', models_usage=None, metadata={}, content='Summarize key facts about climate change.', type='TextMessage'), TextMessage(source='researcher', models_usage=RequestUsage(prompt_tokens=30, completion_tokens=267), metadata={}, content="Certainly! Here are some key facts about climate change:\n\n1. **Global Warming**: Earth's average surface temperature has increased significantly over the past century, primarily due to human activities.\n2. **Greenhouse Gas Emissions**: The main contributors are carbon dioxide (CO₂), methane (CH₄), and nitrous oxide (N₂O), resulting from burning fossil fuels, deforestation, and industrial processes.\n3. **Impacts on Weather and Climate**: Climate change leads to more frequent and severe heatwaves, storms, droughts, and heavy rainfall.\n4. **Rising Sea Levels**: Melting polar ice caps and glaciers, along with thermal expansion of seawater, are causing sea levels to rise.\n5. **Effects on Ecosystems**: Altered habitats threaten plant and animal species, leading to biodiversity loss.\n6. **Human Health and Societies**: Climate change contributes to health issues, food and water insecurity, and displacement of populations.\n7. **Global Response**: International efforts like the Paris Agreement aim to limit temperature rise, promote renewable energy, and reduce emissions.\n8. **Urgency**: Addressing climate change requires immediate, concerted actions to mitigate further damage and adapt to changes.\n\nLet me know if you want more detailed information on any of these points!", type='TextMessage'), TextMessage(source='analyst', models_usage=RequestUsage(prompt_tokens=287, completion_tokens=498), metadata={}, content="Your summary effectively covers the fundamental aspects of climate change and presents them clearly. Here are some suggestions to improve clarity, depth, and engagement:\n\n1. Enhance structure with subheadings: Organize points into thematic sections (e.g., Causes, Effects, Responses) for easier navigation.\n2. Add recent context or data: Incorporate the latest statistics or notable recent events to emphasize urgency.\n3. Emphasize solutions: Briefly mention specific mitigation and adaptation strategies beyond international agreements.\n4. Use more precise language: For example, specify the amount of temperature increase globally (~1.2°C since pre-industrial times).\n5. Incorporate the importance of individual actions: Highlight how personal choices contribute to climate efforts.\n6. Mention climate feedback loops: Briefly note how certain effects (like melting ice) can accelerate warming.\n\n**Improved Version:**\n\n---\n\n**Overview of Climate Change**\n\n**Causes:**\n- Human activities, especially burning fossil fuels, deforestation, and industrial processes, have led to increased concentrations of greenhouse gases such as carbon dioxide (CO₂), methane (CH₄), and nitrous oxide (N₂O).\n- Since the late 19th century, Earth's average surface temperature has risen by approximately 1.2°C, with the past decade being the warmest on record.\n\n**Impacts:**\n- The changing climate causes more frequent and intense heatwaves, storms, droughts, and heavy rainfall events.\n- Melting polar ice caps and glaciers, along with thermal expansion, are raising sea levels, threatening coastal communities.\n- Ecosystems are shifting, leading to habitat loss and risking biodiversity, with some species facing extinction.\n- Human health and societies are affected through increased heat-related illnesses, food and water insecurity, and displacement due to extreme weather events.\n\n**Global Response and Solutions:**\n- International agreements like the Paris Agreement aim to limit global temperature rise well below 2°C.\n- Strategies include transitioning to renewable energy sources, increasing energy efficiency, reforestation, and sustainable land use.\n- Community and individual actions—reducing carbon footprints, supporting sustainable policies, and raising awareness—are essential components.\n\n**Urgency and Call to Action:**\n- Immediate, coordinated efforts are critical to mitigate irreversible damage and adapt to ongoing changes.\n- Every sector, from government to individual, has a role to play in creating a sustainable future.\n\n---\n\nLet me know if you'd like a more detailed explanation of any section or additional statistical data!", type='TextMessage'), TextMessage(source='presenter', models_usage=RequestUsage(prompt_tokens=521, completion_tokens=192), metadata={}, content='**Slide Title:** \n**Climate Change: Causes, Impacts & Solutions**\n\n**Causes:** \n- Emissions from burning fossil fuels, deforestation, industrial activities \n- Greenhouse gases (CO₂, CH₄, N₂O) have increased significantly \n- Global temperature has risen by ~1.2°C since pre-industrial times \n\n**Impacts:** \n- More frequent heatwaves, storms, droughts, and heavy rainfall \n- Melting ice caps and rising sea levels threaten coastal areas \n- Habitat loss and decreased biodiversity \n- Health risks and societal disruptions \n\n**Responses & Solutions:** \n- International efforts like the Paris Agreement aim to limit warming \n- Transitioning to renewable energy, energy efficiency, reforestation \n- Community and individual actions: reducing carbon footprints and raising awareness \n\n**Urgency:** \n- Immediate, coordinated action is essential to prevent irreversible damage \n- Everyone has a role in building a sustainable future', type='TextMessage'), StopMessage(source='DiGraphStopAgent', models_usage=None, metadata={}, content='Digraph execution is complete', type='StopMessage')], stop_reason='Stop message received')
🔁 高级示例:条件循环 + 过滤摘要#
本示例演示:
生成器与评审器之间的循环(当评审器说"APPROVE"时退出)
一个仅能看到首个用户输入和最后评审消息的摘要代理
from autogen_agentchat.agents import AssistantAgent, MessageFilterAgent, MessageFilterConfig, PerSourceFilter
from autogen_agentchat.teams import (
DiGraphBuilder,
GraphFlow,
)
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
model_client = OpenAIChatCompletionClient(model="gpt-4o-mini")
# 代理
generator = AssistantAgent("generator", model_client=model_client, system_message="Generate a list of creative ideas.")
reviewer = AssistantAgent(
"reviewer",
model_client=model_client,
system_message="Review ideas and say 'REVISE' and provide feedbacks, or 'APPROVE' for final approval.",
)
summarizer_core = AssistantAgent(
"summary", model_client=model_client, system_message="Summarize the user request and the final feedback."
)
# 过滤摘要器
filtered_summarizer = MessageFilterAgent(
name="summary",
wrapped_agent=summarizer_core,
filter=MessageFilterConfig(
per_source=[
PerSourceFilter(source="user", position="first", count=1),
PerSourceFilter(source="reviewer", position="last", count=1),
]
),
)
# 构建带条件循环的图
builder = DiGraphBuilder()
builder.add_node(generator).add_node(reviewer).add_node(filtered_summarizer)
builder.add_edge(generator, reviewer)
builder.add_edge(reviewer, generator, condition="REVISE")
builder.add_edge(reviewer, filtered_summarizer, condition="APPROVE")
builder.set_entry_point(generator) # 将入口点设为生成器。若无源节点则必须设置。
graph = builder.build()
# 创建流程
flow = GraphFlow(
participants=builder.get_participants(),
graph=graph,
)
# 运行流程并在控制台中漂亮地打印输出
await Console(flow.run_stream(task="Brainstorm ways to reduce plastic waste."))
---------- TextMessage (user) ----------
Brainstorm ways to reduce plastic waste.
---------- TextMessage (generator) ----------
1. **Reusable Packaging Innovations**: Develop a subscription service that provides reusable containers for household products (like detergents, shampoos, etc.) which can be returned for refills.
2. **Biodegradable Alternatives**: Invest in research for materials that mimic plastic but break down more easily, such as mushroom mycelium, seaweed, or plant-based polymers.
3. **Community Swap Events**: Organize local events where people can swap items they no longer need, reducing the need to buy new plastic-containing products.
4. **Educational Workshops**: Host workshops in schools and communities that educate participants about the impacts of plastic waste and how to reduce it in their daily lives.
5. **Plastic Waste Upcycling**: Encourage creativity with workshops that teach people how to make art or useful items from plastic waste, such as furniture, decor, or gardening tools.
6. **Plastic-Free Challenges**: Create month-long challenges that encourage individuals and families to reduce plastic use, sharing their progress on social media to inspire others.
7. **Refill Stations**: Install public refill stations for water, cleaning supplies, and personal care products in high-footfall areas like gyms, parks, and shopping centers.
8. **Corporate Partnerships**: Collaborate with businesses to create incentives for customers to bring their own containers, such as discounts at coffee shops or grocery stores.
9. **Digital Packaging**: Explore digital solutions like smart packaging that provides all necessary product information via QR codes, reducing the need for physical packaging.
10. **Government Legislation Advocates**: Advocate for local governments to impose bans on single-use plastics and support policies that promote sustainable materials.
11. **Plastic Waste Art Competitions**: Organize contests that challenge artists to create impactful pieces from plastic waste to raise awareness and promote recycling.
12. **Sustainable School Programs**: Integrate sustainability topics within school curricula and promote student-led initiatives to reduce plastic within their institutions.
13. **Eco-Friendly Vending Machines**: Create vending machines that dispense snacks and drinks in reusable containers, encouraging customers to return them after use.
14. **Gardening Kits with Recyclable Components**: Supply gardening starter kits that include materials like biodegradable pots and natural fertilizers, promoting a zero-waste gardening approach.
15. **DIY Personal Care Products**: Develop and disseminate DIY recipes for personal care products, such as soaps and lotions that encourage the use of bulk ingredients instead of plastic-packaged goods.
16. **Plastic Neutrality Programs**: Encourage businesses to adopt plastic neutrality programs where they balance out their plastic usage by supporting clean-up or recycling initiatives.
17. **Plastic-Free Grocery Stores**: Promote or support grocery stores that operate exclusively without plastic, offering bulk options, glass containers, or other eco-friendly packaging.
18. **Innovative Composting Solutions**: Design composting systems that make it easy for households to compost food and biodegradable materials, reducing plastic packaging associated with food waste.
19. **Mobile Apps for Plastic Waste Tracking**: Create an app that helps users track their plastic consumption and offers tips for reducing it, while also rewarding users for achievements.
20. **Public Awareness Campaigns**: Launch a campaign that creatively highlights the environmental impact of plastic waste through social media challenges and community engagement.
---------- TextMessage (reviewer) ----------
REVISE
This is a comprehensive and creative list of ideas to reduce plastic waste. Here are a few feedback points to enhance the suggestions further:
1. **Reusable Packaging Innovations**: Consider adding partnerships with local retailers to encourage participation and facilitate the logistics of returning and refilling containers.
2. **Biodegradable Alternatives**: It may be useful to specify how these materials would be incorporated into existing supply chains or product lines to make the transition smoother for businesses.
3. **Community Swap Events**: Suggest including a digital platform for participants who cannot attend events to swap items online, thereby broadening the reach.
4. **Educational Workshops**: Incorporate activities or hands-on experiences to engage participants better and make the learning process more memorable.
5. **Plastic Waste Upcycling**: Perhaps develop a certification process for workshops to promote quality and attract a wider audience.
6. **Plastic-Free Challenges**: To enhance engagement, consider partnering with influencers or local businesses that could sponsor awards or prizes for participants.
7. **Refill Stations**: Investigate the feasibility and potential locations for such stations to ensure they are accessible to the public.
8. **Corporate Partnerships**: It would be beneficial to specify the types of businesses you think would be most effective partners (e.g., cafes, grocery stores).
9. **Digital Packaging**: Elaborate on the potential barriers to this approach, such as consumer access to smartphones, to provide a well-rounded view.
10. **Government Legislation Advocates**: Suggest specific legislative measures that could be advocated for, making this point more actionable.
11. **Plastic Waste Art Competitions**: Promote collaboration with local art communities or schools to engage a wider audience and increase participation.
12. **Sustainable School Programs**: Propose ways to incentivize schools to participate, perhaps through competitive grants or recognition programs.
13. **Eco-Friendly Vending Machines**: Explore potential partnerships with local food vendors to ensure a diverse and appealing selection of products.
14. **Gardening Kits with Recyclable Components**: Consider the potential market and audience for these kits, such as urban dwellers or schools, to tailor marketing strategies.
15. **DIY Personal Care Products**: Include suggestions for workshops where participants can create these products collectively, fostering community and sharing knowledge.
16. **Plastic Neutrality Programs**: Define what criteria businesses would need to meet to qualify for such a program to encourage transparent efforts.
17. **Plastic-Free Grocery Stores**: Highlight examples of existing stores operating without plastic to serve as inspiration for future initiatives.
18. **Innovative Composting Solutions**: Provide examples of successful composting programs from other regions or communities to illustrate potential implementation.
19. **Mobile Apps for Plastic Waste Tracking**: Suggest features that would make the app engaging, such as gamification, social sharing, or integration with sustainability rewards programs.
20. **Public Awareness Campaigns**: Recommend specific themes or creative approaches that could resonate with the target audience for these campaigns.
These revisions will enhance clarity, practicality, and engagement, making the ideas more actionable and impactful.
---------- TextMessage (generator) ----------
Thank you for the feedback! Here's a revised version of the list of ideas to reduce plastic waste, incorporating your suggestions for enhancement:
1. **Reusable Packaging Innovations**: Partner with local retailers to establish a robust logistics system for returning and refilling containers, ensuring widespread participation and convenience for consumers.
2. **Biodegradable Alternatives**: Identify specific strategies for incorporating biodegradable materials into existing supply chains or product lines, facilitating a smoother transition for businesses to adopt eco-friendly options.
3. **Community Swap Events**: Create a digital platform allowing individuals to swap items online, engaging those who cannot attend physical events and expanding the initiative's reach.
4. **Educational Workshops**: Integrate hands-on experiences and interactive activities to keep participants engaged and enhance retention of knowledge about reducing plastic use.
5. **Plastic Waste Upcycling**: Develop a certification process for upcycling workshops, which would promote quality standards and attract a broader audience interested in sustainable crafts.
6. **Plastic-Free Challenges**: Collaborate with local influencers or businesses to sponsor awards or prizes for challenge participants, boosting engagement and motivation.
7. **Refill Stations**: Conduct feasibility studies to identify optimal locations for refill stations (e.g., busy urban areas, shopping centers) to ensure they are accessible to the public.
8. **Corporate Partnerships**: Target specific types of businesses as partners, such as cafes, grocery stores, and retailers, that naturally align with the initiative to promote reusable and sustainable practices.
9. **Digital Packaging**: Address potential barriers to adopting digital packaging solutions, like smartphone accessibility, to present a comprehensive view of the concept's practicality.
10. **Government Legislation Advocates**: Suggest specific legislative measures to advocate for, such as bans on single-use plastics or tax incentives for companies that commit to sustainable practices, making advocacy more actionable.
11. **Plastic Waste Art Competitions**: Collaborate with local art communities or educational institutions to organize competitions that engage a wider audience and encourage creativity while raising awareness.
12. **Sustainable School Programs**: Propose competitive grants or recognition programs to incentivize schools to adopt sustainable practices and participate in environmental initiatives.
13. **Eco-Friendly Vending Machines**: Explore partnerships with local food vendors to offer a diverse selection of products in vending machines that prioritize sustainability and convenience.
14. **Gardening Kits with Recyclable Components**: Research market segments (like urban dwellers or schools) to tailor gardening kits effectively, ensuring relevance and demand in marketing strategies.
15. **DIY Personal Care Products**: Host community workshops where participants can collaboratively create DIY products, fostering a sense of community while promoting zero waste practices.
16. **Plastic Neutrality Programs**: Define criteria that businesses must meet to qualify for plastic neutrality, ensuring transparency and encouraging meaningful participation in sustainability efforts.
17. **Plastic-Free Grocery Stores**: Highlight successful case studies of existing plastic-free grocery stores or markets to serve as models for new initiatives.
18. **Innovative Composting Solutions**: Provide examples of successful composting initiatives from other communities to illustrate best practices and potential implementations.
19. **Mobile Apps for Plastic Waste Tracking**: Include engaging features like gamification, social sharing capabilities, and integration with local sustainability rewards programs to enhance user experience.
20. **Public Awareness Campaigns**: Recommend specific themes (e.g., ocean conservation, wildlife protection) or creative approaches (like storytelling or humor) to resonate strongly with target audiences in awareness campaigns.
These revisions aim to enhance clarity, practicality, and engagement, making the ideas more actionable and impactful for addressing plastic waste reduction.
---------- TextMessage (reviewer) ----------
APPROVE
This revised list is well-thought-out and addresses the feedback effectively. Each suggestion is clearer, more practical, and focused on engagement and implementation. You've introduced specific strategies, collaborative elements, and examples that can help elevate the initiatives. The points are actionable and demonstrate a comprehensive approach to tackling plastic waste. Well done!
---------- TextMessage (summary) ----------
The user requested ideas for reducing plastic waste. The final feedback expressed approval of a revised list of suggestions, highlighting that it effectively addressed the initially requested feedback. The reviewer praised the clarity, practicality, and comprehensiveness of the strategies, noting that they included actionable points and collaborative elements to enhance implementation.
---------- StopMessage (DiGraphStopAgent) ----------
Digraph execution is complete
TaskResult(messages=[TextMessage(source='user', models_usage=None, metadata={}, content='Brainstorm ways to reduce plastic waste.', type='TextMessage'), TextMessage(source='generator', models_usage=RequestUsage(prompt_tokens=27, completion_tokens=661), metadata={}, content='1. **Reusable Packaging Innovations**: Develop a subscription service that provides reusable containers for household products (like detergents, shampoos, etc.) which can be returned for refills.\n\n2. **Biodegradable Alternatives**: Invest in research for materials that mimic plastic but break down more easily, such as mushroom mycelium, seaweed, or plant-based polymers.\n\n3. **Community Swap Events**: Organize local events where people can swap items they no longer need, reducing the need to buy new plastic-containing products.\n\n4. **Educational Workshops**: Host workshops in schools and communities that educate participants about the impacts of plastic waste and how to reduce it in their daily lives.\n\n5. **Plastic Waste Upcycling**: Encourage creativity with workshops that teach people how to make art or useful items from plastic waste, such as furniture, decor, or gardening tools.\n\n6. **Plastic-Free Challenges**: Create month-long challenges that encourage individuals and families to reduce plastic use, sharing their progress on social media to inspire others.\n\n7. **Refill Stations**: Install public refill stations for water, cleaning supplies, and personal care products in high-footfall areas like gyms, parks, and shopping centers.\n\n8. **Corporate Partnerships**: Collaborate with businesses to create incentives for customers to bring their own containers, such as discounts at coffee shops or grocery stores.\n\n9. **Digital Packaging**: Explore digital solutions like smart packaging that provides all necessary product information via QR codes, reducing the need for physical packaging.\n\n10. **Government Legislation Advocates**: Advocate for local governments to impose bans on single-use plastics and support policies that promote sustainable materials.\n\n11. **Plastic Waste Art Competitions**: Organize contests that challenge artists to create impactful pieces from plastic waste to raise awareness and promote recycling.\n\n12. **Sustainable School Programs**: Integrate sustainability topics within school curricula and promote student-led initiatives to reduce plastic within their institutions.\n\n13. **Eco-Friendly Vending Machines**: Create vending machines that dispense snacks and drinks in reusable containers, encouraging customers to return them after use.\n\n14. **Gardening Kits with Recyclable Components**: Supply gardening starter kits that include materials like biodegradable pots and natural fertilizers, promoting a zero-waste gardening approach.\n\n15. **DIY Personal Care Products**: Develop and disseminate DIY recipes for personal care products, such as soaps and lotions that encourage the use of bulk ingredients instead of plastic-packaged goods.\n\n16. **Plastic Neutrality Programs**: Encourage businesses to adopt plastic neutrality programs where they balance out their plastic usage by supporting clean-up or recycling initiatives.\n\n17. **Plastic-Free Grocery Stores**: Promote or support grocery stores that operate exclusively without plastic, offering bulk options, glass containers, or other eco-friendly packaging.\n\n18. **Innovative Composting Solutions**: Design composting systems that make it easy for households to compost food and biodegradable materials, reducing plastic packaging associated with food waste.\n\n19. **Mobile Apps for Plastic Waste Tracking**: Create an app that helps users track their plastic consumption and offers tips for reducing it, while also rewarding users for achievements.\n\n20. **Public Awareness Campaigns**: Launch a campaign that creatively highlights the environmental impact of plastic waste through social media challenges and community engagement.', type='TextMessage'), TextMessage(source='reviewer', models_usage=RequestUsage(prompt_tokens=710, completion_tokens=625), metadata={}, content='REVISE\n\nThis is a comprehensive and creative list of ideas to reduce plastic waste. Here are a few feedback points to enhance the suggestions further:\n\n1. **Reusable Packaging Innovations**: Consider adding partnerships with local retailers to encourage participation and facilitate the logistics of returning and refilling containers.\n\n2. **Biodegradable Alternatives**: It may be useful to specify how these materials would be incorporated into existing supply chains or product lines to make the transition smoother for businesses.\n\n3. **Community Swap Events**: Suggest including a digital platform for participants who cannot attend events to swap items online, thereby broadening the reach.\n\n4. **Educational Workshops**: Incorporate activities or hands-on experiences to engage participants better and make the learning process more memorable.\n\n5. **Plastic Waste Upcycling**: Perhaps develop a certification process for workshops to promote quality and attract a wider audience.\n\n6. **Plastic-Free Challenges**: To enhance engagement, consider partnering with influencers or local businesses that could sponsor awards or prizes for participants.\n\n7. **Refill Stations**: Investigate the feasibility and potential locations for such stations to ensure they are accessible to the public.\n\n8. **Corporate Partnerships**: It would be beneficial to specify the types of businesses you think would be most effective partners (e.g., cafes, grocery stores).\n\n9. **Digital Packaging**: Elaborate on the potential barriers to this approach, such as consumer access to smartphones, to provide a well-rounded view.\n\n10. **Government Legislation Advocates**: Suggest specific legislative measures that could be advocated for, making this point more actionable.\n\n11. **Plastic Waste Art Competitions**: Promote collaboration with local art communities or schools to engage a wider audience and increase participation.\n\n12. **Sustainable School Programs**: Propose ways to incentivize schools to participate, perhaps through competitive grants or recognition programs.\n\n13. **Eco-Friendly Vending Machines**: Explore potential partnerships with local food vendors to ensure a diverse and appealing selection of products.\n\n14. **Gardening Kits with Recyclable Components**: Consider the potential market and audience for these kits, such as urban dwellers or schools, to tailor marketing strategies.\n\n15. **DIY Personal Care Products**: Include suggestions for workshops where participants can create these products collectively, fostering community and sharing knowledge.\n\n16. **Plastic Neutrality Programs**: Define what criteria businesses would need to meet to qualify for such a program to encourage transparent efforts.\n\n17. **Plastic-Free Grocery Stores**: Highlight examples of existing stores operating without plastic to serve as inspiration for future initiatives.\n\n18. **Innovative Composting Solutions**: Provide examples of successful composting programs from other regions or communities to illustrate potential implementation.\n\n19. **Mobile Apps for Plastic Waste Tracking**: Suggest features that would make the app engaging, such as gamification, social sharing, or integration with sustainability rewards programs.\n\n20. **Public Awareness Campaigns**: Recommend specific themes or creative approaches that could resonate with the target audience for these campaigns.\n\nThese revisions will enhance clarity, practicality, and engagement, making the ideas more actionable and impactful.', type='TextMessage'), TextMessage(source='generator', models_usage=RequestUsage(prompt_tokens=1322, completion_tokens=721), metadata={}, content="Thank you for the feedback! Here's a revised version of the list of ideas to reduce plastic waste, incorporating your suggestions for enhancement:\n\n1. **Reusable Packaging Innovations**: Partner with local retailers to establish a robust logistics system for returning and refilling containers, ensuring widespread participation and convenience for consumers.\n\n2. **Biodegradable Alternatives**: Identify specific strategies for incorporating biodegradable materials into existing supply chains or product lines, facilitating a smoother transition for businesses to adopt eco-friendly options.\n\n3. **Community Swap Events**: Create a digital platform allowing individuals to swap items online, engaging those who cannot attend physical events and expanding the initiative's reach.\n\n4. **Educational Workshops**: Integrate hands-on experiences and interactive activities to keep participants engaged and enhance retention of knowledge about reducing plastic use.\n\n5. **Plastic Waste Upcycling**: Develop a certification process for upcycling workshops, which would promote quality standards and attract a broader audience interested in sustainable crafts.\n\n6. **Plastic-Free Challenges**: Collaborate with local influencers or businesses to sponsor awards or prizes for challenge participants, boosting engagement and motivation.\n\n7. **Refill Stations**: Conduct feasibility studies to identify optimal locations for refill stations (e.g., busy urban areas, shopping centers) to ensure they are accessible to the public.\n\n8. **Corporate Partnerships**: Target specific types of businesses as partners, such as cafes, grocery stores, and retailers, that naturally align with the initiative to promote reusable and sustainable practices.\n\n9. **Digital Packaging**: Address potential barriers to adopting digital packaging solutions, like smartphone accessibility, to present a comprehensive view of the concept's practicality.\n\n10. **Government Legislation Advocates**: Suggest specific legislative measures to advocate for, such as bans on single-use plastics or tax incentives for companies that commit to sustainable practices, making advocacy more actionable.\n\n11. **Plastic Waste Art Competitions**: Collaborate with local art communities or educational institutions to organize competitions that engage a wider audience and encourage creativity while raising awareness.\n\n12. **Sustainable School Programs**: Propose competitive grants or recognition programs to incentivize schools to adopt sustainable practices and participate in environmental initiatives.\n\n13. **Eco-Friendly Vending Machines**: Explore partnerships with local food vendors to offer a diverse selection of products in vending machines that prioritize sustainability and convenience.\n\n14. **Gardening Kits with Recyclable Components**: Research market segments (like urban dwellers or schools) to tailor gardening kits effectively, ensuring relevance and demand in marketing strategies.\n\n15. **DIY Personal Care Products**: Host community workshops where participants can collaboratively create DIY products, fostering a sense of community while promoting zero waste practices.\n\n16. **Plastic Neutrality Programs**: Define criteria that businesses must meet to qualify for plastic neutrality, ensuring transparency and encouraging meaningful participation in sustainability efforts.\n\n17. **Plastic-Free Grocery Stores**: Highlight successful case studies of existing plastic-free grocery stores or markets to serve as models for new initiatives.\n\n18. **Innovative Composting Solutions**: Provide examples of successful composting initiatives from other communities to illustrate best practices and potential implementations.\n\n19. **Mobile Apps for Plastic Waste Tracking**: Include engaging features like gamification, social sharing capabilities, and integration with local sustainability rewards programs to enhance user experience.\n\n20. **Public Awareness Campaigns**: Recommend specific themes (e.g., ocean conservation, wildlife protection) or creative approaches (like storytelling or humor) to resonate strongly with target audiences in awareness campaigns.\n\nThese revisions aim to enhance clarity, practicality, and engagement, making the ideas more actionable and impactful for addressing plastic waste reduction.", type='TextMessage'), TextMessage(source='reviewer', models_usage=RequestUsage(prompt_tokens=2064, completion_tokens=68), metadata={}, content="APPROVE\n\nThis revised list is well-thought-out and addresses the feedback effectively. Each suggestion is clearer, more practical, and focused on engagement and implementation. You've introduced specific strategies, collaborative elements, and examples that can help elevate the initiatives. The points are actionable and demonstrate a comprehensive approach to tackling plastic waste. Well done!", type='TextMessage'), TextMessage(source='summary', models_usage=RequestUsage(prompt_tokens=105, completion_tokens=61), metadata={}, content='The user requested ideas for reducing plastic waste. The final feedback expressed approval of a revised list of suggestions, highlighting that it effectively addressed the initially requested feedback. The reviewer praised the clarity, practicality, and comprehensiveness of the strategies, noting that they included actionable points and collaborative elements to enhance implementation.', type='TextMessage'), StopMessage(source='DiGraphStopAgent', models_usage=None, metadata={}, content='Digraph execution is complete', type='StopMessage')], stop_reason='Stop message received')