-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagents.py
More file actions
151 lines (124 loc) · 5.13 KB
/
agents.py
File metadata and controls
151 lines (124 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from core import Agent, Message, ToolRegistry, WebSearchTool, CalculatorTool, FileReadTool
from typing import Any
class UserAgent(Agent):
"""
用户代理,代表与系统交互的用户
"""
def __init__(self):
super().__init__("user", "User")
def receive(self, message: Message) -> None:
"""
用户代理接收消息(通常是最终结果)
"""
print(f"[{self.name}] 收到最终结果: {message.content}")
print("=" * 50)
class ResearchAgent(Agent):
"""
研究代理,负责搜索和收集信息
"""
def __init__(self):
super().__init__("researcher", "Researcher",
system_prompt="你是一个研究助理,专门负责搜索和收集信息。")
# 注册工具
self.tool_registry = ToolRegistry()
self.tool_registry.register(WebSearchTool())
self.tool_registry.register(CalculatorTool())
def receive(self, message: Message) -> None:
"""
研究代理接收研究请求并执行搜索
"""
print(f"[{self.name}] 收到研究请求: {message.content}")
# 执行搜索
search_query = message.content
try:
search_result = self.tool_registry.execute("web_search", query=search_query)
print(f"[{self.name}] 搜索完成,结果: {search_result['result']}")
# 将结果发送给下一个Agent(WriterAgent)
self.send(
"writer",
{
"original_query": search_query,
"search_result": search_result
},
"research_result"
)
except Exception as e:
self.send("writer", f"搜索出错: {str(e)}", "error")
class WriterAgent(Agent):
"""
作家代理,负责整理和撰写报告
"""
def __init__(self):
super().__init__("writer", "Writer",
system_prompt="你是一个专业的内容整理者和报告撰写者。")
def receive(self, message: Message) -> None:
"""
作家代理接收研究结果并整理成报告
"""
print(f"[{self.name}] 收到研究结果,开始撰写报告...")
if message.type == "research_result":
data = message.content
original_query = data["original_query"]
search_result = data["search_result"]
# 整理成报告
report = f"""
研究报告: {original_query}
================================
{search_result["result"]}
来源: {search_result["source"]}
""".strip()
# 将最终报告发送给用户
self.send("user", report, "final_report")
else:
# 处理其他类型的消息
error_report = f"无法处理的消息类型: {message.type}\n内容: {message.content}"
self.send("user", error_report, "error_report")
class AnalyzerAgent(Agent):
"""
分析代理,负责数据分析和处理
"""
def __init__(self):
super().__init__("analyzer", "Analyzer",
system_prompt="你是一个数据分析专家,专门负责处理和分析数据。")
# 注册工具
self.tool_registry = ToolRegistry()
self.tool_registry.register(CalculatorTool())
self.tool_registry.register(FileReadTool())
def receive(self, message: Message) -> None:
"""
分析代理接收分析请求并执行分析
"""
print(f"[{self.name}] 收到分析请求: {message.content}")
# 根据消息类型执行不同的分析任务
if message.type == "calculate":
try:
expression = message.content
result = self.tool_registry.execute("calculator", expression=expression)
print(f"[{self.name}] 计算完成: {result}")
# 将结果发送给请求者
self.send(
message.sender,
result,
"calculation_result"
)
except Exception as e:
self.send(message.sender, f"计算出错: {str(e)}", "error")
elif message.type == "mcp_command":
# 处理MCP工具命令
try:
command_data = message.content
tool_name = command_data.get("tool_name", "")
command = command_data.get("command", "")
params = command_data.get("params", {})
result = self.tool_registry.execute_mcp_tool(tool_name, command=command, params=params)
print(f"[{self.name}] MCP工具执行完成: {result}")
# 将结果发送给请求者
self.send(
message.sender,
result,
"mcp_result"
)
except Exception as e:
self.send(message.sender, f"MCP工具执行出错: {str(e)}", "error")
else:
self.send(message.sender, "未知的分析任务", "error")