English
主导航

旧版 API

从 prompt 对象迁移

将托管的 prompt 对象用法移至应用代码中。

To migrate away from 提示词 在 OpenAI API 平台中,将 prompt 内容从托管 prompt 对象中移出,并放入您的应用代码中。这使您能更好地控制审查、测试、部署和版本管理。

之前:使用 Prompt 对象

使用 prompt 对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import OpenAI from "openai";

const client = new OpenAI();

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

之后:在代码中内联 prompt

在代码中内联 prompt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import OpenAI from "openai";

const client = new OpenAI();

const response = await client.responses.create({
  model: "gpt-5.1",
  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.
      `.trim(),
    },
  ],
});

console.log(response.output_text);

使用 Codex 进行迁移

使用 OpenAI Developers 插件 and OpenAI 文档技能 以自动化您的迁移过程,并加速使用 OpenAI API 进行构建。

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

变更内容

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

  • 将 prompt 内容移至源代码中 以便 prompt 的更改能像产品逻辑一样经历相同的审查和发布流程。
  • 将 prompt 变量替换为函数参数 以便动态值在您的应用中是显式且类型安全的。
  • 传入消息 input 在 Responses API 调用中,而不是使用 prompt object.
  • 将版本管理移至您的代码库 使用 git commits、PR 审查以及测试或评估。
  • 静态内容在前,动态内容在后 以保留 prompt 缓存的优势,因为缓存命中依赖于精确的前缀匹配。

示例

使用辅助函数构建 prompt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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.
      `.trim(),
    },
    {
      role: "user",
      content: `
Customer name: ${customerName}
Issue: ${issue}

Write a response to the customer.
      `.trim(),
    },
  ];
}

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

您的收获

您将获得更严格的工程控制:prompt 与产品代码共存,更改需经过 PR,测试和评估可在 CI 中运行,并且发布或实验可以通过您自己的配置或功能标志进行管理。

不要将 prompt 内联分散在整个代码库中。创建一个小型 prompts/ 模块,将每个 prompt 保留为一个命名的构建器函数,并添加轻量级的评估固件,以便 prompt 的更改能像产品逻辑一样被审查。