使用 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'; } }}。