跳到正文
全部文档

代码

OpenAI Agents SDK

Agents SDK 通过聊天补全在 nokeep 上运行。给它一个带有 nokeep 基础 URL 的 AsyncOpenAI 客户端,并把模型包装进 OpenAIChatCompletionsModel。

核对于 2026年10月29日

安装与连接

终端
pip install openai-agents
export NOKEEP_API_KEY="sk-nokeep-test"  # your own key once it connects
agent.py
import os
from openai import AsyncOpenAI
from agents import Agent, OpenAIChatCompletionsModel, set_tracing_disabled

client = AsyncOpenAI(
    base_url="https://nokeep.ai/v1",
    api_key=os.environ["NOKEEP_API_KEY"],
)
set_tracing_disabled(True)  # traces go to OpenAI's dashboard, which a nokeep key can't reach

model = OpenAIChatCompletionsModel(model="deepseek-v4.1-flash-uncensored", openai_client=client)

from agents import Runner

agent = Agent(name="Editor", instructions="You are a blunt editor.", model=model)
result = Runner.run_sync(agent, "Cut this opening line to ten words: ...")
print(result.final_output)

除非另行设置,SDK 会调用 Responses API(/v1/responses),而 nokeep 只提供聊天补全。把模型包装进 OpenAIChatCompletionsModel,才能让它正常工作。

或为所有智能体统一设置一次

Python
import os
from openai import AsyncOpenAI
from agents import Agent, set_default_openai_api, set_default_openai_client, set_tracing_disabled

set_default_openai_client(AsyncOpenAI(base_url="https://nokeep.ai/v1", api_key=os.environ["NOKEEP_API_KEY"]), use_for_tracing=False)
set_default_openai_api("chat_completions")
set_tracing_disabled(True)

agent = Agent(name="Editor", instructions="You are a blunt editor.", model="deepseek-v4.1-flash-uncensored")

之后,智能体直接使用字符串形式的模型 ID。智能体之间的交接同样适用。

工具

Python
from agents import Agent, Runner, function_tool

@function_tool
def get_weather(city: str) -> str:
    """Get the weather in a city."""
    return f"18°C and clear in {city}"

agent = Agent(
    name="Weather",
    instructions="Answer questions about the weather.",
    model=model,
    tools=[get_weather],
)
print(Runner.run_sync(agent, "What's the weather in Lisbon?").final_output)

SDK 会以 strict: true 发送每个工具,并运行循环:模型请求调用工具,SDK 运行该工具并把结果送回,直到模型给出回答。参见工具调用。

流式输出与用量

Python
import asyncio
from agents import Runner
from openai.types.responses import ResponseTextDeltaEvent

async def main():
    result = Runner.run_streamed(agent, "Tell me a story in three lines.")
    async for event in result.stream_events():
        if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
            print(event.data.delta, end="", flush=True)
    print("\n", result.context_wrapper.usage)

asyncio.run(main())

SDK 在流式输出时会请求用量信息,因此 context_wrapper.usage 保存整次运行的 token 计数,即每次模型调用的总和。

推理强度与其他设置

Python
from agents import Agent, ModelSettings
from openai.types.shared import Reasoning

planner = Agent(
    name="Planner",
    instructions="Plan before you answer.",
    model=model,
    model_settings=ModelSettings(
        reasoning=Reasoning(effort="high"),  # sent as reasoning_effort
        max_tokens=4000,
        extra_body={"min_p": 0.05},  # fields ModelSettings has no argument for
    ),
)

思考内容计入 max_tokens,所以要为会思考的智能体留出足够空间。参见思考。

注意事项

  • `reasoning_effort` 只接受模型自身的取值。 deepseek-v4.1-flash-uncensored 接受 none、low、high、max;glm-5.3-flash-uncensored 接受 low、high。其他应用传入的 minimal、medium 和 xhigh 会按模型最接近的档位运行(medium 按 high,模型支持时 xhigh 按 max);任何其他值都会返回 400 invalid_reasoning_effort。参见思考。
  • GLM 自己选择工具。 glm-5.3-flash-uncensored 只接受 tool_choice 为 auto 或 none。强制调用工具(required,或指定某个函数名)会返回 400 unsupported_tool_choice。需要强制调用时请用 DeepSeek。在 SDK 中,即 ModelSettings(tool_choice="required"),或指定工具名。
  • 模型不支持的字段会被丢弃,而不是拒绝。 调用仍会返回结果,响应头 x-nokeep-ignored 会列出被丢弃的内容。GLM 只保留 tools、tool_choice 和 reasoning_effort,因此它的 temperature 和 response_format 会被丢弃。参见各模型支持的字段。
  • 只支持聊天补全。 没有 /v1/responses,也没有 /v1/embeddings;默认使用其中之一的框架需要改为指向聊天补全,而嵌入功能需要另一家提供方。

托管工具,例如网页搜索、文件搜索和代码解释器,通过 Responses API 在 OpenAI 自己的服务器上运行。它们无法用于其他任何 API,包括 nokeep。请将它们写成函数工具。

常见问题

我还需要 OpenAI 密钥吗?

不需要。每次模型调用都使用你的 nokeep 密钥发送到 nokeep。追踪数据会上传到 OpenAI 的控制台;若开启了追踪却没有设置 OpenAI 密钥,SDK 会打印 OPENAI_API_KEY is not set, skipping trace export,智能体仍会照常运行。set_tracing_disabled(True) 可以让它保持安静。

智能体应该使用哪个模型?

deepseek-v4.1-flash-uncensored:它可以被引导去调用工具,能读取图片,并支持所有设置。glm-5.3-flash-uncensored 费用更低,适合让模型自行选择工具的智能体。