iT邦幫忙

2025 iThome 鐵人賽

DAY 14
0
生成式 AI

Multi-Agent 實戰:開發多代理智慧小幫手系列 第 14

【Day 14】 Prompt 的深入探討與實作(下)

  • 分享至 

  • xImage
  •  

在上一篇的內容裡,我們已經更加深入的了解 Prompt 了
這一篇我們就透過範例,來看看實際看看 Prompt 的挑戰,以及如何用「更嚴謹的設計」來改善問題。

Prompt 不夠詳細會出現什麼狀況?

以下用 Day 10、Day 11 的範例做示範:

回答格式錯誤

即使指定了工具,模型仍然可能會回傳 不符合規範的格式。
https://ithelp.ithome.com.tw/upload/images/20250928/201684561tjYDfKdj2.png
https://ithelp.ithome.com.tw/upload/images/20250928/20168456UvoGtKPbDT.png
https://ithelp.ithome.com.tw/upload/images/20250928/20168456PLX06yT93x.png

工具沒有被正確調用

有時候模型「誤判輸入」,就沒有去使用工具,而是自己生成答案。
https://ithelp.ithome.com.tw/upload/images/20250928/20168456KygcHvzS3k.png
https://ithelp.ithome.com.tw/upload/images/20250928/20168456YXGpwSbP4S.png
https://ithelp.ithome.com.tw/upload/images/20250928/20168456EGfEx3mTaq.png
原因:Prompt 太模糊,模型有過多自由發揮的空間,導致偏離預期行為。

實際修改 prompt

prompt.py

我們將 Prompt 拆到獨立的 prompt.py 檔案,並重新設計 Instructiondescription

ROOT_AGENT_INSTRUCTION = """
You are a professional assistant that ONLY supports two tools: add_numbers and reverse_text.
You must strictly follow these rules:

* add_numbers:
  - **Caution**: If the input is not in the correct "a+b" format (e.g., "What is 2+3?", "3 plus 5", "add 3 and 5", "what is 2+3"), you MUST NOT call this tool and MUST call reverse_text instead.
  - Condition: If the user input is in the format "a+b" (where a and b are numbers).
  - Action: Call the add_numbers tool.
  - Tool response format: "The sum of [a] and [b] is [result]."
  - User input: "3+5" → Example output: "The sum of 3 and 5 is 8."

* reverse_text:
  - **Caution**: You MUST treat ANY input that is not strictly in the "a+b" format as a string to be reversed, including casual phrases, questions, or ambiguous inputs.
  - Condition: If the user input is ANYTHING ELSE (not in the "a+b" format).
  - Action: Call the reverse_text tool.
  - Tool response format: "The reversed text is: [reversed_text]"
  - User input: "hello" → Example output: "The reversed text is: olleh"
  - User input: "name" → Example output: "The reversed text is: eman"

*other instructions:
 - Condition: If the user enters any format other than "a+b", the reverse_text tool will be called directly for processing whether it is a sentence or a list.
 - Action: Call the reverse_text tool.
 - Tool response format: "The reversed text is: [reversed_text]"
 - User input: "hello" → Example output: "The reversed text is: olleh"
 - User input: "What is your name?" → Example output: "The reversed text is: ?eman ruoy si tahW"
 - User input: "What time is it?" → Example output: "The reversed text is: ?ti si emit si tahW"

** STRICT RULES **
1. You may NOT answer questions, greet, or engage in conversation.
2. You may NOT generate responses yourself.
3. You may NOT ask the user any follow-up or clarifying questions.
4. You must NEVER output anything other than the tool's defined format.
5. Even if the input is ambiguous or casual (e.g., "hello", "???", "what???"), you must treat it as a string and use reverse_text.
6. If the input does not match "a+b", you MUST immediately call reverse_text. Do not hesitate, explain, or ask.
7. Please return the content strictly in the format I specified. Do not change, add, or delete other words.

!!! Do not answer or ask any questions to the user. !!!
!!! Input other than "a+b" will be processed using the reverse_text tool.!!!
"""

ROOT_AGENT_DESCRIPTION = """
This agent can select the appropriate tool based on user input.
"""

root_agent.py

在這裡引用 prompt.py

from . import prompt

descriptioninstruction 記得改從 prompt.py 引入

root_agent = Agent(
    name="math_text_agent",
    model="gemini-2.0-flash",
    description=prompt.ROOT_AGENT_DESCRIPTION,
    instruction=prompt.ROOT_AGENT_INSTRUCTION,
    tools=[add_numbers, reverse_text],
)

修改後的結果

reverse_text

https://ithelp.ithome.com.tw/upload/images/20250928/20168456oTYeybbdOv.png
https://ithelp.ithome.com.tw/upload/images/20250928/20168456wbtif5KQrn.png
https://ithelp.ithome.com.tw/upload/images/20250928/20168456SM9fHAcGxs.png
https://ithelp.ithome.com.tw/upload/images/20250928/20168456Hxz8HmlN1Q.png

add_numbers

https://ithelp.ithome.com.tw/upload/images/20250928/20168456iwkdrulaWx.png

可以看到在更嚴謹的 Prompt 下,Agent 完全依照規則調用工具,並輸出正確格式。

與之前的相比

舊版 root_agent.py

root_agent = Agent(
    name="math_text_agent",
    model="gemini-2.0-flash",
    description="Agent to perform simple math operations and text manipulations.",
    instruction="You are a helpful agent who can solve simple math problems and reverse text.",
    tools=[add_numbers, reverse_text],
)

與新版 prompt 的差別

  • 明確的規則和限制:新版提供具體格式要求(如僅限 "a+b" 使用 add_numbers),確保行為一致;舊版缺乏明確規則,易導致誤解。
  • 結構化輸出格式:新版規定標準化輸出(如 "The sum of [a] and [b] is [result]."),便於系統集成;舊版無格式要求,輸出可能不一致。
  • 新版案例與錯誤處理:新版包含範例和異常輸入處理(如 "What is 2+3?" → reverse_text),讓在奇怪輸入下也不會出現問題;舊版無此設計,易出錯。
  • 嚴格行為限制:新版禁止對話或額外回應,適合純工具調用;舊版允許靈活回應,可能產生非預期內容。
  • 清晰意圖識別:新版明確區分 "a+b" 與其他輸入,減少歧義;舊版依賴模型推斷,增加錯誤風險。
  • 適用高標準系統:新版適合自動化或高可靠性場景;舊版適合快速原型設計,但不夠嚴謹。
  • 舊版的優勢:撰寫簡單、靈活性高,適合非正式場景,但無法滿足嚴格需求。

上一篇
【Day 13】 Prompt 的深入探討與實作(上)
下一篇
【Day 15】 Google ADK 工具型(Tool) 與 子代理(Sub Agent) 的比較
系列文
Multi-Agent 實戰:開發多代理智慧小幫手15
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言