

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

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

基础模型通常是离线训练的，这使得模型不了解在模型训练后创建的任何数据。此外，基础模型在非常通用的领域语料库上训练，这使得它们在特定于领域的任务中的效率较低。您可以使用检索增强生成 (RAG) 在基础模型的外部检索数据，并通过在上下文中添加检索到的相关数据来增强提示。有关 RAG 模型架构的更多信息，请参阅[知识密集型 NLP 任务的检索增强生成](https://arxiv.org/abs/2005.11401)。

使用 RAG，用于增强提示的外部数据可以来自多个数据源，例如文档存储库、数据库或。 APIs第一步是将您的文档和任何用户查询转换为兼容的格式，以执行相关性搜索。为了使格式兼容，需要使用嵌入式语言模型，将文档集合或知识库以及用户提交的查询转换为数字表示形式。*嵌入*是在向量空间中对文本进行数字表示的过程。RAG 模型架构比较知识库向量中用户查询的嵌入情况。然后，将知识库中类似文档的相关上下文附加到原始用户提示中。接下来，此增强提示将发送到基础模型。您可以异步更新知识库及其相关嵌入。

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

检索到的文件应足够大，以便包含有用的上下文，帮助增强提示信息，但又应足够小，以适应提示信息的最大序列长度。您可以使用特定于任务的 JumpStart 模型（例如中的通用文本嵌入 (GTE) 模型）来为提示和知识库文档提供嵌入内容。Hugging Face在比较提示和文档嵌入找到最相关的文档后，利用补充上下文构建新的提示。然后，将增强后的提示传递给您选择的文本生成模型。

## 示例笔记本
<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 A SageMaker I 示例存储库](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)