iT邦幫忙

2025 iThome 鐵人賽

DAY 9
0
自我挑戰組

從讀書筆記到可落地 AI:LangChain、LangSmith 與 Agent 工具 30 講系列 第 9

Day 9|Agent Design - Prompt Design - 結構化輸出(3/5)

  • 分享至 

  • xImage
  •  

目標先講清楚:
除了 Pydantic model,當結構較複雜時,建議把 輸出結構與檢核邏輯寫進 prompt(meta‑prompting),並以 analyst / manager 兩階段來保證內容與結構雙重正確。


基礎做法:Pydantic Model + Few‑shot

當任務簡單、欄位少時,直接用 Pydantic 定義範本 + 幾個情境例子即可。

Pydantic 範例(你提供的模型):

class ClarifyWithUser(BaseModel):
    """Schema for user clarification decision and questions."""
    
    need_clarification: bool = Field(
        description="Whether the user needs to be asked a clarifying question.",
    )
    question: str = Field(
        description="A question to ask the user to clarify the report scope",
    )
    verification: str = Field(
        description="Verify message that we will start research after the user has provided the necessary information.",
    )

對應 Prompt 片段(指示 + few‑shot):

These are the messages that have been exchanged so far from the user asking for the report:  

<Messages>  
{messages}  
</Messages>  

Today's date is {date}.  

Assess whether you need to ask a clarifying question, or if the user has already provided enough information for you to start research.  

**IMPORTANT:**  
- If you can see in the messages history that you have already asked a clarifying question, you almost always do not need to ask another one. Only ask another question if absolutely necessary.  
- If there are acronyms, abbreviations, or unknown terms, ask the user to clarify.  

**If you need to ask a question, follow these guidelines:**  
- Be concise while gathering all necessary information.  
- Make sure to gather all the information needed to carry out the research task in a concise, well-structured manner.  
- Use bullet points or numbered lists if appropriate for clarity. Make sure that this uses markdown formatting and will be rendered correctly if the string output is passed to a markdown renderer.  
- Don't ask for unnecessary information, or information that the user has already provided.  

**Respond in valid JSON format with these exact keys:**  
- `"need_clarification": boolean`  
- `"question": "<question to ask the user to clarify the report scope>"`  
- `"verification": "<verification message that we will start research>"`  

**If you need to ask a clarifying question, return:**  
```json
"need_clarification": true,
"question": "<your clarifying question>",
"verification": ""

進階做法:Meta‑Prompting(Analyst / Manager 兩階段)

當輸出結構複雜(巢狀陣列、跨欄位依賴、枚舉、條件欄位)時,單靠 few‑shot 容易鬆散。建議:

  1. system 內定義 JSON 結構契約 + 檢核規則
  2. Analyst 先生成「中介推理欄位」,再由 Manager 審核→產出最終 JSON。
System Prompt

你要協同兩個內部角色完成結構化 JSON產出與檢核:
Analyst-Follow:依規格產出單一 JSON(無多餘文字/反引號)。
Manage-Check:檢核 JSON 與規則、必要時做最小修正,並填寫 QA 報告。最後只輸出最終 JSON。
最終輸出格式(必填欄位,極簡)
{
  "result": {
    "id": "STRING",               // 3-40字母數字底線或破折號
    "title": "STRING",            // 5-120字
    "severity": "Major|Minor",    // 嚴重度
    "steps": ["STRING"]           // 至少1步,去重、去前後空白
  },
  "qa_report": {
    "schema_valid": true,
    "errors": ["STRING"],
    "fixes": ["STRING"]
  }
}
#可替換成其他要輸出的JSON格式

檢核規則
# 依照不同的輸出,規則會不同

1. steps 至少 1 個;空則錯誤。
2. severity="Major" ⇒ title 必須包含 "crash" 或 "data loss"(不分大小寫);否則自動降為 Minor,在 fixes 記錄原因。
3. 去除字串前後空白;steps 去重。
4. 禁止輸出 null/NaN/Infinity;不得多出或遺漏欄位。
5. 只輸出最終 JSON,不得包含 ```json 或任何解說文字。
.....

可用資訊
原始任務:
{query}

接下來要做什麼

接下來會介紹常用到或覺得有趣的Prompt技巧


參考資源

1.State-Of-The-Art Prompting For AI Agents-Y Combinator
(Youtube)

2.Prompt generation
3.langchain-deep_research_from_scratch
4.State-Of-The-Art Prompting For AI Agents-ihower(review)


上一篇
Day 8|Agent Design - Prompt Design - 結構化輸出(2/5)
下一篇
Day 10|Agent Design - Prompt Design - 撰寫詳細而清楚的Prompt技巧(4/5)
系列文
從讀書筆記到可落地 AI:LangChain、LangSmith 與 Agent 工具 30 講11
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言