問題改寫的提示詞提升多跳問題的檢索效果,用戶輸入部分放到提示詞最后
在使用大模型處理多跳問題(multi-hop question)時,我們常常面臨一個挑戰:原始問題可能不夠具體或缺乏關鍵實體信息,導致語義搜索系統難以準確檢索到相關答案。為了解決這個問題,現在大家常使用問題改寫,獲取深層次的知識。下述是一套有效的問題改寫提示詞(prompt),專門用于“問題改寫”階段,幫助模型生成更清晰、更具實體導向的新問題。
這套提示詞經過實際測試,效果不錯。
實驗效果
qwen-2.5-7B 作為問題改寫的大模型。在 hotpot 數據集上的測試,1000條數據構建向量數據庫:
- 直接使用用戶問題在向量數據庫中做召回TopK@10 hite_rate命中率 可以達到82%左右。
- 使用問題改寫后,TopK@10 + TopK@10 hite_rate命中率 可以達到91%左右。
其實 qwen-2.5-7B的問題改寫能力不強,如果你不使用下述提示詞,會發現很多問題改寫都失敗了,無法獲得下一步的信息。但 一些強大的大模型比如 gpt-4o等大參數的模型表現很好。
使用下述問題改寫的提示詞達到的效果,可以與gpt-4o問題改寫相媲美!
一、提示詞設計思路詳解
以下是我在項目中使用的提示詞模板,專門用于引導大模型進行高質量的問題改寫:
query_rewrite = """
You are given the following four elements:
1. **Original Question**
2. **Relevant Supporting Text(s)**
Your task is to **create a new, better question** that would help a semantic search system (like vector-based retrieval) find relevant information more accurately.
### ?? Follow These Clear Steps:
**Step 1: Understand the original question.**
Identify what the question is asking — focus on the key person, object, or event it refers to.
**Step 2: Extract the key detail from the supporting text.**
Look carefully at the relevant text and **find the most important new information** — especially **names**, dates, roles, or titles.
?? **You must include this key information in the new question.**
**Step 3: Create a natural follow-up question.**
Now, think of a new question that:
* Focuses on the subject identified from the relevant text (e.g., a person).
* Moves the conversation toward what the original question was looking for (but in a clearer or more direct way).
**Step 4: Write the new question clearly and completely.**
Your final question must:
* **Include the key entity or name (e.g., a person) from the relevant text.**
* Be directly connected to the original topic.
* Make it easier for a search system to retrieve the right answer.
### ?? Do Not:
* Leave out key names or details that were introduced in the relevant text.
* Repeat the original question exactly.
### ? Example (Just for Reference - Do Not Copy):
If the original question was:
> "Which team does the quarterback picked first in the 2010 draft play for?"
And the relevant text tells us:
> "Sam Bradford was taken first in the 2010 draft."
Then your new question **must include 'Sam Bradford'** and could be something like:
> "Which NFL team did Sam Bradford play for during the early 2010s?"
### ?? Output Format:
1. A multi-step, logically coherent explanation showing your reasoning process.
2. A json block at the end containing the final inferred question.
{{
"new_question": "Your clearly written, specific, entity-rich question goes here."
}}
### Input Format:
- Question: {user_question}
- Relevant Texts: {relevant_texts}
""".lstrip()
這個提示詞的設計有幾個關鍵點:
- 分步指導明確:從理解原問題、提取關鍵信息,到構造新問題,每一步都有清晰指引。
- 強調實體信息:要求必須包含從支持文本中提取的關鍵實體,如人名、地名等。
- 輸出格式規范:以 JSON 格式返回結果,便于后續解析和集成到系統中。
- 示例輔助理解:通過一個具體例子幫助模型更好地理解任務目標。
二、為什么這個提示詞如此有效?
根據我的實踐觀察,以下幾點是該提示詞成功的核心原因:
1. 實體抽取能力增強
多跳問題往往需要模型在多個文檔之間跳躍推理,而原始問題通常模糊不清。通過要求模型從支持文本中提取關鍵實體(如人名、時間、地點等),可以顯著提高問題的具體性。
例如,在“誰是2010年選秀狀元?”這個問題中,如果支持文本指出“Sam Bradford 是 2010 年選秀狀元”,那么新的問題就可以被改寫為“Sam Bradford 效力于哪支 NFL 球隊?”,這顯然更容易被搜索引擎識別并定位答案。
2. 輸出格式統一,利于自動化處理
在提示詞中給了一個例子, 讓大模型根據相關文本中的信息,完成原始問題中未知內容的替換
三、實戰案例演示
我們來看一個具體的例子,感受一下這套提示詞的實際效果:
原始問題: “《百年孤獨》這本書的作者,還寫過哪些書?”
相關文本: “加布里埃爾·加西亞·馬爾克斯(Gabriel García Márquez)是哥倫比亞著名作家,他創作了《百年孤獨》。”
按照提示詞的步驟,模型會這樣推理并生成新問題:
- 原問題詢問的是《百年孤獨》的作者。
- 支持文本提供了關鍵信息:“Gabriel García Márquez 創作了這本書。”
- 新問題是:“Gabriel García Márquez 寫過哪些著名小說?” 或者更精確地指向主題:“Gabriel García Márquez 的代表作是什么?”
這樣改寫后的問題不僅包含了關鍵實體,而且更適合搜索引擎檢索,極大提高了找到正確答案的概率。
四、輸入文本放到提示詞最后
上述提示詞是GPT生成的,GPT根據我的要求潤色與修改提示詞的能力特別強。國內相關的大模型,還是有差距。
我發現GPT在針對我的提示詞潤色、修改的過程中,頻繁地把用戶輸入部分的文本放到最后。一開始我沒理解這樣做的原因。
我認為,提示詞先提到用戶輸入,再提到輸出要求,更符合邏輯。
但是后面的實驗經歷告訴我,用戶的輸入部分的文本,非常推薦放到提示詞的最后。
本地模型在處理長文本時會啟用截斷機制(truncation),若輸入部分的文本過長,那么提示詞的輸出要求部分就會被截斷刪除掉,模型就無法理解任務目標,從而出現無效輸出甚至續寫輸入內容的情況。
因此,建議大家編寫提示詞時將 ??{user_question}?
??和 ??{relevant_texts}?
?放在提示詞末尾,確保該項任務的邏輯與輸出要求不會被截斷。
本文轉載自?????AI悠閑區?????,作者:jieshenai
