RAG技术如何提升AI大模型?2026年企业应用全解析
RAG (Retrieval-Augmented Generation) is a technique that enhances large language models by retrieving relevant information from external knowledge sources before generating responses, improving accuracy, reducing hallucinations, and enabling real-time updates for enterprise applications.
原文翻译: RAG(检索增强生成)是一种通过从外部知识源检索相关信息后再生成回答的技术,它增强了大型语言模型的能力,提高了准确性,减少了幻觉,并为企业应用实现了实时更新。
一、认识RAG
1、简介
在自然语言处理领域,大型语言模型(如GPT系列、Claude系列、Meta的LLaMA系列、谷歌的Gemini和PaLM以及阿里的通义千问等)已经取得了突破性的成就,并在多个基准测试中表现出色。然而,它们在处理行业领域或高度专业化的知识时仍存在局限,有时会产生“幻觉”,且知识更新通常滞后一年左右。
In the field of natural language processing, large language models (such as the GPT series, Claude series, Meta's LLaMA series, Google's Gemini and PaLM, and Alibaba's Tongyi Qianwen) have achieved groundbreaking success and performed excellently in various benchmark tests. However, they still have limitations when dealing with industry-specific or highly specialized knowledge, sometimes generating "hallucinations," and their knowledge is typically updated with a lag of about a year.
在行业或公司内部业务场景中,数据的持续更新至关重要,以确保信息的时效性。同时,生成的内容需要透明且可追溯,这不仅有助于成本控制,也能更好地保护数据隐私。
In industry or internal corporate business scenarios, continuous data updates are crucial to ensure the timeliness of information. Additionally, the generated content needs to be transparent and traceable, which not only aids in cost control but also better protects data privacy.
本文参考了同济大学、复旦大学等机构联合发表的一篇RAG综述论文《Retrieval-Augmented Generation for Large Language Models: A Survey》:
论文链接:https://arxiv.org/abs/2312.10997
GitHub项目:https://github.com/Tongji-KGLLM/RAG-Survey
This article references a comprehensive RAG survey paper co-authored by institutions such as Tongji University and Fudan University, titled "Retrieval-Augmented Generation for Large Language Models: A Survey":
Paper link:https://arxiv.org/abs/2312.10997
GitHub project:https://github.com/Tongji-KGLLM/RAG-Survey
2、RAG起源
RAG技术大约在2020年左右出现,代表了LLM领域内增强生成任务的一种新范式。它通过结合检索(Retrieval)和生成(Generation) 两大核心技术,增强了大型语言模型的功能。
RAG technology emerged around 2020, representing a new paradigm for enhancing generation tasks within the LLM field. It augments the capabilities of large language models by combining two core technologies: Retrieval and Generation.
RAG技术在语言模型生成答案前,先从文档数据库中检索出相关信息,作为上下文被注入到LLM的提示中,然后让大模型总结生成,从而提高了内容的准确性和相关性。该技术减轻了幻觉问题,加快了知识更新速度,并增强了生成内容的可追溯性,使大型语言模型在实际应用中更加高效、可靠。
Before the language model generates an answer, RAG technology first retrieves relevant information from a document database, injects it as context into the LLM's prompt, and then lets the large model summarize and generate. This improves the accuracy and relevance of the content. This technology mitigates the hallucination problem, accelerates knowledge updates, and enhances the traceability of generated content, making large language models more efficient and reliable in practical applications.
RAG技术的引入,不仅解决了生成“幻觉”的问题,而且成为行业知识AI落地、企业私有知识库、AI搜索的关键技术。加上当前开源模型(如Llama 3.1、Qwen2、Gemma2、Mistral等)性能出色,RAG技术的发展成为一种结合了RAG和微调优势的混合方法。向量搜索领域也因RAG的火热而迅速发展,像Chroma、Weaviate.io、Pinecone等向量数据库A database system designed to store and perform high-dimensional semantic similarity searches on vector embeddings of data.初创公司都以开源搜索索引(主要是FAISS和NMSLIB)为基础。基于大模型的多个著名开源库,包括LangChain、LlamaIndex、AutoGen、MetaGPT等,也推动了RAG的普及。
The introduction of RAG technology not only addresses the issue of generation "hallucinations" but also becomes a key technology for industry knowledge AI implementation, enterprise private knowledge bases, and AI search. Coupled with the excellent performance of current open-source models (such as Llama 3.1, Qwen2, Gemma2, Mistral, etc.), the development of RAG technology has become a hybrid approach combining the advantages of both RAG and fine-tuning. The vector search field has also rapidly developed due to the popularity of RAG. Vector database startups like Chroma, Weaviate.io, and Pinecone are built upon open-source search indexes (primarily FAISS and NMSLIB). Several well-known open-source libraries based on large models, including LangChain, LlamaIndex, AutoGen, and MetaGPT, have also contributed to the popularization of RAG.
3、RAG流程
一个典型的RAG流程包含以下步骤:
A typical RAG process consists of the following steps:
- 知识提取:首先,从用户给定的文档、图片、表格和外部URL等资源中提取内容。这涉及到PDF数据提取、表格数据提取、图片OCR识别后的数据提取,以及网页文档数据的提取,包括结构化数据和非结构化数据。
Knowledge Extraction: First, extract content from user-provided resources such as documents, images, tables, and external URLs. This involves PDF data extraction, table data extraction, data extraction from images after OCR recognition, and web document data extraction, covering both structured and unstructured data.
- 知识索引:然后通过分块(Chunking) 进行合理切割,再使用嵌入(Embedding) 将文本转化为向量数据存入向量数据库A database system designed to store and perform high-dimensional semantic similarity searches on vector embeddings of data.或Elasticsearch等数据库中。结合这些非结构化文件所附带的元数据(时间、文件名、作者、副标题、文件类型等)进行索引创建(高级索引可以是树结构、图结构等)。
Knowledge Indexing: Then, perform reasonable segmentation through Chunking, and use Embedding to convert text into vector data, storing it in vector databases or databases like Elasticsearch. Create indexes by combining the metadata attached to these unstructured files (time, filename, author, subtitle, file type, etc.). Advanced indexing can involve tree structures, graph structures, etc.
- 知识检索:当RAG接收到用户提问内容时,先将内容通过Embedding转化为向量数据,然后与前面建立的索引进行相似度匹配。系统优先检索与查询最相似的前K个块,比如从百万个块中找出匹配度较高的100个。然后再将这100个块进行更耗时但也更精准的重排序(使用交叉熵校验的Rerank算法),找到最相关的Top 3结果。
Knowledge Retrieval: When RAG receives a user's query, it first converts the content into vector data via Embedding, then performs similarity matching with the previously built index. The system prioritizes retrieving the top K chunks most similar to the query, for example, finding the 100 most matching chunks from millions. Then, these 100 chunks undergo a more time-consuming but more precise re-ranking (using a Rerank algorithm with cross-entropy verification) to find the top 3 most relevant results.
- 生成:最后,RAG将用户问题、经过技术处理的Top 3块,以及提示词(Prompt)一起送入大语言模型,让它生成最终的答案。
Generation: Finally, RAG feeds the user's question, the technically processed top 3 chunks, and the prompt together into the large language model to generate the final answer.
4、RAG优缺点
(1)RAG的优势
RAG结合大语言模型,可以解决大语言模型本身存在的4个主要问题:
RAG, combined with large language models, can address four main issues inherent to LLMs themselves:
- 私有数据和时效性:大语言模型可以通过预训练和监督微调(SFT)将私有数据(如公司的业务文件等)压缩到模型中。预训练和微调成本都比较高,实时性也不强,技术门槛也高不少,与企业内部最新鲜私有数据进行对话还是存在很大难度的。而RAG则采用“外脑”的方式处理这个问题,只要用户将最新的资料上传,即可与这些文件进行对话,获取想要的答案,从而快速提升知识的时效性。
Private Data and Timeliness: Large language models can compress private data (such as a company's business documents) into the model through pre-training and supervised fine-tuning (SFT). However, both pre-training and fine-tuning are costly, lack real-time capability, and have a high technical barrier, making it challenging to converse with the freshest private data within an enterprise. RAG addresses this problem using an "external brain" approach. As long as users upload the latest materials, they can converse with these files to obtain desired answers, thereby rapidly enhancing knowledge timeliness.
- 幻觉问题:通过检索相关知识,RAG能够提供更准确、更相关的答案,减少模型的幻觉现象。在涉及公司最新动态或数据权限问题时,可以在检索阶段提供简单的权限控制,甚至可以设置在检索召回内容质量不高的情况下,禁止大模型介入,而是直接回复“不知道”或“没有权限”。
Hallucination Problem: By retrieving relevant knowledge, RAG can provide more accurate and relevant answers, reducing the model's hallucination phenomena. When dealing with a company's latest developments or data permission issues, simple permission control can be implemented during the retrieval stage. It can even be configured to prohibit the large model from intervening if the quality of retrieved content is low, instead directly replying with "I don't know" or "no permission."
- 增强可解释性:由于RAG生成的文本基于可检索的知识,因此用户可以验证答案的准确性,并增加对模型输出的信任。这在行业领域和专业领域(如医疗健康、教育、法律、金融等)非常必要。
Enhanced Explainability: Since RAG-generated text is based on retrievable knowledge, users can verify the accuracy of answers and increase trust in the model's output. This is essential in industry and professional fields such as healthcare, education, law, and finance.
- 数据安全问题:对于数据安全要求极高的企业用户,如果不想使用在线大语言模型(如ChatGPT、通义千问、文心一言等),那么可以采用完全本地化部署将AI模型和系统部署在企业内部网络,确保数据不出内网,在RAG中用于满足高数据安全要求的企业场景。。RAG配合百亿级别参数的可本地部署大模型即可提供绝大多数AI服务,同时确保企业数据不出内网。另外,访问权限控制也是一个重要角度,我们肯定不希望新来的实习生也能通过对话获取公司未发布的财务数据,这些都是RAG作为LLM的“外脑”可以做到的事情。
Data Security: For enterprise users with extremely high data security requirements who do not wish to use online large language models (such as ChatGPT, Tongyi Qianwen, Wenxin Yiyan, etc.), fully localized deployment can be adopted. RAG, combined with locally deployable large models with tens of billions of parameters, can provide the majority of AI services while ensuring enterprise data remains within the internal network. Additionally, access control is another important aspect. We certainly do not want new interns to be able to access unpublished financial data through conversation. These are things RAG, as an "external brain" for LLMs, can achieve.
(2)RAG缺点
- 性能和效率问题:RAG技术在实际应用中,尤其是在数据丰富且复杂的企业环境中,可能会遇到性能和效率的问题。例如,检索数据量大时比较耗时,大模型的多轮对话和评分也比较费时间。
Performance and Efficiency Issues: In practical applications, especially in data-rich and complex enterprise environments, RAG technology may encounter performance and efficiency problems. For instance, retrieving large amounts of data can be time-consuming, and multi-turn dialogue and scoring by large models also consume significant time.
- 低命中率问题:当用户意图明确时,RAG技术可能无法提供高召回率或精度,导致命中率较低。此外,如果用户意图不明确,RAG系统可能无法直接作答,存在语义鸿沟,简单的检索方式难以找到答案。
Low Hit Rate Problem: When user intent is clear, RAG technology may fail to provide high recall or precision, resulting in a low hit rate. Furthermore, if user intent is ambiguous, the RAG system might be unable to answer directly due to a semantic gap, making it difficult to find answers with simple retrieval methods.
- 语义搜索的不准确:语义搜索的难点在于如何理解用户的问题和文档的语义,以及如何衡量问题和文档之间的语义相似度。向量空间中的距离或相似度并不一定能反映真实的语义相似度。检索能力没有大模型的语义理解能力强,可能导致检索出的内容相关性不强(当然,例如Graph RAG能缓解很多)。
Inaccuracy of Semantic Search: The difficulty of semantic search lies in understanding the semantics of user queries and documents, and measuring the semantic similarity between them. Distance or similarity in vector space does not necessarily reflect true semantic similarity. Retrieval capabilities lack the semantic understanding power of large models, which may lead to retrieved content having weak relevance (though techniques like Graph RAG can mitigate this significantly).
- 冗余和重复:这也是一个问题,特别是当多个检索到的段落包含相似的信息时,会导致生成的响应中出现重复的内容。
Redundancy and Repetition: This is also an issue, especially when multiple retrieved passages contain similar information, leading to repetitive content in the generated response.
5、Native RAG
RAG现在发展出三种类型:Naive RAG(朴素RAG)、Advanced RAG(高级RAG)和Modular RAG(模块化RAG)。
RAG has now evolved into three types: Naive RAG, Advanced RAG, and Modular RAG.
RAG在成本效益上超过了原生LLM,但也表现出几个局限性,这也给大家一个印象:入门容易,做好难。
RAG surpasses native LLMs in cost-effectiveness but also exhibits several limitations, giving the impression that it's easy to get started but hard to master.
Advanced RAG和Modular RAG的发展是为了解决Naive RAG中的缺陷。Naive RAG遵循一个传统的流程,包括索引、检索和生成。它也被称为“检索-阅读”框架。
The development of Advanced RAG and Modular RAG aims to address the shortcomings of Naive RAG. Naive RAG follows a traditional process including indexing, retrieval, and generation. It is also known as the "retrieve-then-read" framework.
6、Advanced RAG
(1)简介
Advanced RAG是为了解决Naive RAG的局限性而开发的。在检索质量方面,Advanced RAG实现了预检索和后检索策略。
Advanced RAG was developed to address the limitations of Naive RAG. In terms of retrieval quality, Advanced RAG implements pre-retrieval and post-retrieval strategies.
为了解决Naive RAG在索引方面的挑战,Advanced RAG通过滑动窗口、细粒度分割和元数据等技术优化了索引方法。它还引入了各种方法来优化检索过程。
To address Naive RAG's challenges in indexing, Advanced RAG optimizes indexing methods through techniques like sliding windows, fine-grained segmentation, and metadata. It also introduces various methods to optimize the retrieval process.
- 数据清洗与优化:确保输入数据的质量,删除无关内容、无法识别的内容(如特殊字符、停用词等),并纠正错误,以提高语义表示的质量。同时确认事实准确性、更新过时信息等。
Data Cleaning and Optimization: Ensure the quality of input data by removing irrelevant content, unrecognizable content (such as special characters, stop words, etc.), and correcting errors to improve the quality of semantic representation. Also, verify factual accuracy and update outdated information.
- 提取方式:支持各种数据提取,包括网页、PDF文档、OCR识别、各种表格等。
LlamaIndex Reader文档:https://docs.llamaindex.ai/en/stable/api_reference/readers/Extraction Methods: Supports various data extraction, including web pages, PDF documents, OCR recognition, various tables, etc.
LlamaIndex Reader documentation:https://docs.llamaindex.ai/en/stable/api_reference/readers/ - 索引(Index):索引优化主要体现在分块(Chunks) 优化和索引优化上。数据索引优化技术旨在以有助于提高检索效率的方式存储数据。
Index: Index optimization is mainly reflected in Chunk optimization and index optimization. Data indexing optimization techniques aim to store data in a way that helps improve retrieval efficiency.
- 滑动窗口:平衡这些需求的一个简单方法是使用重叠块。滑动窗口使块之间重叠,增强了语义转换。然而,也存在局限性,包括对上下文大小的控制不精确、单词或句子被截断的风险以及缺乏语义考虑。
Sliding Window: A simple method to balance these needs is to use overlapping chunks. Sliding windows create overlap between chunks, enhancing semantic transitions. However, limitations exist, including imprecise control over context size, the risk of words or sentences being truncated, and a lack of semantic consideration.
- 添加元数据:
LlamaIndex元数据提取文档:https://docs.llamaindex.ai/en/stable/api_reference/extractors/
示例:https://docs.llamaindex.ai/en/stable/examples/metadata_extraction/EntityExtractionClimate/
利用额外的信息(元数据)来帮助检索过程,从而提高生成内容的相关性和质量。元数据可以包括各种类型的信息,如文档的标题、作者、发布日期、标签或任何其他描述性数据,甚至如网页标签的alt数据等。这些数据用来提高检索系统的准确性和效率。人工构建元数据也是一种思路,比如给每个段落增加摘要、引入可能的假设性问题等。在检索时计算原始问题和假设问题之间的相似度,从而减少问题与答案之间的语义差距,提高效果。Adding Metadata:
LlamaIndex metadata extraction documentation:https://docs.llamaindex.ai/en/stable/api_reference/extractors/
Example:https://docs.llamaindex.ai/en/stable/examples/metadata_extraction/EntityExtractionClimate/
Utilize additional information (metadata) to assist the retrieval process, thereby improving the relevance and quality of generated content. Metadata can include various types of information, such as document title, author, publication date, tags, or any other descriptive data, even like alt data from web page tags. This data is used to enhance the accuracy and efficiency of the retrieval system. Manually constructing metadata is also an approach, for example, adding summaries, introducing possible hypothetical questions to each paragraph. During retrieval, calculating the similarity between the original question and hypothetical questions can reduce the semantic gap between questions and answers, improving effectiveness.
常见问题(FAQ)
RAG技术具体是如何工作的?
RAG技术的工作流程是:先从外部知识库中检索相关信息,然后将这些信息作为上下文注入到大型语言模型的提示中,最后由模型基于检索到的内容生成回答。
RAG技术有哪些主要优点?
RAG技术的主要优点包括:提高生成内容的准确性和相关性,减少模型“幻觉”,支持知识实时更新,并增强了生成内容的可追溯性。
RAG技术主要应用在哪些场景?
RAG技术是行业知识AI落地、企业私有知识库构建和AI搜索的关键技术,特别适用于需要处理专业、实时或私有数据的业务场景。
版权与免责声明:本文仅用于信息分享与交流,不构成任何形式的法律、投资、医疗或其他专业建议,也不构成对任何结果的承诺或保证。
文中提及的商标、品牌、Logo、产品名称及相关图片/素材,其权利归各自合法权利人所有。本站内容可能基于公开资料整理,亦可能使用 AI 辅助生成或润色;我们尽力确保准确与合规,但不保证完整性、时效性与适用性,请读者自行甄别并以官方信息为准。
若本文内容或素材涉嫌侵权、隐私不当或存在错误,请相关权利人/当事人联系本站,我们将及时核实并采取删除、修正或下架等处理措施。 也请勿在评论或联系信息中提交身份证号、手机号、住址等个人敏感信息。