使用 AI SDK 指定任意模型
开箱即用时,Agents SDK 通过 Responses API 或 Chat Completions API 与 OpenAI 模型配合工作。不过,如果您想使用其他模型,Vercel 的 AI SDK 提供了多种受支持的模型,可通过此适配器引入到 Agents SDK 中。
-
通过安装扩展包来安装 AI SDK 适配器:
Terminal window npm install @openai/agents-extensions -
从 Vercel 的 AI SDK 选择所需的模型包并进行安装:
Terminal window npm install @ai-sdk/openai -
导入适配器和模型以连接到您的智能体:
import { openai } from '@ai-sdk/openai';import { aisdk } from '@openai/agents-extensions'; -
初始化一个模型实例供智能体使用:
const model = aisdk(openai('gpt-5-mini'));
import { Agent, run } from '@openai/agents';
// Import the model package you installedimport { openai } from '@ai-sdk/openai';
// Import the adapterimport { aisdk } from '@openai/agents-extensions';
// Create a model instance to be used by the agentconst model = aisdk(openai('gpt-5-mini'));
// Create an agent with the modelconst agent = new Agent({ name: 'My Agent', instructions: 'You are a helpful assistant.', model,});
// Run the agent with the new modelrun(agent, 'What is the capital of Germany?');传递提供方元数据
Section titled “传递提供方元数据”如果您需要随消息发送提供方特定的选项,请通过 providerMetadata 传递。这些值会直接转发给底层的 AI SDK 模型。例如,以下在 Agents SDK 中的 providerData
providerData: { anthropic: { cacheControl: { type: 'ephemeral'; } }}在使用 AI SDK 集成时会变为
providerMetadata: { anthropic: { cacheControl: { type: 'ephemeral'; } }}