For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
主导航

从提示对象迁移

将托管提示对象的使用迁移到应用程序代码中。

OpenAI 正在弃用 API 中可复用的提示对象。自 2026 年 6 月 3 日起, 提示创建功能将不再作为重点,而 v1/prompts 计划于 2026 年 11 月 30 日 关闭。请参阅弃用信息 页面,了解最新的 时间安排。

要停止使用 OpenAI API 平台中的 提示 功能,请将提示内容从托管的 prompt 对象迁移到您的应用程序代码中。这样,您就能更好地掌控审查、测试、部署和版本管理。

迁移前:使用提示对象

使用提示对象
import OpenAI from "openai";

const client = new OpenAI();

const response = await client.responses.create({
  prompt: {
    id: "pmpt_123",
    version: "1",
    variables: {
      customer_name: "Acme",
      issue: "billing question",
    },
  },
});

迁移后:在代码中内联提示

在代码中内联提示
import OpenAI from "openai";

const client = new OpenAI();

const response = await client.responses.create({
  model: "gpt-6-astra",
  input: [
    {
      role: "system",
      content:
        "You are a helpful support assistant. Be concise, accurate, and friendly.",
    },
    {
      role: "user",
      content:
        "Customer name: Acme. Issue: billing question. Write a response to the customer.",
    },
  ],
});

console.log(response.output_text);

使用 Codex 进行迁移

使用 OpenAI Developers 插件OpenAI Docs 技能自动完成迁移,加快使用 OpenAI API 进行开发的速度。

$openai-docs update this project to store prompts in code instead of using a prompts object

需要做哪些调整

将提示文本存储在您的代码库中,并在 Responses API 调用中将生成的消息直接作为 input 传入,不再在 API 请求中引用已保存的提示对象。

  • 将提示内容迁移到源代码中 ,让提示变更与产品逻辑变更遵循相同的审查和发布流程。
  • 用函数参数替代提示变量 ,让应用程序中的动态值以显式方式传入,并具有明确的类型。
  • 在 Responses API 调用中通过 input 传递消息 ,不再使用 prompt 对象。
  • 将版本管理迁移到您的代码仓库 ,使用 git 提交、PR 审查以及测试或评测进行管理。
  • 将静态内容放在前面,动态内容放在后面 ,以保留提示缓存带来的优势,因为缓存命中依赖于前缀的精确匹配。

示例

使用辅助函数构建提示
import OpenAI from "openai";

const client = new OpenAI();

function buildSupportPrompt({ customerName, issue }) {
  return [
    {
      role: "system",
      content:
        "You are a helpful support assistant. Be concise, accurate, and friendly. Do not invent policy details.",
    },
    {
      role: "user",
      content: `Customer name: ${customerName}. Issue: ${issue}. Write a response to the customer.`,
    },
  ];
}

const response = await client.responses.create({
  model: "gpt-6-astra",
  input: buildSupportPrompt({
    customerName: "Acme",
    issue: "billing question",
  }),
});

迁移带来的好处

您可以更严格地管控工程流程:提示与产品代码存放在一起,变更通过 PR 进行,测试和评测可以在 CI 中运行,而发布或实验则可以通过您自己的配置或功能开关进行管理。

不要将提示以内联形式分散在代码库各处。请创建一个小型的 prompts/ 模块,为每个提示定义一个具名的构建函数,并添加轻量级评测夹具,让提示变更像产品逻辑变更一样接受审查。