如需完整的文件索引,請參閱 llms.txt。在文件頁面的網址末尾加上 .md ,即可取得該頁面的 Markdown 版本。
主要導覽
主題

從提示詞物件遷移至應用程式碼

將原本由平台管理的提示詞移至應用程式碼。

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 請更新這個專案,將提示詞存放在程式碼中,並停止使用提示詞物件

遷移後的變更

原本的做法是在 API 請求中參照已儲存的提示詞物件。遷移後,請將提示詞文字存放在程式碼庫中,並在呼叫 Responses API 時,直接將產生的訊息作為 input 傳入。

  • 將提示詞內容移至原始碼,讓提示詞變更與產品邏輯變更採用相同的審查及發布流程。
  • 以函式引數取代提示詞變數,讓應用程式明確傳入動態值,並為這些值指定型別。
  • 呼叫 Responses API 時,透過 input 傳入訊息,取代原先使用的 prompt 物件。
  • 改在程式碼庫中管理版本,搭配 Git 提交、PR 審查以及測試或評估。
  • 將靜態內容放在前面,動態內容放在後面,以維持提示詞快取的效益,因為只有前綴完全相符時才會命中快取。

範例

使用輔助函式組合提示詞
import OpenAI from "openai";

const client = new OpenAI();

function buildSupportPrompt({
  customerName,
  issue,
}: {
  customerName: string;
  issue: string;
}): OpenAI.Responses.ResponseInput {
  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/ 模組,為每個提示詞各寫一個具名函式來組合內容,並加入輕量的評估用資料,讓提示詞變更和產品邏輯變更採用相同的審查方式。