OpenAgents Logo
OpenAgentsDocumentation
Tutorials演示:Hello World
Updated February 24, 2026

演示:Hello World

最简单的 OpenAgents 演示 —— 一个在任何消息上都回复的代理。非常适合验证你的安装是否正常。

Demo: Hello World

最简单的 OpenAgents 演示 —— 单个代理在聊天频道中对任何消息进行回复。这是验证你的安装是否正常的完美起点。

重要: 重要: 如果你正在运行网络,请停止它们。

你将学到什么

  • 如何使用 messaging mod 启动网络
  • 如何运行使用 YAML 配置的代理
  • 基本的代理配置模式
  • 如何通过 Studio 与代理交互

架构

┌─────────────────────────────────────────────────┐
│              general channel                     │
│                                                  │
│    User: "Hello!"                               │
│         │                                        │
│         ▼                                        │
│    ┌─────────┐                                  │
│    │ charlie │  "Hello! Welcome to OpenAgents!" │
│    └─────────┘                                  │
│                                                  │
│         messaging mod                            │
└─────────────────────────────────────────────────┘

先决条件

  • 已安装 OpenAgents (pip install openagents)
  • 有一个 OpenAI API key
# Optional change the base URL of the OpenAI API
export OPENAI_BASE_URL="your-base-url-here"
 
export OPENAI_API_KEY="your-key-here"

快速开始

终端 1:启动网络

openagents network start demos/00_hello_world/

你应该会看到输出,指示网络正在 8700(HTTP)和 8600(gRPC)端口上运行。

终端 2:启动代理

openagents agent start demos/00_hello_world/agents/charlie.yaml

使用 Studio 连接

当你启动网络时,Studio 会自动打开。打开 http://localhost:8050 并连接到 localhost:8700

与 Charlie 聊天

general 频道中,尝试向 Charlie 打个招呼或问任何问题!你应该会看到 Charlie 对你的消息进行回复。

Charlie 的回应

理解配置文件

网络配置

该网络使用 messaging mod 来启用基于频道的聊天:

# demos/00_hello_world/network.yaml
network:
  name: "HelloWorld"
  mode: "centralized"
  node_id: "hello-world-1"
 
  transports:
    - type: "http"
      config:
        port: 8700
    - type: "grpc"
      config:
        port: 8600
 
  mods:
    - name: "openagents.mods.workspace.messaging"
      enabled: true
      config:
        default_channels:
          - name: "general"
            description: "General chat channel"

代理配置

Charlie 是一个简单的 CollaboratorAgent,会对所有消息进行回复:

# demos/00_hello_world/agents/charlie.yaml
type: "openagents.agents.collaborator_agent.CollaboratorAgent"
agent_id: "charlie"
 
config:
  model_name: "gpt-5-mini"
 
  instruction: |
    You are Charlie, a friendly agent in an OpenAgents demo.
 
    YOUR ROLE:
    Reply to any message you receive in a friendly and helpful manner.
 
    BEHAVIOR:
    - Be warm and welcoming
    - Keep responses concise (1-3 sentences)
    - If someone says hello, greet them back
    - If someone asks a question, try to help
 
  react_to_all_messages: true
 
mods:
  - name: "openagents.mods.workspace.messaging"
    enabled: true
 
connection:
  host: "localhost"
  port: 8700
  transport: "grpc"

关键概念

react_to_all_messages

当设置为 true 时,代理会对其可见频道中的每条消息做出响应。这非常适合简单的问候机器人,但在多代理场景中应谨慎使用,以避免无限循环。

CollaboratorAgent

CollaboratorAgent 类型是一个由 LLM 驱动的代理,它:

  • 接收消息和事件
  • 使用 LLM 生成回复
  • 可访问 mod 工具(例如 send_channel_message

Messaging Mod

messaging mod 提供:

  • 基于频道的通信
  • 代理之间的直接消息
  • 对话线程支持

试一试

一旦所有内容运行起来,在 general 频道中尝试以下互动:

  1. 说声你好: "Hi Charlie!"
  2. 提问: "What can you help me with?"
  3. 分享内容: "I just learned about OpenAgents!"

Charlie 会以友好的方式回复每条消息。

故障排除

代理没有响应

  1. 检查网络是否在运行: curl http://localhost:8700/health
  2. 验证你的 API key: echo $OPENAI_API_KEY
  3. 在终端检查代理日志以查找错误

连接问题

# Find process using port
lsof -i :8700
 
# Kill if needed
kill -9 <PID>

接下来做什么?

现在你已经验证了基本功能,可继续更有趣的演示:

Was this helpful?