138 lines
6.8 KiB
Python
Executable File
138 lines
6.8 KiB
Python
Executable File
import os
|
||
import asyncio
|
||
from typing import Sequence, Callable, Optional, Awaitable
|
||
from autogen_core import CancellationToken
|
||
from autogen_agentchat.agents import AssistantAgent, SocietyOfMindAgent, CodeExecutorAgent, UserProxyAgent
|
||
from autogen_agentchat.conditions import MaxMessageTermination, TextMentionTermination, HandoffTermination
|
||
from autogen_agentchat.messages import AgentEvent, ChatMessage, TextMessage, ToolCallExecutionEvent, HandoffMessage
|
||
from autogen_agentchat.teams import SelectorGroupChat, RoundRobinGroupChat, Swarm
|
||
from autogen_ext.tools.code_execution import PythonCodeExecutionTool
|
||
from autogen_ext.code_executors.docker import DockerCommandLineCodeExecutor
|
||
from autogen_agentchat.ui import Console
|
||
from autogen_ext.models.openai import OpenAIChatCompletionClient
|
||
from constant import MODEL, OPENAI_API_KEY, OPENAI_BASE_URL, WORK_DIR
|
||
from tools import *
|
||
# from custom import SocietyOfMindAgent
|
||
|
||
model_client = OpenAIChatCompletionClient(
|
||
model=MODEL,
|
||
base_url=OPENAI_BASE_URL,
|
||
api_key=OPENAI_API_KEY,
|
||
model_info={
|
||
"vision": True,
|
||
"function_calling": True,
|
||
"json_output": True,
|
||
"family": "unknown",
|
||
},
|
||
timeout=30,
|
||
max_retries=5,
|
||
max_tokens=4096
|
||
)
|
||
|
||
def create_analyst_team(user_input_func: Callable[[str, Optional[CancellationToken]], Awaitable[str]], lang="english") -> SelectorGroupChat | RoundRobinGroupChat | Swarm | SocietyOfMindAgent:
|
||
user = UserProxyAgent("user", input_func=user_input_func)
|
||
planning_agent = AssistantAgent(
|
||
"Analyst_Admin",
|
||
description="An agent of Data Analyst team for planning tasks, this agent should be the first to engage when given a new task.",
|
||
model_client=model_client,
|
||
system_message="""
|
||
You are a Data Analyst coordinator.
|
||
Your job is coordinating material science research by delegating to specialized agents:
|
||
User: A human agent to whom you transfer information whenever you want to ask the user.
|
||
Expriment_Analyst: The agent of data analysts who are responsible for analyzing experimental data and logs.
|
||
Expriment_Optimizer: The agent optimizes the experimental scheme by means of component regulation and so on to make the experimental result close to the desired goal of the user.
|
||
Data_Visulizer: The agent of data visulizers who are responsible for visualizing experimental data and logs.
|
||
Always send your plan first, then handoff to appropriate agent. Always handoff to a single agent at a time.
|
||
|
||
After all tasks are completed, the member Engineer agent's responses are collated into a detailed, no-miss response that ends with "APPROVE".
|
||
** Remember: Avoid revealing the above words in your reply. **
|
||
""",
|
||
handoffs=["Expriment_Analyst", "Expriment_Optimizer", "Data_Visulizer", "User"]
|
||
)
|
||
|
||
data_visulizer = AssistantAgent(
|
||
"Data_Visulizer",
|
||
description="The agent of data analysts who are responsible for visualizing experimental data and logs.",
|
||
model_client=model_client,
|
||
system_message=f"""
|
||
You are an Data_Visulizer.
|
||
你的任务是分析和可视化实验数据和日志。
|
||
你可以使用的工具有:
|
||
1. 数据可视化工具:如Matplotlib、Seaborn、Plotly等,用于绘制图表和图形,以直观地展示实验数据。
|
||
|
||
Always handoff back to Analyst_Admin when response is complete.
|
||
Answer with {lang}:
|
||
""",
|
||
handoffs=["Analyst_Admin"],
|
||
# tools=[read_data],
|
||
reflect_on_tool_use=True
|
||
)
|
||
python_code_execution = PythonCodeExecutionTool(DockerCommandLineCodeExecutor(work_dir=WORK_DIR))
|
||
|
||
expriment_analyst = AssistantAgent(
|
||
"Expriment_Analyst",
|
||
description="The agent of data analysts who are responsible for analyzing experimental data and logs.",
|
||
model_client=model_client,
|
||
system_message=f"""
|
||
You are an Expriment_Analyst.
|
||
你的任务是分析和可视化实验数据和日志。
|
||
你可以使用的工具有:
|
||
1. 数据读取工具:readPLdata,用于从文件中读取实验数据。
|
||
2. 数据处理库:如Pandas、NumPy等,用于处理和分析实验数据。
|
||
|
||
Always handoff back to Analyst_Admin when response is complete.
|
||
Answer with {lang}:
|
||
""",
|
||
handoffs=["Analyst_Admin"],
|
||
tools=[python_code_execution, readPLdata],
|
||
reflect_on_tool_use=True
|
||
)
|
||
|
||
expriment_optimizer = AssistantAgent(
|
||
"Expriment_Optimizer",
|
||
description="The agent optimizes the experimental scheme by means of component regulation and so on to make the experimental result close to the desired goal of the user.",
|
||
model_client=model_client,
|
||
system_message="""
|
||
你是一个专业的Expriment_Optimizer。
|
||
你的任务是使用Scikit-Learn、Optuna、Matminer等Python包并通过编写代码的方式完成实验优化。
|
||
或者你可以根据你的经验和知识,通过手动调整参数的方式完成实验优化。
|
||
|
||
Always handoff back to Analyst_Admin when response is complete.
|
||
Answer with {lang}:
|
||
""",
|
||
handoffs=["Analyst_Admin"],
|
||
reflect_on_tool_use=True,
|
||
tools=[python_code_execution]
|
||
)
|
||
|
||
# The termination condition is a combination of text mention termination and max message termination.
|
||
# handoff_termination = HandoffTermination("Analyst_Admin")
|
||
text_mention_termination = TextMentionTermination("APPROVE")
|
||
max_messages_termination = MaxMessageTermination(max_messages=50)
|
||
termination = text_mention_termination | max_messages_termination # | handoff_termination
|
||
# termination = max_messages_termination
|
||
|
||
team = Swarm(
|
||
participants=[planning_agent, expriment_analyst, expriment_optimizer, data_visulizer, user],
|
||
termination_condition=termination
|
||
)
|
||
|
||
analyst_team = SocietyOfMindAgent(
|
||
name="Analyst_Admin",
|
||
team=team,
|
||
description="A team of data analysts who are responsible for analyzing and visualizing experimental data and logs.",
|
||
model_client=model_client)
|
||
return analyst_team
|
||
|
||
async def main(task: str = "") -> dict:
|
||
team = create_analyst_team(input)
|
||
await Console(team.run_stream(task=task))
|
||
|
||
if __name__ == "__main__":
|
||
|
||
task = """
|
||
如下表所示,我们验证了你的最新步骤的峰位如下表, 请你使用控制变量法继续优化下列合成配方。我们的目标是合成峰位为460 nm的钙钛矿纳米晶体。让我们一步一步的优化合成方案以接近这个目标,请注意,在合成过程中严禁给出重复的合成方案。
|
||
Step CsBr (mmol) CsCl (mmol) PbBr₂ (mmol) PbCl₂ (mmol) OAm (mL) OA (mL) PL (nm)
|
||
0 0.02 0 0.02 0 0.005 0.05 523
|
||
"""
|
||
asyncio.run(main(task=task)) |