Open AI Agent https://openai.com/index/new-tools-for-building-agents/
一个专门为 Agent 进行优化的 API,支持以往 API (Chat Completions API)的所有功能,重点:Responses API 支持新的内置工具,并支持可预测性的流式事件,极大的简化了项目的复杂度。
能力对比:
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model: "适用模型", // computer-use-preview或gpt-4o等
tools: [{
type: "工具名称", // web_search_preview, file_search, computer_use_preview
// 工具特定参数...
}],
truncation: "auto", // computer_use必需
input="What was a positive news story from today?"
// 其他参数...
)
print(response.output_text)
不是新功能,之前就推出过,算版本更新。
使用流程大抵是:
目前支持 9 种行为,这些行为,将会被 CUA 进行自动的组合和执行,达到操作电脑的效果
Git 地址:https://github.com/openai/openai-agents-python
这是一个支持 multi-agent 的框架,任何符合 OpenAI Chat Completions API 的模型都可以来用。
因此,DeepSeek 也能用 OpenAI 的这个框架。
pip install openai-agents
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)
# 输出: Code within the code,
# Functions calling themselves,
# Infinite loop's dance.
pip install openai-agents
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)
# 输出: ¡Hola! Estoy bien, gracias por preguntar. ¿Y tú, cómo estás?
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="Hello world",
instructions="You are a helpful agent.",
tools=[get_weather],
)
async def main():
result = await Runner.run(agent, input="What's the weather in Tokyo?")
print(result.final_output)
# 输出: The weather in Tokyo is sunny.
if __name__ == "__main__":
asyncio.run(main())