Prompting Techniques-Prompt Chaining(提示鏈)

Prompt Chaining(提示鏈)

  • Prompt Chaining(提示鏈)是一種將複雜任務拆分為多個子任務的技術,每個子任務依序執行,上一個步驟的輸出可作為下一個步驟的輸入。
  • 它主要用於提升大型語言模型在處理複雜問題時的準確性、可控性與透明性

目錄


Prompt Chaining 的優勢

  • 可控性高:每個步驟可單獨調整與測試,避免整體出錯。
  • 易於除錯:能檢視每一步輸出,方便追蹤問題來源。
  • 適合複雜任務:模型可逐步推理或生成,避免一次性錯誤。
  • 模組化重用:部分子任務可複用於不同應用場景。

如何設計與實作 Prompt Chaining

1. 鏈式結構類型

  • 線性鏈 (Linear Chain):步驟依序執行,適用於簡單的分解任務。
  • 分支鏈 (Branching Chain):一步輸出分流為多個方向處理。
  • 遞迴鏈 (Recursive Chain):重複執行直到滿足條件。
  • 條件鏈 (Conditional Chain):依據條件決定下一步行為。

2. 設計流程

  1. 拆解任務:將複雜任務分為明確的小步驟。
  2. 設計提示:為每一步撰寫明確指令。
  3. 傳遞上下文:讓前一步輸出作為後一步的輸入。
  4. 測試與調整:逐步驗證每個鏈節點的輸出品質。

例子:

任務  
撰寫一篇關於「高爾夫規則」的短文,並將其翻譯成繁體中文,再生成三個測驗題目。

Step 1:生成高爾夫規則的英文短文
Prompt 1:
Write a short paragraph explaining the basic rules of golf in English.

Output 1:
Golf is a sport where players aim to hit a ball into a series of holes using the fewest strokes possible. Each hole is played once per round, and the player with the lowest total strokes wins. Players use different clubs to hit the ball from the tee to the green and then into the hole.


Step 2:將結果翻譯成繁體中文
Prompt 2(接續前一步輸出):
Translate the following paragraph into Traditional Chinese:  
"Golf is a sport where players aim to hit a ball into a series of holes using the fewest strokes possible. Each hole is played once per round, and the player with the lowest total strokes wins. Players use different clubs to hit the ball from the tee to the green and then into the hole."

Output 2:
高爾夫是一項運動,玩家的目標是用最少的桿數將球打進一系列的洞中。每一輪比賽每個洞只打一次,總桿數最低的選手獲勝。球員使用不同的球桿將球從開球台擊到果嶺,再將球打入洞中。

---

Step 3:根據中文短文生成三個測驗題目
Prompt 3(接續前一步輸出):
Based on the following Chinese paragraph, create 3 multiple-choice quiz questions (with 4 options each) and indicate the correct answer.

Output 3:
1. 高爾夫比賽的主要目標是什麼?  
   A. 用最多桿數完成比賽  
   B. 用最少桿數完成比賽 (正確)
   C. 盡量將球打遠  
   D. 用最快速度完成比賽  

2. 每一輪比賽中,每個洞打幾次?  
   A. 一次 (正確)
   B. 兩次  
   C. 三次  
   D. 無限制  

3. 球員使用什麼工具來擊球?  
   A. 球棒  
   B. 球拍  
   C. 球桿 (正確)
   D. 球鞋  


常見應用場景

  • 文件問答系統:先擷取關鍵段落,再整合生成答案。
  • 內容創作:生成大綱 → 撰寫初稿 → 編輯潤飾。
  • 資料分析與報告:數據整理 → 分析解讀 → 報告撰寫。
  • 多輪對話系統:先意圖分類 → 再生成回覆 → 進一步推理。

結語

  • Prompt Chaining 是提示工程(Prompt Engineering)中非常重要的技術,能夠將複雜任務拆解,並透過多階段提示提升模型表現。
  • 掌握鏈式提示後,你可設計更高效、可控且容易除錯的 AI 工作流,適合文件問答、內容生成、分析任務與多輪對話等場景。

References

Prompt Engineering Guide
documentation
prompt engineering


目錄:Prompting Techniques

上一篇:Prompting Techniques - Knowledge Generation
下一篇:Prompting Techniques - 思維樹