建置自己的 RAG - Amazon Nova

建置自己的 RAG

在建構自己的檢索增強生成 (RAG) 系統時,您可以利用檢索器系統和產生器系統。檢索器可以是嵌入模型,根據相似性分數識別向量資料庫中的相關區塊。產生器可以是大型語言模型 (LLM),利用模型的功能,根據擷取的結果 (也稱為區塊) 回答問題。在下列各節中,我們將提供有關如何最佳化 RAG 系統提示詞的其他秘訣。

利用系統提示詞

與其他功能一樣,增強系統提示詞可能非常有益。您可以在系統提示詞中定義 RAG 系統描述,概述模型所需的人物角色和行為。

使用模型指示

您可以在系統提示詞中包含專用 "Model Instructions:" 區段,您可以在其中提供模型需要遵循的特定指導方針。例如,您可以列出以下指示:

In this example session, the model has access to search results and a user's question, its job is to answer the user's question using only information from the search results.

Model Instructions: - You should provide concise answer to simple questions when the answer is directly contained in search results, but when comes to yes/no question, provide some details. - In case the question requires multi-hop reasoning, you should find relevant information from search results and summarize the answer based on relevant information with logical reasoning. - If the search results do not contain information that can answer the question, please state that you could not find an exact answer to the question, and if search results are completely irrelevant, say that you could not find an exact answer, then summarize search results. - Remember to add citations to your response using markers like %[1]%, %[2]%, %[3]%, etc for the corresponding passage supports the response.
透過限制指示來規避幻覺

將更多焦點放在指示上,以模型指示的方式明確提及「請勿使用不在搜尋結果中的資訊!」 確認答案以提供的上下文為基礎。

- DO NOT USE INFORMATION THAT IS NOT IN SEARCH RESULTS!
提供輸入查詢,後面緊接搜尋結果

提供輸入查詢,後面緊接檢索器搜尋結果或上下文相關區塊。在 Resource: Search Results: 之後提供區塊結果時,模型效果最佳

{query} Resource: Search Results: {rag_chunks_retreiver_results}
引文

引文可作為有用的參考,參考為回答問題而提供的上下文。引文通常主要用於為 LLM 答案建立基礎。引文用作評估工具,可讓使用者從上下文中參考引用的來源,以評估答案是否仍然忠於提供的資訊。

這是範例提示詞,應新增到系統提示詞中的「模型指示」中,讓模型專注於在答案中產生引文:

- Make sure to always add citations to your response using markers like %[1]%, %[2]%, %[3]%, and for the corresponding passage that supports the response.

您可以將所有先前的建議與下列提示詞範本結合。此範本只會根據擷取的區塊產生輸出。

Role Prompt

系統

在此工作階段中,模型可存取搜尋結果和使用者的問題,您的任務是僅使用搜尋結果中的資訊來回答使用者的問題。

模型指示:

  • 當答案直接包含在搜尋結果中時,您應該針對簡單問題提供簡潔的答案,但在遇到是/否問題時,請提供一些詳細資訊。

  • 如果問題需要多躍點推理,您應該從搜尋結果中找到相關資訊,並根據相關資訊使用邏輯推理來總結答案。

  • 如果搜尋結果不包含可以回答問題的資訊,請陳述您找不到問題的確切答案,如果搜尋結果完全無關,則表示找不到確切答案,然後總結搜尋結果。

  • 請記得針對支援回應的對應段落使用 %[1]%、%[2]%、%[3]%,等標記,將引文新增至回應結尾。

  • 請勿使用不在搜尋結果中的資訊!

使用者

{Query}

資源:搜尋結果:{search_results}

多模態 RAG

在建立多模態 RAG 時,您應該遵守一些額外的最佳實務。

  • 如果影像不是文字密集型 (即自然場景、文字稀疏的投影片、資訊圖表等),則直接使用影像。Amazon Nova 已最佳化,可以處理非文字密集型影像。您不需要在有依據的產生中,為這些影像傳遞額外的文字摘要。

  • 使用文字摘要增強文字密集型影像 (例如 PDF 報告、論文)。對於文字密集型 PDF,最佳方法是同時擷取影像 (PDF) 和對應的文字摘要。文字摘要可協助模型從原始影像的大量文字中識別相關資訊。

  • 讓模型知道您正在傳遞影像。在指示中,您可以新增像是「You will be provided with images and texts from search results」的句子。