OpenAI Agents SDK 是一個輕量級且功能強大的Python多智能體工作流構建框架。這是OpenAI官方發布的生產級智能體開發工具,是對之前實驗性項目Swarm的升級版本。該框架設計簡潔但功能完備,專門用於構建複雜的多智能體AI應用。
# 創建Python虛擬環境
python -m venv env
source env/bin/activate
# 安裝基礎版本
pip install openai-agents
# 安裝包含語音支持的版本
pip install 'openai-agents[voice]'
from agents import Agent, Runner
agent = Agent(name="Assistant", instructions="You are a helpful assistant")
result = Runner.run_sync(agent, "Write a haiku about recursion in programming.")
print(result.final_output)
from agents import Agent, Runner
import asyncio
spanish_agent = Agent(
name="Spanish agent",
instructions="You only speak Spanish.",
)
english_agent = Agent(
name="English agent",
instructions="You only speak English",
)
triage_agent = Agent(
name="Triage agent",
instructions="Handoff to the appropriate agent based on the language of the request.",
handoffs=[spanish_agent, english_agent],
)
async def main():
result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?")
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())
import asyncio
from agents import Agent, Runner, function_tool
@function_tool
def get_weather(city: str) -> str:
return f"The weather in {city} is sunny."
agent = Agent(
name="Weather Assistant",
instructions="You are a helpful weather agent.",
tools=[get_weather],
)
async def main():
result = await Runner.run(agent, input="What's the weather in Tokyo?")
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())
output_type
,當LLM返回匹配類型的内容時結束output_type
,當智能體產生沒有工具調用或交接的消息時結束OPENAI_API_KEY
環境變量# 安裝uv包管理器
uv --version
# 安裝依賴
make sync
# 運行測試
make tests
# 類型檢查
make mypy
# 代碼規範檢查
make lint
OpenAI Agents Python是一個專業級的多智能體開發框架,它將複雜的AI協作變得簡單易用。通過其核心的智能體、交接、防護和追踪機制,開發者可以快速構建出功能强大的AI應用系統。無論是簡單的單智能體任務還是複雜的多智能體協作場景,這個框架都能提供優雅的解決方案。