問(wèn)題改寫(xiě)的提示詞提升多跳問(wèn)題的檢索效果,用戶輸入部分放到提示詞最后
在使用大模型處理多跳問(wèn)題(multi-hop question)時(shí),我們常常面臨一個(gè)挑戰(zhàn):原始問(wèn)題可能不夠具體或缺乏關(guān)鍵實(shí)體信息,導(dǎo)致語(yǔ)義搜索系統(tǒng)難以準(zhǔn)確檢索到相關(guān)答案。為了解決這個(gè)問(wèn)題,現(xiàn)在大家常使用問(wèn)題改寫(xiě),獲取深層次的知識(shí)。下述是一套有效的問(wèn)題改寫(xiě)提示詞(prompt),專門用于“問(wèn)題改寫(xiě)”階段,幫助模型生成更清晰、更具實(shí)體導(dǎo)向的新問(wèn)題。
這套提示詞經(jīng)過(guò)實(shí)際測(cè)試,效果不錯(cuò)。
實(shí)驗(yàn)效果
qwen-2.5-7B 作為問(wèn)題改寫(xiě)的大模型。在 hotpot 數(shù)據(jù)集上的測(cè)試,1000條數(shù)據(jù)構(gòu)建向量數(shù)據(jù)庫(kù):
- 直接使用用戶問(wèn)題在向量數(shù)據(jù)庫(kù)中做召回TopK@10 hite_rate命中率 可以達(dá)到82%左右。
- 使用問(wèn)題改寫(xiě)后,TopK@10 + TopK@10 hite_rate命中率 可以達(dá)到91%左右。
其實(shí) qwen-2.5-7B的問(wèn)題改寫(xiě)能力不強(qiáng),如果你不使用下述提示詞,會(huì)發(fā)現(xiàn)很多問(wèn)題改寫(xiě)都失敗了,無(wú)法獲得下一步的信息。但 一些強(qiáng)大的大模型比如 gpt-4o等大參數(shù)的模型表現(xiàn)很好。
使用下述問(wèn)題改寫(xiě)的提示詞達(dá)到的效果,可以與gpt-4o問(wèn)題改寫(xiě)相媲美!
一、提示詞設(shè)計(jì)思路詳解
以下是我在項(xiàng)目中使用的提示詞模板,專門用于引導(dǎo)大模型進(jìn)行高質(zhì)量的問(wèn)題改寫(xiě):
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()
這個(gè)提示詞的設(shè)計(jì)有幾個(gè)關(guān)鍵點(diǎn):
- 分步指導(dǎo)明確:從理解原問(wèn)題、提取關(guān)鍵信息,到構(gòu)造新問(wèn)題,每一步都有清晰指引。
- 強(qiáng)調(diào)實(shí)體信息:要求必須包含從支持文本中提取的關(guān)鍵實(shí)體,如人名、地名等。
- 輸出格式規(guī)范:以 JSON 格式返回結(jié)果,便于后續(xù)解析和集成到系統(tǒng)中。
- 示例輔助理解:通過(guò)一個(gè)具體例子幫助模型更好地理解任務(wù)目標(biāo)。
二、為什么這個(gè)提示詞如此有效?
根據(jù)我的實(shí)踐觀察,以下幾點(diǎn)是該提示詞成功的核心原因:
1. 實(shí)體抽取能力增強(qiáng)
多跳問(wèn)題往往需要模型在多個(gè)文檔之間跳躍推理,而原始問(wèn)題通常模糊不清。通過(guò)要求模型從支持文本中提取關(guān)鍵實(shí)體(如人名、時(shí)間、地點(diǎn)等),可以顯著提高問(wèn)題的具體性。
例如,在“誰(shuí)是2010年選秀狀元?”這個(gè)問(wèn)題中,如果支持文本指出“Sam Bradford 是 2010 年選秀狀元”,那么新的問(wèn)題就可以被改寫(xiě)為“Sam Bradford 效力于哪支 NFL 球隊(duì)?”,這顯然更容易被搜索引擎識(shí)別并定位答案。
2. 輸出格式統(tǒng)一,利于自動(dòng)化處理
在提示詞中給了一個(gè)例子, 讓大模型根據(jù)相關(guān)文本中的信息,完成原始問(wèn)題中未知內(nèi)容的替換
三、實(shí)戰(zhàn)案例演示
我們來(lái)看一個(gè)具體的例子,感受一下這套提示詞的實(shí)際效果:
原始問(wèn)題: “《百年孤獨(dú)》這本書(shū)的作者,還寫(xiě)過(guò)哪些書(shū)?”
相關(guān)文本: “加布里埃爾·加西亞·馬爾克斯(Gabriel García Márquez)是哥倫比亞著名作家,他創(chuàng)作了《百年孤獨(dú)》。”
按照提示詞的步驟,模型會(huì)這樣推理并生成新問(wèn)題:
- 原問(wèn)題詢問(wèn)的是《百年孤獨(dú)》的作者。
- 支持文本提供了關(guān)鍵信息:“Gabriel García Márquez 創(chuàng)作了這本書(shū)?!?/li>
- 新問(wèn)題是:“Gabriel García Márquez 寫(xiě)過(guò)哪些著名小說(shuō)?” 或者更精確地指向主題:“Gabriel García Márquez 的代表作是什么?”
這樣改寫(xiě)后的問(wèn)題不僅包含了關(guān)鍵實(shí)體,而且更適合搜索引擎檢索,極大提高了找到正確答案的概率。
四、輸入文本放到提示詞最后
上述提示詞是GPT生成的,GPT根據(jù)我的要求潤(rùn)色與修改提示詞的能力特別強(qiáng)。國(guó)內(nèi)相關(guān)的大模型,還是有差距。
我發(fā)現(xiàn)GPT在針對(duì)我的提示詞潤(rùn)色、修改的過(guò)程中,頻繁地把用戶輸入部分的文本放到最后。一開(kāi)始我沒(méi)理解這樣做的原因。
我認(rèn)為,提示詞先提到用戶輸入,再提到輸出要求,更符合邏輯。
但是后面的實(shí)驗(yàn)經(jīng)歷告訴我,用戶的輸入部分的文本,非常推薦放到提示詞的最后。
本地模型在處理長(zhǎng)文本時(shí)會(huì)啟用截?cái)鄼C(jī)制(truncation),若輸入部分的文本過(guò)長(zhǎng),那么提示詞的輸出要求部分就會(huì)被截?cái)鄤h除掉,模型就無(wú)法理解任務(wù)目標(biāo),從而出現(xiàn)無(wú)效輸出甚至續(xù)寫(xiě)輸入內(nèi)容的情況。
因此,建議大家編寫(xiě)提示詞時(shí)將 ??{user_question}?
??和 ??{relevant_texts}?
?放在提示詞末尾,確保該項(xiàng)任務(wù)的邏輯與輸出要求不會(huì)被截?cái)唷?/p>
本文轉(zhuǎn)載自?????AI悠閑區(qū)?????,作者:jieshenai
