Skip to content

Getting Started

5 分钟跑通第一个 Agent。

安装

bash
pip install dumplingsAI

需要 Python 3.10+。无额外可选依赖。

准备 API Key

bash
export API_KEY="sk-..."               # OpenAI 协议
export ANTHROPIC_API_KEY="sk-ant-..." # Anthropic 协议

v0.2.2+ 起 api_keyos.getenv() 读取,不要硬编码到子类里。

第一个 Agent(OpenAI 协议)

python
import os
import dumplingsAI

# 注册一个工具
@dumplingsAI.tool_registry.register_tool(
    allowed_agents=["weather"],
    name="get_weather",
    description="查询某城市当前天气",
    parameters={
        "type": "object",
        "properties": {"city": {"type": "string", "description": "城市名"}},
        "required": ["city"],
    },
)
def get_weather(city: str) -> str:
    return f"{city}今天晴,25°C"

# 注册 Agent(v0.3.0+ 模板池写法)
from dumplingsAI import template_agent
from dumplingsAI.Agent_list import activate_template

@template_agent("weather", uuid="weather-uuid", description="天气小助手")
class WeatherAgent(dumplingsAI.BaseAgent):
    prompt = "你是天气助手,使用 get_weather 工具查询天气。"
    api_provider = "https://api.example.com/v1/chat/completions"
    model_name = os.getenv("OPENAI_MODEL")
    api_key = os.getenv("API_KEY")

# 激活模板(也可由 LLM 在对话中通过 activate_template builtin_tool 触发)
activate_template("weather")

# 跑一次对话
agent = dumplingsAI.agent_list["weather"]
print(agent.conversation_with_tool("北京今天天气怎么样?"))

第一个 Agent(Anthropic 协议)

python
import os
import dumplingsAI
from dumplingsAI import template_agent
from dumplingsAI.Agent_list import activate_template

@template_agent("reviewer", uuid="reviewer-uuid", description="评审 Agent")
class ReviewerAgent(dumplingsAI.AnthropicAgent):
    prompt = "你是评审助手。完成工作后用 attempt_completion 汇报。"
    api_provider = "https://api.anthropic.com"
    model_name = os.getenv("ANTHROPIC_MODEL")
    api_key = os.getenv("ANTHROPIC_API_KEY")

activate_template("reviewer")
agent = dumplingsAI.agent_list["reviewer"]
print(agent.conversation_with_tool("请评审:xxx"))

同一份 agent_list,OpenAI Agent 和 Anthropic Agent 可以直接 ask_for_help 互调。

下一步