> ## Documentation Index
> Fetch the complete documentation index at: https://ppio.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Agents SDK 接入 PPIO LLM API

3月12日，OpenAI 推出了 Agents SDK，彻底改变了开发者们构建 AI 应用程序的方式。[OpenAI Agents SDK](https://github.com/openai/openai-agents-python) 是一个轻量级而功能强大的框架，用于构建多代理工作流，将人工智能代理所需的时间从几周缩短到了几分钟。

该 SDK 基于 OpenAI 的强大语言模型，能帮助开发者们快速创建可理解自然语言、执行任务并与用户交互的智能代理，并兼容任何支持 OpenAI Chat Completions API 格式的模型提供商。

<img src="https://mintcdn.com/ppinfra/FhPQL2dauyVkuUFM/third-party/images/OpenAI-Agents-SDK/1.PNG?fit=max&auto=format&n=FhPQL2dauyVkuUFM&q=85&s=bdf9889c5f65e58c6d7ab4a87209a061" alt="1.PNG" width="750" height="671" data-path="third-party/images/OpenAI-Agents-SDK/1.PNG" />

作为一站式 AIGC 云服务平台，PPIO 派欧云提供高性能的 API 服务，涵盖了最新的 DeepSeek R1/V3 Turbo、Qwen 等系列模型，仅需一行代码即可调用；并支持在 chatbox、angthingLLM、Ragflow 等 20+ 主流第三方平台调用。

以下教程教您轻松集成 PPIO LLM API 与 OpenAI Agents SDK，快速构建多代理工作流。

## PPIO X OpenAI Agents SDK 图文集成教程

1. 前置条件：获取 PPIO LLM API key
   1. 【API Base URL】：固定为：[https://api.ppio.com/openai](https://api.ppio.com/openai)
   2. 【API key】：
      （1）登录派欧云控制台[API密钥管理](https://ppio.com/settings/key-management)页面，点击【+创建】按钮。注册账号填写邀请码【VOJL20】得代金券

      <img src="https://mintcdn.com/ppinfra/FhPQL2dauyVkuUFM/third-party/images/OpenAI-Agents-SDK/2.PNG?fit=max&auto=format&n=FhPQL2dauyVkuUFM&q=85&s=24f462f75841c9d3958408fb4a17aac2" alt="2.PNG" width="1280" height="498" data-path="third-party/images/OpenAI-Agents-SDK/2.PNG" />

      （2）生成并保存【API密钥】
      !!注意：密钥在服务端是加密存储，请在生成时保存好密钥；若遗失可以在控制台上删除并创建一个新的密钥。

      <img src="https://mintcdn.com/ppinfra/FhPQL2dauyVkuUFM/third-party/images/OpenAI-Agents-SDK/3.PNG?fit=max&auto=format&n=FhPQL2dauyVkuUFM&q=85&s=01af9d069ec9d0f874973365dcb23016" alt="3.PNG" width="1280" height="498" data-path="third-party/images/OpenAI-Agents-SDK/3.PNG" />

      <img src="https://mintcdn.com/ppinfra/FhPQL2dauyVkuUFM/third-party/images/OpenAI-Agents-SDK/4.PNG?fit=max&auto=format&n=FhPQL2dauyVkuUFM&q=85&s=9de165bb89654ec59b093df94c3a7480" alt="4.PNG" width="1280" height="529" data-path="third-party/images/OpenAI-Agents-SDK/4.PNG" />

      （3）获取需要使用的模型 ID，推荐使用：

      1. deepseek/deepseek-r1-turbo
      2. deepseek/deepseek-v3-turbo
      3. deepseek/deepseek-v3
      4. qwen/qwq-32b
         其他模型ID、最大上下文及价格可参考：[模型列表](https://ppio.com/model-api/pricing)
2. 设置python环境并安装Agents SDK，输入以下代码即可完成操作

```
python -m venv env
source env/bin/activate
pip install openai-agents==0.0.7
```

## 运行示例

### 示例1：输出功能

运行前，请确保已设置`PPIO API KEY`和`模型名称`环境变量

```python theme={null}
import os
from openai import AsyncOpenAI
from agents import (
    Agent,
    Runner,
    set_default_openai_api,
    set_default_openai_client,
    set_tracing_disabled,
)

BASE_URL = "https://api.ppio.com/openai"
API_KEY = "在此处粘贴 PPIO 官网的 API Key" #此处需修改
MODEL_NAME = "在此输入模型名称" #此处需修改

# 基于PPIO不支持responses API，因此我们使用chat completions API作为示例
set_default_openai_api("chat_completions")
set_default_openai_client(AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY))

# 在此示例中禁用追踪# 如需使用自定义追踪处理器，请参考：https://openai.github.io/openai-agents-python/tracing/#external-tracing-processors-list
set_tracing_disabled(disabled=True)

agent = Agent(name="Assistant",
              instructions="You are a helpful assistant", model=MODEL_NAME)

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.
```

### 示例2：Handoff

运行前，请确保已设置`PPIO API KEY`和`模型名称`环境变量

```python theme={null}
import os
import asyncio
from openai import AsyncOpenAI
from agents import (
    Agent,
    Runner,
    set_default_openai_api,
    set_default_openai_client,
    set_tracing_disabled,
)

BASE_URL = "https://api.ppio.com/openai"
API_KEY = "在此处粘贴PPIO官网的API Key" #此处需修改
MODEL_NAME = "在此输入模型名称" #此处需修改

# 基于PPIO不支持responses API，因此我们使用chat completions API作为示例
set_default_openai_api("chat_completions")
set_default_openai_client(AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY))
# 该示例中禁止使用追踪
#如需使用自定义追踪处理器，请参考：https://openai.github.io/openai-agents-python/tracing/#external-tracing-processors-list to use the custom spans.
set_tracing_disabled(disabled=True)

spanish_agent = Agent(
    name="Spanish agent",
    instructions="You only speak Spanish.",
    model=MODEL_NAME,
)

english_agent = Agent(
    name="English agent",
    instructions="You only speak English",
    model=MODEL_NAME,
)

triage_agent = Agent(
    name="Triage agent",
    instructions="Handoff to the appropriate agent based on the language of the request.",
    handoffs=[spanish_agent, english_agent],
    model=MODEL_NAME,
)

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())
```

### 示例3：函数工具调用

运行前，请确保已设置`PPIO API KEY`和`模型名称`环境变量

```python theme={null}
import os
import asyncio
from openai import AsyncOpenAI
from agents import (
    Agent,
    Runner,
    set_default_openai_api,
    set_default_openai_client,
    set_tracing_disabled,
    function_tool,
)

BASE_URL = "https://api.ppio.com/openai"
API_KEY = "在此处粘贴PPIO官网的API Key" #此处需修改
MODEL_NAME = "在此输入模型名称" #此处需修改

# 基于PPIO不支持responses API，因此我们使用chat completions API作为示例
set_default_openai_api("chat_completions")
set_default_openai_client(AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY))
# 该示例中禁止使用追踪
#如需使用自定义追踪处理器，请参考：https://openai.github.io/openai-agents-python/tracing/#external-tracing-processors-list to use the custom spans.
set_tracing_disabled(disabled=True)

@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],
    model=MODEL_NAME,
)

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())
```
