微信扫码
添加专属顾问
本文是关于在实践中使用大型语言模型的更大系列的一部分。在上一篇文章中,我们使用 QLoRA 对 Mistral-7b-Instruct 进行了微调,以回应 YouTube 评论。尽管微调后的模型在回应观众反馈时成功捕捉了我的风格,但它对技术问题的回答与我的解释并不匹配。在这里,我将讨论如何通过检索增强生成(即 RAG)来提高 LLM 的性能。
大型语言模型(LLMs)在响应用户查询时展示了存储和部署大量知识的惊人能力。虽然这使得像 ChatGPT 这样的强大 AI 系统得以创建,但以这种方式压缩世界知识有两个关键限制。
首先,LLM 的知识是静态的,即不会随着新信息的出现而更新。其次,LLM 可能对其训练数据中不显著的利基和专业信息缺乏足够的“理解”。这些限制可能导致模型对用户查询的回答不理想(甚至是虚构的)。
我们可以通过通过专业和可变的知识库增强模型来缓解这些限制,例如客户常见问题解答、软件文档或产品目录。这使得创建更强大和适应性更强的 AI 系统成为可能。
检索增强生成,或称 RAG,就是这样一种方法。在这里,我提供 RAG 的高级介绍,并分享使用 LlamaIndex 实现 RAG 系统的示例 Python 代码。
LLM 的基本用法是给它一个提示并获取响应。
RAG 通过在这个基本过程中添加一个步骤来工作。具体来说,执行一个检索步骤,根据用户的提示,从外部知识库中提取相关信息,并在传递给 LLM 之前将其注入到提示中。
请注意,RAG 并没有从根本上改变我们使用 LLM 的方式;它仍然是 提示输入和响应输出。RAG 只是增强了这个过程(因此得名)。
这使得 RAG 成为一种灵活且(相对)简单的方式来改善基于 LLM 的系统。此外,由于知识存储在外部数据库中,更新系统知识就像从表中添加或删除记录一样简单。
本系列之前的文章讨论了微调,即为特定用例调整现有模型。虽然这是一种赋予LLM专业知识的替代方法,但从经验来看,微调似乎在这方面的效果不如RAG [1]。
RAG 系统有两个关键要素:检索器和 知识库。
检索器接收用户提示并从知识库中返回相关项目。这通常使用所谓的 文本嵌入,即文本在概念空间中的数值表示。换句话说,这些是 表示给定文本的 含义 的数字。
文本嵌入可以用来计算用户查询与知识库中每个项目之间的相似性得分。这个过程的结果是 每个项目与输入查询相关性的排名。
然后,检索器可以选择前 k 个(例如 k=3)最相关的项目,并将它们注入到用户提示中。这个增强的提示随后被传递给 LLM 进行生成。
RAG 系统的下一个关键要素是知识库。这个 包含了您希望提供给 LLM 的所有信息。虽然有无数种方法可以构建 RAG 的知识库,但在这里我将重点介绍如何从一组文档中构建一个知识库。
这个过程可以分为 4 个关键步骤 [2,3].
加载文档 — 这包括收集一组文档并确保它们处于可解析的格式(稍后会详细介绍)。
分块文档—由于 LLM 的上下文窗口有限,文档必须被拆分成更小的块 (例如, 256 或 512 个字符长)。
嵌入块 — 使用文本嵌入模型将每个块转换为数字。
加载到向量数据库— 将文本嵌入加载到数据库(即向量数据库)中。
虽然构建 RAG 系统的步骤在概念上很简单,但一些细微差别可能使得在现实世界中构建一个系统变得更加复杂。
文档准备—RAG 系统的质量取决于从源文档中提取有用信息的能力。例如,如果一个文档格式混乱,充满了图像和表格,那么解析起来会比一个格式良好的文本文件更困难。
选择合适的块大小—我们已经提到由于 LLM 上下文窗口的需要进行分块。然而,还有 2 个额外的分块动机。
首先,它可以降低(计算)成本。你在提示中注入的文本越多,生成完成所需的计算就越多。第二是性能。特定查询的相关信息往往集中在源文档中(通常仅一句话就可以回答一个问题)。分块有助于最小化传递给模型的无关信息的数量 [4]。
改善搜索 — 虽然文本嵌入提供了一种强大且快速的搜索方式,但它并不总是能如人所愿地工作。换句话说,它可能返回与用户查询“相似”的结果,但对回答问题并没有帮助,例如,“洛杉矶的天气怎么样?”可能返回“纽约的天气怎么样?”。
缓解这一问题的最简单方法是通过良好的文档准备和分块。然而,对于某些用例,可能需要额外的策略来改善搜索,例如为每个块使用 元标签、采用结合关键词和嵌入搜索的 混合搜索,或使用 重排序器,这是一种专门计算两段文本相似性的模型。
在对 RAG 工作原理有基本了解后,让我们看看如何在实践中使用它。我将基于 上一篇文章 中的示例,在其中我使用 QLoRA 对 Mistral-7B-Instruct 进行了微调,以响应 YouTube 评论。我们将使用 LlamaIndex 为之前微调的模型添加 RAG 系统。
示例代码可在 Colab Notebook 中免费获得,该 Notebook 可以在提供的(免费)T4 GPU 上运行。此示例的源文件可在 GitHub 仓库 中找到。
? Google Colab | GitHub Repo
我们首先安装并导入必要的 Python 库。
!pip install llama-index
!pip install llama-index-embeddings-huggingface
!pip install peft
!pip install auto-gptq
!pip install optimum
!pip install bitsandbytes
## 如果不是在 Colab 上运行,请确保也安装 transformers
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.core import Settings, SimpleDirectoryReader, VectorStoreIndex
from llama_index.core.retrievers import VectorIndexRetriever
from llama_index.core.query_engine import RetrieverQueryEngine
from llama_index.core.postprocessor import SimilarityPostprocessor
我们可以通过定义我们的嵌入模型、块大小和块重叠来配置我们的知识库。在这里,我们使用来自BAAI的\~33M参数bge-small-en-v1.5嵌入模型,该模型可在Hugging Face hub上获取。其他嵌入模型选项可以在这个text embedding leaderboard上找到。
## import any embedding model on HF hub
Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
Settings.llm = None # we won't use LlamaIndex to set up LLM
Settings.chunk_size = 256
Settings.chunk_overlap = 25
接下来,我们加载源文档。在这里,我有一个名为“articles”的文件夹,其中包含我在fat tails上写的3篇Medium文章的PDF版本。如果在Colab中运行,您必须从GitHub repo下载文章文件夹并手动上传到您的Colab环境。
对于该文件夹中的每个文件,下面的函数将从PDF中读取文本,将其拆分成块(基于之前定义的设置),并将每个块存储在名为documents的列表中。
documents = SimpleDirectoryReader("articles").load_data()
由于这些博客是直接从Medium下载为PDF的,因此它们更像是网页,而不是格式良好的文章。因此,一些块可能包含与文章无关的文本,例如网页标题和Medium文章推荐。
在下面的代码块中,我对documents中的块进行精炼,删除文章主体前后的大部分块。
print(len(documents)) # prints: 71
for doc in documents:
if "Member-only story" in doc.text:
documents.remove(doc)
continue
if "The Data Entrepreneurs" in doc.text:
documents.remove(doc)
if " min read" in doc.text:
documents.remove(doc)
print(len(documents)) # prints: 61
最后,我们可以将精炼后的块存储在向量数据库中。
index = VectorStoreIndex.from_documents(documents)
在我们的知识库建立之后,我们可以使用 LlamaIndex 的 VectorIndexRetriever() 创建一个检索器,它返回与用户查询最相似的 3 个块。
## set number of docs to retreive
top_k = 3
## configure retriever
retriever = VectorIndexRetriever(
index=index,
similarity_top_k=top_k,
)
接下来,我们定义一个查询引擎,使用检索器和查询返回一组相关的块。
## assemble query engine
query_engine = RetrieverQueryEngine(
retriever=retriever,
node_postprocessors=[SimilarityPostprocessor(similarity_cutoff=0.5)],
)
现在,随着我们的知识库和检索系统的建立,让我们使用它来返回与查询相关的内容。在这里,我们将传递我们向ShawGPT(YouTube评论回复者)提出的相同技术问题,来自上一篇文章。
query = "What is fat-tailedness?"
response = query_engine.query(query)
查询引擎返回一个响应对象,其中包含文本、元数据和相关块的索引。下面的代码块返回该信息的更易读版本。
## reformat response
context = "Context:\n"
for i in range(top_k):
context = context + response.source_nodes[i].text + "\n\n"
print(context)
Context:
Some of the controversy might be explained by the observation that log-
normal distributions behave like Gaussian for low sigma and like Power Law
at high sigma [2].
However, to avoid controversy, we can depart (for now) from whether some
given data fits a Power Law or not and focus instead on fat tails.
Fat-tailedness — measuring the space between Mediocristan
and Extremistan
Fat Tails are a more general idea than Pareto and Power Law distributions.
One way we can think about it is that “fat-tailedness” is the degree to which
rare events drive the aggregate statistics of a distribution. From this point of
view, fat-tailedness lives on a spectrum from not fat-tailed (i.e. a Gaussian) to
very fat-tailed (i.e. Pareto 80 – 20).
This maps directly to the idea of Mediocristan vs Extremistan discussed
earlier. The image below visualizes different distributions across this
conceptual landscape [2].
print("mean kappa_1n = " + str(np.mean(kappa_dict[filename])))
print("")
Mean κ (1,100) values from 1000 runs for each dataset. Image by author.
These more stable results indicate Medium followers are the most fat-tailed,
followed by LinkedIn Impressions and YouTube earnings.
Note: One can compare these values to Table III in ref [3] to better understand each
κ value. Namely, these values are comparable to a Pareto distribution with α
between 2 and 3.
Although each heuristic told a slightly different story, all signs point toward
Medium followers gained being the most fat-tailed of the 3 datasets.
Conclusion
While binary labeling data as fat-tailed (or not) may be tempting, fat-
tailedness lives on a spectrum. Here, we broke down 4 heuristics for
quantifying how fat-tailed data are.
Pareto, Power Laws, and Fat Tails
What they don’t teach you in statistics
towardsdatascience.com
Although Pareto (and more generally power law) distributions give us a
salient example of fat tails, this is a more general notion that lives on a
spectrum ranging from thin-tailed (i.e. a Gaussian) to very fat-tailed (i.e.
Pareto 80 – 20).
The spectrum of Fat-tailedness. Image by author.
This view of fat-tailedness provides us with a more flexible and precise way of
categorizing data than simply labeling it as a Power Law (or not). However,
this begs the question: how do we define fat-tailedness?
4 Ways to Quantify Fat Tails
我们首先从 Hugging Face hub 下载 微调模型。
## load fine-tuned model from hub
from peft import PeftModel, PeftConfig
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "TheBloke/Mistral-7B-Instruct-v0.2-GPTQ"
model = AutoModelForCausalLM.from_pretrained(model_name,
device_map="auto",
trust_remote_code=False,
revision="main")
config = PeftConfig.from_pretrained("shawhin/shawgpt-ft")
model = PeftModel.from_pretrained(model, "shawhin/shawgpt-ft")
## load tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
作为基线,我们可以观察模型在没有任何文章上下文的情况下如何回应技术问题。为此,我们使用 lambda 函数创建一个提示模板,该函数接受观众评论并返回 LLM 的提示。有关此提示来源的更多详细信息,请参见本系列的 上一篇文章。
## prompt (no context)
intstructions_string = f"""ShawGPT, functioning as a virtual data science \
consultant on YouTube, communicates in clear, accessible language, escalating \
to technical depth upon request. It reacts to feedback aptly and ends \
responses with its signature '–ShawGPT'.
ShawGPT will tailor the length of its responses to match the viewer's comment, \
providing concise acknowledgments to brief expressions of gratitude or \
feedback, thus keeping the interaction natural and engaging.
Please respond to the following comment.
"""
prompt_template = lambda comment: f'''[INST] {intstructions_string} \n{comment} \n[/INST]'''
comment = "What is fat-tailedness?"
prompt = prompt_template(comment)
print(prompt)
[INST] ShawGPT, functioning as a virtual data science consultant on YouTube, communicates in clear, accessible language, escalating to technical depth upon request. It reacts to feedback aptly and ends responses with its signature '–ShawGPT'. ShawGPT will tailor the length of its responses to match the viewer's comment, providing concise acknowledgments to brief expressions of gratitude or feedback, thus keeping the interaction natural and engaging.
Please respond to the following comment.
What is fat-tailedness?
[/INST]
接下来,我们可以使用以下代码将此提示传递给模型。
model.eval()
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(input_ids=inputs["input_ids"].to("cuda"),
max_new_tokens=280)
print(tokenizer.batch_decode(outputs)[0])
这是模型的响应(没有上下文)。
Great question!
Fat-tailedness is a statistical property of a distribution. In simple terms,
it refers to the presence of extreme outliers or heavy tails in the
distribution.
For instance, consider the distribution of heights in a population. A normal
distribution would have most people clustered around an average height with a
few people deviating slightly from the mean. However, in a fat-tailed
distribution, you would observe a larger number of people being significantly
taller or shorter than the average.
In finance, fat-tailedness is often used to describe the presence of extreme
market moves or outliers in financial returns. It's an important concept in
risk management and financial modeling.
I hope this explanation helps! Let me know if you have any questions.
–ShawGPT
虽然响应的风格和格式都很好,但模型对 fat-tailedness 的解释与我在我的 视频 和 博客系列 中定义的有所不同。
让我们看看当我们包含适当的上下文时模型的响应会发生什么。为此,我们创建另一个提示模板,该模板也可以从 RAG 系统中获取上下文。
## prompt (with context)
prompt_template_w_context = lambda context, comment: f"""[INST]ShawGPT, functioning as a virtual data science consultant on YouTube, communicates in clear, accessible language, escalating to technical depth upon request. \
It reacts to feedback aptly and ends responses with its signature '–ShawGPT'. \
ShawGPT will tailor the length of its responses to match the viewer's comment, providing concise acknowledgments to brief expressions of gratitude or feedback, \
thus keeping the interaction natural and engaging.
{context}
Please respond to the following comment. Use the context above if it is helpful.
{comment}
[/INST]
"""
接下来,我们将查询引擎中的上下文和观众评论传递给模型。
prompt = prompt_template_w_context(context, comment)
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(input_ids=inputs["input_ids"].to("cuda"), max_new_tokens=280)
print(tokenizer.batch_decode(outputs)[0])
这是新的响应(带上下文)。
Great question!
Fat-tailedness is a property of a distribution that describes how much the
presence of extreme values (rare events) affects the overall statistics of the
distribution.
In the context of the video, the author discusses how Mediocristan and
Extremistan distributions differ in terms of fat-tailedness. Mediocristan
distributions have a relatively small impact from extreme values, while
Extremistan distributions are heavily influenced by extreme values.
The author also mentions that fat-tailedness is a more general concept than
specific distributions like Pareto or Power Laws. Instead, it lives on a
spectrum ranging from thin-tailed (Gaussian) to very fat-tailed (Pareto 80-20).
I hope that helps clarify things a bit! Let me know if you have any questions.
–ShawGPT
这比没有上下文的响应更好地捕捉了我对 fat tails 的解释,并且甚至提到了 Mediocristan 和 Extremistan 的细分概念。
在这里,我为初学者提供了一个关于 RAG 的友好介绍,并分享了如何使用 LlamaIndex 实现它的具体示例。RAG 使我们能够通过可更新和特定领域的知识来改善 LLM 系统。
虽然最近的 AI 热潮主要集中在构建 AI 助手上,但一个强大的(但不那么流行的)创新来自于文本嵌入(即我们用来进行检索的东西)。在本系列的下一篇文章中,我将更详细地探讨 文本嵌入,包括它们如何用于 语义搜索 和 分类任务。
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-07-05
AI 知识库为什么总答不准?不是模型笨,是资料没整理好
2026-07-05
AI知识库RAG演进:上一代解决「找得到」,下一代解决「记得住、连得起、信得过」
2026-07-04
大模型支持的上下文已超 1M, RAG 是不是没有意义了?
2026-07-03
RAG 检索优化策略:从命中率到答案质量的一套工程打法
2026-07-03
RAG 落地总翻车?全球赛事冠军架构,改造适配企业级生产
2026-07-01
提升 RAG 准确率全攻略 让你的 AI 知识库 真正靠谱起来!
2026-06-30
教程:如何用AutoRAG + Milvus避免RAG 与Agent 中出现串租问题
2026-06-30
知识库不是文件堆——我把RAG准确率从60%调到了92%
2026-04-27
2026-04-23
2026-04-20
2026-04-09
2026-04-12
2026-04-22
2026-04-10
2026-05-14
2026-04-30
2026-04-27
2026-07-04
2026-06-23
2026-06-23
2026-06-15
2026-06-10
2026-06-10
2026-05-20
2026-05-18
欢迎您使用【53AI 官方网站】(以下简称“本网站”或“我们”)。本《会员服务协议》(以下简称“本协议”)是您(以下简称“会员”或“用户”)与【深圳市博思协创网络科技有限公司】之间关于注册、登录及使用本网站会员服务所订立的法律协议。
在您注册或登录前,请务必审慎阅读、充分理解各条款内容,特别是免除或限制责任的条款、知识产权条款、争议解决条款等。此类条款将以加粗形式提示您注意。 当您通过微信公众号授权、手机验证码验证或其他方式成功登录本网站时,即视为您已完全理解并同意接受本协议的全部内容。
一、 定义
本网站:指由【深圳市博思协创网络科技有限公司】运营的,域名为【53ai.com】的网站及相关移动端页面。
会员服务:指本网站向注册会员提供的知识库文章查阅、内容检索及其他相关增值服务。
知识库内容:指本网站发布的包括但不限于文字、图表、数据、研究报告、行业分析等数字化内容资源。
二、 账号注册与登录
登录方式:本网站支持以下登录方式,您可根据实际情况选择:
微信公众号授权登录:您同意将您的微信OpenID信息授权给本网站,用于创建或关联会员账号。
手机验证码登录:您需提供真实有效的手机号码,并通过短信验证码完成身份验证与登录/注册。
账号安全:您的账号仅限您本人使用,禁止赠与、借用、租用、转让或售卖。因您保管不善导致的账号被盗、密码泄露等损失,由您自行承担。
实名认证:根据相关法律法规要求,我们可能要求您在特定功能下完成实名认证。如您拒绝提供,可能无法使用部分或全部服务。
未成年人保护:若您未满18周岁,请在法定监护人的陪同下阅读本协议,并在征得监护人同意后使用本服务。
三、 服务内容与规范
知识库查阅权限:会员登录后,有权按照其会员等级对应的权限范围,在线浏览、检索本网站知识库中的相关文章及内容。
服务变更:我们有权根据业务发展需要,调整、变更或终止部分服务内容,并将以网站公告、公众号消息等方式提前通知。
禁止行为:您在使用服务时不得实施以下行为:
利用技术手段批量爬取、下载、转存知识库内容;
将知识库内容用于商业目的或未经授权地向第三方传播;
干扰本网站正常运行或侵犯其他用户合法权益;
发布违法违规信息或从事违反公序良俗的活动。
四、 知识产权声明
权利归属:本网站知识库中的排版设计、软件代码等内容的知识产权均归【公司全称】或原权利人所有,受《中华人民共和国著作权法》等法律保护。
有限许可:本网站授予会员一项非独占、不可转让、不可转授权的普通许可,仅限于个人学习、研究之目的在线查阅知识库内容。
侵权追责:未经书面许可,任何单位或个人不得以任何形式复制、转载、摘编、镜像、汇编或以其他方式使用上述内容。一经发现,我们保留追究其法律责任的权利。
五、 个人信息保护
我们重视对您个人信息的保护。关于我们如何收集、使用、存储和保护您的个人信息,请单独阅读 《隐私政策》。
您通过微信公众号授权或手机号验证所提供的信息,我们将严格按照《个人信息保护法》的规定处理,仅用于身份识别、服务提供及安全验证等必要用途。
您可以随时通过网站设置或联系客服行使查阅、更正、删除个人信息及撤回授权同意的权利。
六、 免责声明
内容准确性:知识库内容仅供参考,不构成专业建议。我们不对其完整性、准确性、时效性作任何明示或暗示的保证,您应自行判断并承担使用风险。
不可抗力:因自然灾害、政策法规变化、网络故障、第三方平台接口异常(如微信接口维护、运营商短信通道故障)等不可抗力导致的服务中断或延迟,我们不承担违约责任。
第三方链接:本网站可能包含指向第三方网站的链接,该等网站的内容和服务不受我们控制,请您自行甄别风险。
七、 违约责任
如您违反本协议约定,我们有权视情节采取警告、限制功能、暂停服务、注销账号等措施,并保留要求赔偿损失的权利。
如因您的违约行为导致我们遭受行政处罚、第三方索赔或商誉损失,您应承担全部赔偿责任(包括但不限于罚款、赔偿金、律师费、公证费等)。
八、 法律适用与争议解决
本协议的订立、执行和解释均适用中华人民共和国大陆地区法律。
因本协议产生的或与本协议有关的任何争议,双方应友好协商解决;协商不成的,任何一方均可向【公司所在地】有管辖权的人民法院提起诉讼。
九、 其他
本协议构成双方就本服务达成的完整协议,取代此前任何口头或书面约定。
本协议任一条款被认定为无效或不可执行的,不影响其他条款的效力。
我们对本协议享有最终解释权,并在法律允许的范围内保留随时修改的权利。修改后的协议一经公布即生效,继续使用服务即视为同意修订内容。