在 OpenAI Agent SDK 中使用自定义的 AI Agent

Feb 04, 2026 • 预计阅读时间 1 分钟

OpenAI 的 SDK 可以自定义 base_urlapi_keymodel,这样就可以使用第三方平台提供的大模型能力了,比如我使用的是字节火山引擎的 Kimi 模型,把下面 python 代码中的 OpenRouterConfig 配置换成你自己的信息就可以。

import asyncio
from openai import AsyncOpenAI
from openai import OpenAIError
from agents import Agent, Runner, OpenAIChatCompletionsModel, set_tracing_disabled

# Disable tracing since we're using Azure OpenAI
set_tracing_disabled(disabled=True)

class OpenRouterConfig:
    def __init__(self):
        self.model = "kimi-k2-250905"
        self.key = "0058B9EE-D0C7-43C4-B214-1DF127FCB0C0"
        self.endpoint = "https://openrouter.me/ark/api/v1"

config = OpenRouterConfig()

async def main():
    try:
        # Create the Async Azure OpenAI client
        client = AsyncOpenAI(
            api_key=config.key,
            base_url=config.endpoint
        )

        # Configure the agent with Azure OpenAI
        agent = Agent(
            name="Assistant",
            instructions="You are a helpful assistant",
            model=OpenAIChatCompletionsModel(
                model=config.model,
                openai_client=client,
            )
        )

        result = await Runner.run(agent, "Who are you? ")
        print(result.final_output)

    except OpenAIError as e:
        print(f"OpenAI API Error: {str(e)}")
    except Exception as e:
        print(f"An unexpected error occurred: {str(e)}")

if __name__ == "__main__":
    asyncio.run(main())

参考信息

OpenAI
版权声明:如果转发请带上本文链接和注明来源。

lvv.me

iOS/macOS Developer

使用命令把 ipcc 文件安装到 iOS 上