Skip to content

agent的hello world

Github

https://github.com/auliwenjiang/agentcp/tree/master/samples/helloworld

1、使用指南

1)、创建两个agent身份

请参考一、创建身份&读写公私有数据

2)、修改hello_world.py文件

将seed_password、name1、name2修改为步骤1)创建的身份信息

3)、执行代码

bash
python hello_world.py

2、功能简介

该Agent基于agentcp库构建的hello world示例,主要演示以下功能:

  • 创建两个基础Agent身份(name1/name2)
  • 实现Agent间的消息接收与回复
  • 控制台输入消息的测试能力
  • 固定回复"hello world"的简单交互逻辑

3、完整示例代码

python
# coding:utf-8
import agentcp

if __name__ == "__main__":
    print(f"\n开始:agentcp版本:{agentcp.__version__},{__file__}")
    # 以当前文件夹为acp根路径
    acp = agentcp.AgentCP(".", seed_password="888777")

    # 创建接收者Agent
    llrecv = acp.create_aid("agentunion.cn", "name1")

    @llrecv.message_handler()
    async def sync_message_handler(msg):
        print("收到消息:", msg)
        llrecv.reply_message(msg, "hello world")
        return True

    llrecv.online()

    # 创建发送者Agent
    llsend = acp.create_aid("agentunion.cn", "name2")

    @llsend.message_handler()
    async def sync_message_handler_test(msg):
        llsend.reply_message(msg, "hello world")
        return True

    llsend.online()

    # 控制台交互测试
    while True:
        user_input = input("请输入内容:")
        llsend.quick_send_messsage_content(llrecv.id, user_input, sync_message_handler_test)

Released under the Apache 2.0 License.