Agent Communication Protocol
Github
  • ACP(智能体通信协议)介绍
    • 为什么选择ACP?
    • ACP架构图
    • AID智能体身份标识
    • ACP证书管理体系
    • Agent会话时序
    • Agent的通信协议
      • ACP底层协议
      • ACP消息格式
    • Agent数据规范
      • agentprofile.json规范
      • config.json规范
    • Agent授权与交易
    • Agent的发现机制
    • Agent行为及安全规范
    • 一些设计的理念和原则
  • ACP SDK快速入门
    • Agent如何接入智能体互联网
    • 接入点部署
    • Agent例程
      • 一、创建身份&读写公私有数据
      • 二、agent的hello world
      • 三、把在线大模型封装成agent
        • 1.deepseek异步响应
        • 2.qwen3大模型流式输出
        • 3.qwen3大模型function calling
      • 四、把本地大模型封装成agent
      • 五、通过调用大模型agent来替代直接对大模型的调用
      • 六、调用api的方式来实现天气查询的agent
      • 七、通过使用agent的方式来完成天气的查询
      • 八、把输出写入到文件的agent
      • 九、读取文件数据输出的agent
      • 十、将python执行器封装成agent
      • 十一、对agent实现串行和并行的调用
      • 十二、HCP 天气问答智能体
      • 十三、将dify实现的agent接入agent互联网
        • 1.dify chat接入
        • 2.dify workflow接入
      • 十四、用agently来实现agent
      • 十五、将阿里百炼平台上封装的agent接入agent互联网
      • 十六、千问大模型智能体接入程序
      • 十七、生成agent调用关系图
  • 常见问题FAQ
  • 其它
由 GitBook 提供支持
在本页
  • github:
  • README.md
  • 使用指南
  • 功能简介
  • 完整示例代码
  1. ACP SDK快速入门
  2. Agent例程

六、调用api的方式来实现天气查询的agent

上一页五、通过调用大模型agent来替代直接对大模型的调用下一页七、通过使用agent的方式来完成天气的查询

最后更新于23天前

github:

README.md

使用指南

1、环境要求

  • Python 3.8+

2、安装依赖

确保已安装agentcp库

pip install agentcp

3、创建身份ID

请参考

4、修改main.py文件

5、执行main.py代码

python main.py

功能简介

该 Agent 展示了如何基于 agentcp 实现一个天气查询服务 Agent。你可以通过本地 Agent 与大模型 Agent 进行通信,从而实现函数调用能力,例如获取天气信息。

  • 接收并处理用户的消息请求

  • 转发查询请求到目标大模型 Agent

  • 自动调用工具函数(如 get_weather)并返回结果

  • 处理大模型 Agent 的回复并返回给用户

完整示例代码

import agentcp
import json
import time
tools=[{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Retrieves the current weather report for a specified city",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {
                    "type": "string",
                    "description": "The name of the city for which to retrieve the weather report"
                }
            },
            "required": ["city"]
        }
    }
}]
def get_weather(city: str) -> str:
    return f"The weather in {city} is sunny with a temperature of 25 degrees"

def call_function(name, args):
    if name == "get_weather":
        return get_weather(**args)
    else:
        return f"Function {name} not found"

if __name__ == "__main__":
    llm_agent_id = "agent_id_from_mu"
    agent_id = 'your_agent_id'
    acp = agentcp.AgentCP('.')
    aid = acp.load_aid(agent_id)
    async def reply_message_handler(reply_msg, sender, session_id):
        message_json = json.loads(reply_msg.get("message"))
        if isinstance(message_json, list) and len(message_json) > 0:
            message_json = message_json[0]
        type = message_json.get("type")
        if type == "tool_call":
            tool = json.loads(message_json.get("content"))
            tool_name = tool.get("tool_name")
            tool_args = tool.get("tool_args")
            tool_result = call_function(tool_name, tool_args)
            aid.quick_send_messsage_content(llm_agent_id, tool_result, lambda reply_msg: reply_message_handler(reply_msg, sender, session_id))
        else:
            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)
    
    @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)
        msg_block = {
            "type": "content",
            "status": "success",
            "timestamp": int(time.time() * 1000),
            "content": sender_content,
            "tools": tools
        }
        aid.quick_send_messsage(llm_agent_id, msg_block, lambda reply_msg: reply_message_handler(reply_msg, sender, session_id))
        return True
    aid.online()
    acp.serve_forever()

1、将seed_password、agent_id修改为上一步创建的身份信息2、将 llm_agent_id修改为你想要调用的

https://github.com/auliwenjiang/agentcp/tree/master/samples/query_weather_api_agent
AgentCP SDK
创建身份,读写公有私有数据文档
agent_id