通过使用 Agent 的方式来完成天气的查询
GitHub: query_weather_from_agent
使用指南
1. 环境要求
- Python 3.8+
- AgentCP SDK
2. 安装依赖
确保已安装 agentcp 库
bash
pip install agentcp3. 创建身份 ID
请参考 创建身份,读写公有私有数据
4. 修改 main.py 文件
- 将
seed_password、agent_id修改为上一步创建的身份信息 - 将
to_agent_id修改为你想要调用的 agent_id
5. 执行 main.py 代码
bash
python main.py功能简介
该 Agent 基于 agentcp 库构建,实现了一个天气查询的中转代理 Agent,可接收用户请求并将其转发给下游天气查询 Agent,最终将结果返回用户。
- 接收并处理用户的消息请求
- 转发查询请求到目标 Agent
- 处理目标 Agent 的响应并返回给原始请求方
完整示例代码
python
import agentcp
if __name__ == "__main__":
to_agent_id = "agent_id_from_mu"
agent_id = 'your_agent_name'
acp = agentcp.AgentCP('.', seed_password='')
aid = acp.load_aid(agent_id)
# 处理目标 Agent 回复内容
async def reply_message_handler(reply_msg, sender, session_id):
reply_text = aid.get_content_from_message(reply_msg)
aid.send_message_content(to_aid_list=[sender], session_id=session_id, llm_content=reply_text)
# 处理用户请求并转发至目标 Agent
@aid.message_handler()
async def sync_message_handler(msg):
receiver = aid.get_receiver_from_message(msg)
if aid.id not in receiver:
return
session_id = aid.get_session_id_from_message(msg)
sender = aid.get_sender_from_message(msg)
sender_content = aid.get_content_from_message(msg)
aid.quick_send_messsage_content(to_agent_id, sender_content, lambda reply_msg: reply_message_handler(reply_msg, sender, session_id))
return True
aid.online()
acp.serve_forever()