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

有哪些變更

不再從 API 請求中參照已儲存的提示詞物件,而是將提示詞文字儲存在程式碼庫中,並在呼叫 Responses API 時,直接將產生的訊息作為 input 傳入。

  • 將提示詞內容移至原始碼中 ,讓提示詞的變更與產品邏輯遵循相同的審查和發布流程。
  • 以函式引數取代提示詞變數 ,讓應用程式中的動態值明確且具有型別。
  • 呼叫 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/ 模組,以具名的建構函式定義每個提示詞,並加入輕量的評估測試資料,讓提示詞變更能像產品邏輯一樣接受審查。