明确了swarm分配方式的局限性

This commit is contained in:
2025-01-12 14:12:57 +08:00
parent 6fba3800d7
commit 75fa56938c
6 changed files with 275 additions and 193 deletions

View File

@@ -1,10 +1,11 @@
import asyncio
from typing import Sequence
from autogen_agentchat.agents import AssistantAgent, SocietyOfMindAgent
from autogen_agentchat.conditions import MaxMessageTermination, TextMentionTermination
from autogen_agentchat.agents import AssistantAgent, SocietyOfMindAgent, UserProxyAgent
from autogen_agentchat.conditions import MaxMessageTermination, TextMentionTermination, HandoffTermination
from autogen_agentchat.messages import AgentEvent, ChatMessage, TextMessage, ToolCallExecutionEvent
from autogen_agentchat.teams import SelectorGroupChat, RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_agentchat.base import Handoff
from autogen_ext.models.openai import OpenAIChatCompletionClient
from constant import MODEL, OPENAI_API_KEY, OPENAI_BASE_URL
from scientist_team import create_scientist_team
@@ -28,6 +29,8 @@ async def main(task: str = "") -> dict:
engineer_team = create_engineer_team()
result = {}
user = UserProxyAgent("user", input_func=input)
planning_agent = AssistantAgent(
"PlanningAgent",
description="An agent for planning tasks, this agent should be the first to engage when given a new task.",
@@ -37,16 +40,23 @@ async def main(task: str = "") -> dict:
Your job is to break down complex Materials science research tasks into smaller, manageable subtasks.
Assign these subtasks to the appropriate sub-teams; not all sub-teams are required to participate in every task.
Your sub-teams are:
User: A human agent to whom you transfer information whenever you need to confirm your execution steps to a human.
Engineer team: A team of professional engineers who are responsible for writing code, visualizing experimental schemes, converting experimental schemes to JSON, and more.
Scientist team: A professional team of material scientists who are mainly responsible for consulting on material synthesis, structure, application and properties.
Engineer team: A team of professional engineers who are responsible for writing code, visualizing experimental schemes, converting experimental schemes to machine code, and more.
You only plan and delegate tasks - you do not execute them yourself.
第一次回答时你需要初始化任务分配并按顺序执行在后续的回答中重申你的任务分配使用如下格式并利用Mermaid绘制流程图
| team | subtask |
| --------- | -------------------- |
| 1. team_name | sub-Task description |
When assigning subtasks, use this format:
1. <agent> : <task>
When assigning subtasks, give a flow chart with following format or mermaid to visualize the collaboration between the various teams, such as:
<agent 1> -> <agent 2> -> <agent 3>
每次回答时,你需要清晰明确的指出已经完成的子任务下一步子任务,使用如下格式:
**已完成子任务:**
1. <team> : <subtask>
**Next sub-task:**
n. <team> : <subtask>
You can end with "USER" if you need to, which means you need human approval or other advice or instructions;
After plan and delegate tasks are complete, end with "START";
Determine if all sub-teams have completed their tasks, and if so, summarize the findings and end with "TERMINATE".
""",
@@ -55,7 +65,7 @@ async def main(task: str = "") -> dict:
# The termination condition is a combination of text mention termination and max message termination.
text_mention_termination = TextMentionTermination("TERMINATE")
max_messages_termination = MaxMessageTermination(max_messages=25)
max_messages_termination = MaxMessageTermination(max_messages=100)
termination = text_mention_termination | max_messages_termination
# The selector function is a function that takes the current message thread of the group chat
@@ -63,10 +73,12 @@ async def main(task: str = "") -> dict:
def selector_func(messages: Sequence[AgentEvent | ChatMessage]) -> str | None:
if messages[-1].source != planning_agent.name:
return planning_agent.name # Always return to the planning agent after the other agents have spoken.
elif "USER" in messages[-1].content:
return user.name
return None
team = SelectorGroupChat(
[planning_agent, scientist_team, engineer_team],
[planning_agent, user, scientist_team, engineer_team],
model_client=model_client, # Use a smaller model for the selector.
termination_condition=termination,
selector_func=selector_func,
@@ -83,6 +95,7 @@ async def main(task: str = "") -> dict:
# Example usage in another function
async def main_1():
# result = await main(input("Enter your instructions below: \n"))
result = await main("Let the robot synthesize CsPbBr3 nanocubes at room temperature")
# result = await main("查一下CsPbBr3的晶体结构")