

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 檢索增強生成
<a name="jumpstart-foundation-models-customize-rag"></a>

基礎模型通常是離線訓練的，使得模型在訓練模型之後建立的任何資料都是不可知的。此外，基礎模型會在非常一般的網域語料庫上進行訓練，因此對於網域特定任務的效率較低。您可以使用檢索增強生成 (RAG) 從基礎模型外部擷取資料，並透過在上下文中新增相關擷取的資料來擴充提示。如需 RAG 模型架構的詳細資訊，請參閱[檢索增強生成的知識密集型 NLP 任務](https://arxiv.org/abs/2005.11401)。

使用 RAG，用於擴充提示的外部資料可以來自多個資料來源，例如文件儲存庫、資料庫或 API。第一步是將您的文件和任何使用者查詢轉換為相容的格式，以執行相關性搜尋。為了使格式相容，文件集合或知識庫以及使用者提交的查詢會使用嵌入語言模型轉換成數值表示。*嵌入*是在向量空間中將文字以數字表示的過程。RAG 模型架構比較了知識庫向量中的使用者查詢的嵌入。然後，原始使用者提示會附加來自知識庫中類似文件的相關上下文。然後將此增強提示發送到基礎模型。您可以非同步更新知識庫及相關嵌入。

 ![\[A model architecture diagram of Retrieval Augmented Generation (RAG).\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/jumpstart/jumpstart-fm-rag.jpg) 

檢索的文件應大到足以包含有用的內容，以協助增強提示，但小到足以符合提示的最大序列長度。您可以使用任務特定的 JumpStart 模型，例如來自 Hugging Face 的一般文字嵌入 (GTE) 模型，為您的提示和知識程式庫文件提供嵌入。比較提示和文件嵌入以尋找最相關的文件後，使用補充內容建構新的提示。然後，將增強的提示傳遞至您選擇的文字產生模型。

## 範例筆記本
<a name="jumpstart-foundation-models-customize-rag-examples"></a>

如需 RAG 基礎模型解決方案的詳細資訊，請參閱下列範例筆記本：
+ [檢索增強生成：使用 LangChain 和 Cohere 生成和 SageMaker JumpStart 嵌入模型回答問題](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/question_answering_retrieval_augmented_generation/question_answering_Cohere+langchain_jumpstart.html)
+ [檢索增強生成：使用 LLama-2、Pinecone 和自訂資料集回答問題](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/question_answering_retrieval_augmented_generation/question_answering_pinecone_llama-2_jumpstart.html)
+ [檢索增強生成：以開放原始碼的 LangChain 程式庫的自訂資料集為基礎回答問題](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/question_answering_retrieval_augmented_generation/question_answering_langchain_jumpstart.html)
+ [檢索增強生成：以自訂資料集為基礎回答問題](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/question_answering_retrieval_augmented_generation/question_answering_jumpstart_knn.html)
+ [檢索增強生成：使用 Llama-2 和文字嵌入模型回答問題](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/question_answering_retrieval_augmented_generation/question_answering_text_embedding_llama-2_jumpstart.html)
+ [Amazon SageMaker JumpStart - 文字嵌入與句子類似性](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/question_answering_retrieval_augmented_generation/text-embedding-sentence-similarity.html)

您可以複製 [Amazon SageMaker AI 範例儲存庫](https://github.com/aws/amazon-sagemaker-examples/tree/main/introduction_to_amazon_algorithms/jumpstart-foundation-models)，以在 Studio 內您選擇的 Jupyter 環境中執行可用的 JumpStart 基礎模型範例。如需可用於在 SageMaker AI 中建立和存取 Jupyter 之應用程式的詳細資訊，請參閱[Amazon SageMaker Studio 中支援的應用程式](studio-updated-apps.md)。