主导航

遗留 API

从提示词对象 (Prompt objects) 迁移

将托管式提示词对象的使用方式迁移至应用程序代码中。

要从 OpenAI API 平台的提示词 (Prompts) 迁移,请将提示词内容从托管的 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",
    },
  },
});

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

在代码中内联提示词
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 插件OpenAI Docs skill 来自动化迁移流程,并加速基于 OpenAI API 的开发。

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

变更内容

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

  • 将提示词内容移入源代码,以便提示词的变更能够经历与产品逻辑相同的审查和发布流程。
  • 用函数参数替换提示词变量,从而使动态值在您的应用程序中更加明确且类型化。
  • 在 Responses API 调用中通过 input 传入消息,而不是使用 prompt 对象。
  • 使用 git 提交、PR 审查以及测试或评估 (evals),将版本控制迁移至您的代码库中
  • 优先保留静态内容,将动态内容置后,以维持提示词缓存 (Prompt caching) 的优势,因为缓存命中取决于精确的前缀匹配。

示例

使用辅助函数构建提示词
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",
  }),
});

您将获得什么

您将获得更严谨的工程管控:提示词与产品代码共存,变更需经过 PR,测试和评估可在 CI 中运行,发布或实验可通过您自己的配置或功能标志 (feature flags) 进行管理。

不要将提示词分散内联在代码库各处。创建一个小型 prompts/ 模块,将每个提示词作为一个具名的构建器函数保留,并添加轻量级的评估夹具 (eval fixtures),以便像审查产品逻辑一样审查提示词的变更。

© . This website operates independently and is not affiliated with or endorsed by OpenAI, Inc. All brand names, logos, and trademarks are the property of their respective owners.