欣淇
发布于 2026-05-11 / 0 阅读
0
0

🤖 OpenAI Agents SDK:26k Stars 的官方多智能体框架,3 行代码搭起 Agent 团队

🤖 OpenAI Agents SDK:26k Stars 的官方多智能体框架,3 行代码搭起 Agent 团队

项目地址:https://github.com/openai/openai-agents-python | ⭐ 26.1k Stars | 🛠 Python | 作者 OpenAI


老实说,市面上多智能体框架一大堆——LangChain、CrewAI、AutoGen……选哪个都纠结。OpenAI 自己下场做了个 Agents SDK,26k Stars,MIT 协议,轻量到离谱,而且不绑定 OpenAI 自己的模型,支持 100+ LLM 提供商。

说白了,这玩意儿就是一个 pip install openai-agents 就完事的框架,核心概念只有几个:Agent(智能体)、Handoff(交接)、Tool(工具)、Guardrail(护栏)。别整那些花里胡哨的。

一、安装 + Hello World

一行命令搞定:

pip install openai-agents

如果要语音支持:pip install 'openai-agents[voice]'。Redis 会话管理:pip install 'openai-agents[redis]'

然后直接跑个 Agent:

import asyncio
from agents import Agent, Runner

async def main():
    agent = Agent(
        name="Assistant",
        instructions="You only respond in haikus.",
    )
    result = await Runner.run(agent, "Tell me about recursion in programming.")
    print(result.final_output)
    # Function calls itself,
    # Looping in smaller pieces,
    # Endless by design.

asyncio.run(main())

3 行核心代码,一个 Agent 就跑起来了。最骚的是 Runner.run() 自动处理所有对话上下文,你什么都不用管。

二、给 Agent 装工具

@function_tool 装饰器,任何 Python 函数都能变成 Agent 的工具:

from typing import Annotated
from agents import Agent, Runner, function_tool

@function_tool
def get_weather(city: Annotated[str, "The city to get weather for"]) -> str:
    """Get the current weather for a city."""
    return f"The weather in {city} is sunny, 14-20°C."

agent = Agent(
    name="Weather Bot",
    instructions="You are a helpful weather assistant.",
    tools=[get_weather],
)

result = await Runner.run(agent, "What's the weather in Tokyo?")
print(result.final_output)
# The weather in Tokyo is sunny, 14-20°C.

踩坑提醒:工具函数的 docstring 和参数注解一定要写清楚,LLM 靠这个理解什么时候调哪个工具。不写的话 Agent 可能乱调用。

三、多智能体协作——Handoff

多个 Agent 之间可以互相"交班",像团队协作一样:

spanish_agent = Agent(
    name="spanish_agent",
    instructions="You translate messages to Spanish",
)

french_agent = Agent(
    name="french_agent",
    instructions="You translate messages to French",
)

orchestrator = Agent(
    name="orchestrator",
    instructions="Translate user messages using the available tools.",
    tools=[
        spanish_agent.as_tool(tool_name="translate_to_spanish"),
        french_agent.as_tool(tool_name="translate_to_french"),
    ],
)

result = await Runner.run(orchestrator, "Translate 'Hello' to Spanish and French.")

每个子 Agent 就像专人专岗,编排 Agent 负责任务分发。这就是 OpenAI 说的 Agents as Tools 模式。

四、Human-in-the-Loop

有些操作不想让 AI 自己决定?加个审批:

from agents import function_tool

@function_tool(needs_approval=lambda ctx, params, id: "Oakland" in params.get("city", ""))
async def get_temperature(city: str) -> str:
    return f"The temperature in {city} is 20°C"

agent = Agent(name="Weather Assistant", tools=[get_weather, get_temperature])
result = await Runner.run(agent, "What's the weather and temperature in Oakland?")
# → 触发审批流程

实际开发中,删除操作、写数据库、调用外部 API 需要人类确认的场景,这个功能特别实用。别问我怎么知道的,踩过的坑都是泪。

五、总结

  • pip install openai-agents,3 行代码跑起第一个 Agent
  • @function_tool 装饰器把任意函数变成 Agent 工具
  • agent.as_tool() 让 Agent 之间互相调用,搭建多智能体协作
  • needs_approval 实现 Human-in-the-Loop,关键操作需人类审批
  • 不绑定 OpenAI,支持 100+ LLM 提供商,MIT 开源协议
  • 最骚的是它轻量——没有重抽象,没有奇怪的概念层,Python 3.10+ 就行。OpenAI 官方出品,文档清晰,社区活跃。想做 AI Agent 又不想折腾,这个是最稳的起点。


    评论