iT邦幫忙

2025 iThome 鐵人賽

DAY 10
0
自我挑戰組

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

Day 10|Agent Design - Prompt Design - 撰寫詳細而清楚的Prompt技巧(4/5)

  • 分享至 

  • xImage
  •  

目標先講清楚:
當任務變複雜,長而清楚的結構化提示比「一句話魔法」更重要。本文整理 Claude Code 與社群最佳實務(含 parahelp、LangChain),整理一些技巧


Claude 提供的建議

Claude 的建議是幫助讓你跟它的模型進行更有效率的溝通,透過技巧,掌握跟LLM說話的方式;使用Parahelp的prompt當作技巧的範例。
https://ithelp.ithome.com.tw/upload/images/20250925/20178568X74fz3Yq3e.png

1) Be explicit with your instructions(明確指示)

  • 目的:消除推測、避免幻覺或未授權的工具操作。
  • 要點:禁止猜測來源與政策為準動作需對應有效工具

對應片段

- Make sure your description NEVER assumes any information, variables or tool call results...
- Make sure your plan NEVER includes or guesses on information/instructions/rules...
- Make sure you ALWAYS highlight ... that <helpcenter_result> is the source of truth...
- Make sure that the plan always follows the procedures and rules of the # Customer service agent Policy doc
- The action_name should always be the name of a valid tool

2) Add context to improve performance(添加上下文改善表現)

  • 目的:讓模型知道邊界與依據,只「規劃下一步」。
  • 要點:描述場景指向政策文件限定規劃範圍

對應片段

- When planning next steps, make sure it's only the goal of next steps...
- Make sure that the plan always follows the procedures and rules of the # Customer service agent Policy doc
Scenario: The user has error with feature_name and have provided basic information about the error

3) Be vigilant with examples & details(善用範例與細節)

  • 目的:降低歧義、提高可預測性。
  • 要點:提供參考計劃條件分支欄位細節規格

對應片段

_IMPORTANT_: This example of a plan is only to give you an idea...
<plan>
  <step> ... </step>
  <if_block condition='...'> ... </if_block>
</plan>

- The description should be a short description of why the action is needed...
- ...which outputs from other tool calls that should be used (if relevant)
- ...e.g. "reply to the user with instrucitons from <helpcenter_result>"

4) Control the format of responses(控制回應格式)

  • 目的:輸出可被機器解析,利於下游程式與評測。
  • 要點:固定骨架明確欄位名嚴格 XML 語法

對應片段

- A plan consists of steps.
- You can always include <if_block> tags...
- The step should be in the following format:
<step>
  <action_name></action_name>
  <description>{...}</description>
</step>

- Every step can have an if block...
- ...wrapped with the <if_block condition=''></if_block> tags.

5) Leverage thinking & interleaved thinking(運用思考與交錯思考的能力)

  • 目的:把「推理」顯性化,讓上下游工具互餵。
  • 要點:條件分支前步輸出即後步輸入多路徑推演

對應片段

- You can always include <if_block> tags to include different steps based on a condition.
- ...which outputs from other tool calls that should be used (if relevant)
- To create multiple if/else blocks just create multiple <if_block> tags.
- ...think in possible paths and use <tool_calls> as variable names...

XML 標籤能把 政策、步驟、條件、工具 I/O 變成讓模型容易讀懂的格式。


Planning 與 Reflection:怎麼放進 Prompt?

重點:把「計畫」與「反思」寫成明確步驟
使用在 LangChain 的 open_deep_research 當作Reflection的範例
(補充-langchain的這個專案,把Reflect包成一個 think_tool給agent調用的方式很酷)
Plan也是類似的方式,透過how to plan:steps for planning去引導模型

@tool(description="Strategic reflection tool for research planning")
def think_tool(reflection: str) -> str:
    """Tool for strategic reflection on research progress and decision-making.

    Use this tool after each search to analyze results and plan next steps systematically.
    This creates a deliberate pause in the research workflow for quality decision-making.

    When to use:
    - After receiving search results: What key information did I find?
    - Before deciding next steps: Do I have enough to answer comprehensively?
    - When assessing research gaps: What specific information am I still missing?
    - Before concluding research: Can I provide a complete answer now?

    Reflection should address:
    1. Analysis of current findings - What concrete information have I gathered?
    2. Gap assessment - What crucial information is still missing?
    3. Quality evaluation - Do I have sufficient evidence/examples for a good answer?
    4. Strategic decision - Should I continue searching or provide my answer?

    Args:
        reflection: Your detailed reflection on research progress, findings, gaps, and next steps

    Returns:
        Confirmation that reflection was recorded for decision-making
    """
    return f"Reflection recorded: {reflection}"

接下來要做什麼

接下來是Prompt的管理


參考資源

  1. AI prompt design at Parahelp
  2. Claude 4 prompt engineering best practices
  3. langchain-open deep research
  4. Google-Agentic Design Patterns-CH6-Planning
  5. Google-Agentic Design Patterns-CH4-Reflection

上一篇
Day 9|Agent Design - Prompt Design - 結構化輸出(3/5)
下一篇
Day 11|Agent Design - Prompt Design - LangSmith Prompt管理(5/5)
系列文
從讀書筆記到可落地 AI:LangChain、LangSmith 與 Agent 工具 30 講11
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言