跳转到内容

使用 AI SDK 指定任意模型

开箱即用,Agents SDK 可通过 Responses API 或 Chat Completions API 使用 OpenAI 模型。不过,如果您想使用其他模型,Vercel 的 AI SDK 提供了一系列受支持的模型,可通过此适配器引入到 Agents SDK 中。

  1. 通过安装扩展包来安装 AI SDK 适配器:

    Terminal window
    npm install @openai/agents-extensions
  2. Vercel 的 AI SDK 选择并安装所需的模型包:

    Terminal window
    npm install @ai-sdk/openai
  3. 导入适配器和模型以连接到您的智能体:

    import { openai } from '@ai-sdk/openai';
    import { aisdk } from '@openai/agents-extensions';
  4. 初始化一个模型实例供智能体使用:

    const model = aisdk(openai('gpt-5-mini'));
AI SDK 设置
import { Agent, run } from '@openai/agents';
// Import the model package you installed
import { openai } from '@ai-sdk/openai';
// Import the adapter
import { aisdk } from '@openai/agents-extensions';
// Create a model instance to be used by the agent
const model = aisdk(openai('gpt-5-mini'));
// Create an agent with the model
const agent = new Agent({
name: 'My Agent',
instructions: 'You are a helpful assistant.',
model,
});
// Run the agent with the new model
run(agent, 'What is the capital of Germany?');

如果您需要在消息中发送特定于提供方的选项,请通过 providerMetadata 传递。其值会直接转发给底层的 AI SDK 模型。例如,以下在 Agents SDK 中的 providerData

providerData: {
anthropic: {
cacheControl: {
type: 'ephemeral';
}
}
}

在使用 AI SDK 集成时会变为

providerMetadata: {
anthropic: {
cacheControl: {
type: 'ephemeral';
}
}
}