LLM大模型: RAG的langchain+向量数据库实现和评估方案
LLM大模型的核心功能之一就是聊天对话(信息检索),RAG的使用必不可少!大致的流程是:用户的query先转成embedding,去向量数据库查询最接近的top K回答;然后这query + top K的回答 + 其他context一起进入LLM,让LLM整合上述所有的信息后给出最终的回复!
前言
LLM大模型的核心功能之一就是聊天对话(信息检索),RAG的使用必不可少!大致的流程是:用户的query先转成embedding,去向量数据库查询最接近的top K回答;然后这query + top K的回答 + 其他context一起进入LLM,让LLM整合上述所有的信息后给出最终的回复!

为了简便、快速地实现RAG的功能,搭配上langchain使用效果更佳!为配合下面的功能说明,这里先加载长文档,并分割后求embedding后存入向量数据库
from langchain.text\_splitter import RecursiveCharacterTextSplitter
from langchain.document\_loaders import PyPDFLoader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
# Load pdf
loader = PyPDFLoader("data\\\\baichuan.pdf")
data \= loader.load()
# Split 分割长文档,每个chunk 500个token
text\_splitter = RecursiveCharacterTextSplitter(chunk\_size=500, chunk\_overlap=0)
splits \= text\_splitter.split\_documents(data\[:6\])
使用指定的LLM把每个chunk转成embedding后存入Chroma向量数据库(换成FAISS也行)
import os
from getpass import getpass
OPENAI\_API\_KEY \= getpass()
os.environ\["OPENAI\_API\_KEY"\] = OPENAI\_API\_KEY
# VectorDB
embedding = OpenAIEmbeddings()
vectordb \= Chroma.from\_documents(documents=splits, embedding=embedding)#调用上面的embedding工具,把分割后的文档转成embedding,存入Chroma向量数据库;
1、MuitiQueryRetriever:从名字就能看出来这个类能生成多个query,那么问题来了:为啥要这样做了?
用户的query,存在如下两个问题:
- 过于简单,比如“什么是地球?”,常见于通用的聊天工具
- 不专业,比如“我肚子疼”,常见于垂直领域的聊天工具
经常使用各种聊天工具的用户都有一个体会:query1达不到自己的预期,换个角度生成query2继续提问,如果还达不到预期,继续换个角度生成query3再次提问,这就是人为从不同角度花样生成不同的query提问,以提高检索的多样性和覆盖面!幸运的是,这个功能在langchain已经实现,demo如下:
from langchain.retrievers.multi\_query import MultiQueryRetriever
from langchain.chat\_models import ChatOpenAI
question \= "what is baichuan2 ?"
llm \= ChatOpenAI(temperature=0) # 这里可以换成其他的llm
retriever\_from\_llm = MultiQueryRetriever.from\_llm(
retriever\=vectordb.as\_retriever(), llm=llm #通过上述的llm生成多个query,并指定检索的向量数据库
)
效果如下:同一个query被指定的大模型衍生出了3个query:
INFO:langchain.retrievers.multi\_query:Generated queries: \['1\. Can you provide information about baichuan2?', '2\. What can you tell me about baichuan2?', '3\. Could you explain the concept of baichuan2 to me?'\]
然后直接和向量数据库中的embedding计算距离:
docs = retriever\_from\_llm.get\_relevant\_documents(query=question)#多个query和向量数据库的embedding向量直接计算距离
\[Document(page\_content='In this technical report, we introduce Baichuan\\n2, a series of large-scale multilingual language\\nmodels. Baichuan 2 has two separate models,\\nBaichuan 2-7B with 7 billion parameters and\\nBaichuan 2-13B with 13 billion parameters. Both\\nmodels were trained on 2.6 trillion tokens, which\\nto our knowledge is the largest to date, more than\\ndouble that of Baichuan 1 (Baichuan, 2023b,a).\\nWith such a massive amount of training data,\\nBaichuan 2 achieves significant improvements over', metadata={'page': 1, 'source': 'data\\\\baichuan.pdf'}),
Document(page\_content\='demonstrates strong performance on medical and\\nlegal domain tasks. On benchmarks such as\\nMedQA (Jin et al., 2021) and JEC-QA (Zhong\\net al., 2020), Baichuan 2 outperforms other open-\\nsource models, making it a suitable foundation\\nmodel for domain-specific optimization.\\nAdditionally, we also released two chat\\nmodels, Baichuan 2-7B-Chat and Baichuan 2-\\n13B-Chat, optimized to follow human instructions.\\nThese models excel at dialogue and context\\nunderstanding. We will elaborate on our', metadata={'page': 1, 'source': 'data\\\\baichuan.pdf'}),
Document(page\_content\='Baichuan 1-13B-Base 52.40 51.60 55.30 49.69 43.20 43.01 26.76 11.5913B\\nBaichuan 2-13B-Base 58.10 59.17 61.97 54.33 48.17 48.78 52.77 17.07\\nTable 1: Overall results of Baichuan 2 compared with other similarly sized LLMs on general benchmarks. \* denotes\\nresults derived from official websites.\\nFigure 1: The distribution of different categories of\\nBaichuan 2 training data.\\nData processing : For data processing, we focus\\non data frequency and quality. Data frequency', metadata={'page': 2, 'source': 'data\\\\baichuan.pdf'}),
Document(page\_content\='Baichuan 1. On general benchmarks like MMLU\\n(Hendrycks et al., 2021a), CMMLU (Li et al.,\\n2023), and C-Eval (Huang et al., 2023), Baichuan\\n2-7B achieves nearly 30% higher performance\\ncompared to Baichuan 1-7B. Specifically, Baichuan\\n2 is optimized to improve performance on math\\nand code problems. On the GSM8K (Cobbe\\net al., 2021) and HumanEval (Chen et al., 2021)\\nevaluations, Baichuan 2 nearly doubles the results\\nof the Baichuan 1. In addition, Baichuan 2 also', metadata={'page': 1, 'source': 'data\\\\baichuan.pdf'}),
Document(page\_content\='overall performance of the Baichuan 2 base models\\ncompared to other open or closed-sourced models\\nin Table 1. We then describe our pre-training data\\nand data processing methods. Next, we elaborate\\non the Baichuan 2 architecture and scaling results.\\nFinally, we describe the distributed training system.\\n2.1 Pre-training Data\\nData sourcing : During data acquisition, our\\nobjective is to pursue comprehensive data\\nscalability and representativeness. We gather data', metadata={'page': 1, 'source': 'data\\\\baichuan.pdf'})\]
LLM又是怎么把一个query变成三个的了?核心在这里了:https://python.langchain.com/v0.2/docs/how_to/MultiQueryRetriever/ 本质还是使用合适的prompt,调用LLM的能力把1个query扩展到多个:
from typing import List
from langchain\_core.output\_parsers import BaseOutputParser
from langchain\_core.prompts import PromptTemplate
from langchain\_core.pydantic\_v1 import BaseModel, Field
# Output parser will split the LLM result into a list of queries
class LineListOutputParser(BaseOutputParser\[List\[str\]\]):
"""Output parser for a list of lines."""
def parse(self, text: str) -> List\[str\]:
lines \= text.strip().split("\\n")
return lines
output\_parser \= LineListOutputParser()
QUERY\_PROMPT \= PromptTemplate(
input\_variables\=\["question"\],
template\="""You are an AI language model assistant. Your task is to generate 3
different versions of the given user question to retrieve relevant documents from a vector
database. By generating multiple perspectives on the user question, your goal is to help
the user overcome some of the limitations of the distance-based similarity search.
Provide these alternative questions separated by newlines.
Original question: {question}""",
)
llm \= ChatOpenAI(temperature=0)
# Chain
llm\_chain = QUERY\_PROMPT | llm | output\_parser
# Other inputs
question = "What are the approaches to Task Decomposition?"
2、Contextual Compression:知识库中的有些文档又大又长,根据向量距离检索出来后长度过长,用户可能没耐心挨个看;而且context过长,可能影响后续LLM的生成,所以需要适当压缩,去掉细枝末节,只保留主干或主题;demo代码如下
from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document\_compressors import LLMChainExtractor
from langchain.chat\_models import ChatOpenAI
# 创建LLM实例
llm = ChatOpenAI(temperature=0)
# 创建压缩器实例
compressor = LLMChainExtractor.from\_llm(llm)
# 假设retriever是你的基础检索器,例如向量数据库检索器
retriever = vectordb.as\_retriever()
# 创建上下文压缩检索器实例
compression\_retriever = ContextualCompressionRetriever(
base\_compressor\=compressor, base\_retriever=retriever
)
# 获取压缩后的相关文档
compressed\_docs = compression\_retriever.get\_relevant\_documents("What is baichuan2 ?")
# 输出结果
for doc in compressed\_docs:
print(doc)
原来的文档:
Baichuan2 is a next-generation artificial intelligence model developed by XYZ Corp. It is designed to handle complex natural language processing tasks, including text generation, translation, and summarization. Baichuan2 utilizes a novel neural network architecture that improves on previous models by incorporating advanced machine learning techniques. Additionally, it supports real-time data processing and can be integrated into various applications to enhance their AI capabilities. The development of Baichuan2 marks a significant milestone in AI research and development.
压缩后的文档:
Baichuan2 is a next-generation artificial intelligence model developed by XYZ Corp. It handles complex natural language processing tasks and utilizes a novel neural network architecture.
压缩的原理也很简单,和MuitiQueryRetriever类似,都是依赖prompt实现的,详见:https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/retrievers/document_compressors/chain_extract_prompt.py
prompt\_template = """Given the following question and context, extract any part of the context \*AS IS\* that is relevant to answer the question. If none of the context is relevant return {no\_output\_str}.
Remember, \*DO NOT\* edit the extracted parts of the context.
> Question: {{question}}
> Context:
>>>
{{context}}
>>>
Extracted relevant parts:"""
核心原理还是通过prompt让LLM帮忙压缩信息,提取主干!
3、EnsembleRetriever:前面最常用的Retriever就是计算两个embedding的距离了,这种方式又称“密集检索”,因为embedding的每个维度都有数值,看起来很紧凑和密集,所以叫密集检索!既然有密集检索,那肯定有稀疏检索啦,最常见的是BM25稀疏检索,如下:
传统做NLP的对TF-IDF肯定不陌生,本以为transformer爆火后中文分词、TF-IDF已经不再需要,没想到在这里居然还用上了,宝刀未老啊!

和密集检索比,稀疏检索的优势:
- 性能优势:BM25只需要计算词频和文档长度,计算量比密集计算小
- 语义捕捉:通过考虑词频和文档频率,BM25能够准确捕捉到用户查询的意图,提供高度相关的结果
- 灵活调节:通过调节K1和b的值,适应不同的检索场景。K1调节词频的影响,一般取值范围为1.2到2;b 调节文档长度归一化的影响,一般取值范围为0到1。
- 新闻文章检索:新闻类的文档(文章)通常较长,重要的内容可能分布在文档的各个部分,所以K1=1.5,适度重视词频;而b=0.75,对文档长度归一化!这种设置下,BM25可以在长文档中有效地找到与查询相关的段落,避免因文档长度而导致的评分偏差。
- twtter、微博这种短文本检索:这类文本通常较短,词频的重要性更高,所以K1=1.2,更重视词频;而b=0.25,较少考虑文本长度;
langChain的接口很简单:
from langchain.retrievers import BM25Retriever, EnsembleRetriever
bm25\_retriever \= BM25Retriever.from\_documents(
documents\=splits
) #生成bm25检索器
bm25\_retriever.k = 4 #BM25返回top4文档
vectordb \= Chroma.from\_documents(documents=splits, embedding=embedding)
retriever \= vectordb.as\_retriever(search\_kwargs={"k": 4})#embedding的密集检索
ensemble\_retriever \= EnsembleRetriever(
retrievers\=\[bm25\_retriever, retriever\], weights=\[0.5, 0.5\]
)#两个检索器各自的权重都是0.5,大家均分
docs \= ensemble\_retriever.invoke("What is baichuan2 ?")
上述的权重组合实现方式如下:密集和稀疏检索器分别打分,然后乘以各自的权重后相加,得到整体的分数,然后返回得分topK的文档即可!
稀疏和密集检索配合的流程如下:
4、任何系统都要有合适的评价指标,否则怎么知道质量好坏了?传统的机器学习常用准确率、覆盖率、AUC等指标评价模型的好坏,大模型本身可以用BLUE、ROUGE等指标评判,RAG应该怎么量化评价指标了?
(1)先看看评估架构如下:
-
query: 用户的输入
-
relevant context:通过query的embedding从向量数据库检索到的top K上下文
-
Answer:把query + context输入LLM后综合得到的结果
-
ground truth:人为确定的正确答案

根据以上架构,衍生出三大类的评价指标:
- Faithfulness:LLM所有的回答的语义全都要基于context,不能让LLM产生幻觉胡乱回答
- answer relevant:回答的相关性,也就是LLM的回答要满足query
- context relevant:上下文的相关性,从知识库中初步召回的N条context一定要和query高度相关
(2)Faithfulness:衡量生成答案与给定上下文之间的事实一致性【避免产生幻觉】。忠实度得分是基于答案和检索到的上下文计算出来的,答案的评分范围在0到1之间,分数越高越好;如果生成的答案的每个token都在context里面,评分就是1;

(3)Answer relevance:答案相关性的评估指标旨在评估生成的答案与给定提示的相关程度。如果答案不完整或包含冗余信息,则会被赋予较低的分数。这个指标使用问题和答案来计算,其值介于0到1之间,得分越高表明答案的相关性越好。
这个评价指标最难的点在于:怎么计算分数?这就要用到逆向思维方式了:某个特定answer用LLM生成N个query_i,每个query_i和原始用户的query求相似度,然后N个query_i的相似度求均值,就是该answer和query之间的Answer relevance得分啦!感觉这是在用LLM评价LLM啊,实属是左右互搏!
(4)Context Precision:上下文精确度衡量上下文中所有相关的真实信息是否被排在了较高的位置。理想情况下,所有相关的信息块都应该出现在排名的最前面。这个指标是根据问题和上下文来计算的,数值范围在0到1之间,分数越高表示精确度越好。

(5)Context Recall:用来衡量检索到的上下文与被视为事实真相的标注答案的一致性程度。它根据事实真相和检索到的上下文来计算,数值范围在0到1之间,数值越高表示性能越好; 为了从事实真相的答案中估计上下文召回率,需要分析真实答案中的每个句子是否可以归因于检索到的上下文。在理想情况下,事实真相答案中的所有句子都应该能够对应到检索到的上下文中。

(6)上面的评价指标很多,总的来说可以分成两类:
-
Retriever的评价(主要针对embedding系统)
- Context Precision:评估初步从向量数据库召回context的质量,有没有召回错误(或者说不相关)信息
- Context Recall:评估初步从向量数据库召回context的覆盖率,有没有遗漏重要信息
-
Generator的评价**(主要针对LLM**)
- Faithfulness:LLM根据query+context生成最终回答在语义上有没有遵从context,避免回答跳出context的范围,产生幻觉,进而驴唇不对马嘴
- Answer relevance:LLM根据query+context生成的最终回答有没有紧扣query的要求,答案是否完整或包含冗余信息
幸运的是,这些指标的计算在langChain都已经实现,直接调用即可!https://github.com/blackinkkkxi/RAG_langchain/blob/main/learn/evaluation/RAGAS-langchian.ipynb 这有整个完整的流程参考!
先定义好prompt: 明确告知LLM根据question和context生成answer,不要自己胡乱联想,不知道就是不知道!
from langchain import PromptTemplate
template \= """You are an assistant for question-answering tasks.
Use the following pieces of retrieved context to answer the question.
If you don't know the answer, just say that you don't know.
Question: {question}
Context: {context}
Answer:
"""
prompt \= PromptTemplate(
template\=template,
input\_variables\=\["context","question"\]
)
print(prompt)
再利用 `Runnable` 定义一个 `chain` 实现rag全流程,指定LLM、prompt、retriever等关键信息!
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema.output\_parser import StrOutputParser
from langchain.chat\_models import ChatOpenAI
llm \= ChatOpenAI(model\_name="gpt-3.5-turbo", temperature=0)
rag\_chain \= (
{"context": base\_retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
定义评价指标所需要的数据:
# Ragas 数据集格式要求 \['question', 'answer', 'contexts', 'ground\_truths'\]
'''
{
"question": \[\], <-- 问题基于Context的
"answer": \[\], <-- 答案基于LLM生成的
"contexts": \[\], <-- context
"ground\_truths": \[\] <-- 标准答案
}
'''
from datasets import Dataset
questions \= \["What is faithfulness ?",
"How many pages are included in the WikiEval dataset, and which years do they cover information from?",
"Why is evaluating Retrieval Augmented Generation (RAG) systems challenging?",
\]
ground\_truths \= \[\["Faithfulness refers to the idea that the answer should be grounded in the given context."\],
\[" To construct the dataset, we first selected 50 Wikipedia pages covering events that have happened since the start of 2022."\],
\["Evaluating RAG architectures is, however, challenging because there are several dimensions to consider: the ability of the retrieval system to identify relevant and focused context passages, the ability of the LLM to exploit such passages in a faithful way, or the quality of the generation itself."\]\]
answers \= \[\]
contexts \= \[\]
# 生成答案
for query in questions:
answers.append(rag\_chain.invoke(query))
contexts.append(\[docs.page\_content for docs in base\_retriever.get\_relevant\_documents(query)\])
# 构建数据
data = {
"question": questions,
"answer": answers,
"contexts": contexts,
"ground\_truths": ground\_truths
}
dataset \= Dataset.from\_dict(data)
最后一步直接调用现成接口评估:
from ragas import evaluate
from ragas.metrics import (
faithfulness,
answer\_relevancy,
context\_relevancy,
context\_recall,
context\_precision,
)
result \= evaluate(
dataset \= dataset,
metrics\=\[
context\_precision,
context\_recall,
faithfulness,
answer\_relevancy,
\],
)
评估结果:

根据评估指标判断:如果context两个指标较低,明显是retriever的问题,可以引入EnsembleRetriver、LongContextReorder、ParentDocumentRetriever;如果faithfulness或answer relevance较低,可以考虑换LLM!
(7)最后,整理一个answer不满足用户需求的问题排查思路:

5、其他踩坑注意事项:
(1)知识库的数据:
- 数据是否有重复冗余?可以先用minhash去重
- 数据分类:业务意义不同的数据分开存放在不同的向量数据库实例,比如安全领域的渗透、windows逆向、Android逆向、windows逆向、漏洞等分开,存放在不同的向量数据库实例
(2)chunk分块:原则上讲,chunk应该把有同样语义的token合成一个块,换句话说,同一个块最好只有一个语义,不同语义的token放在不同的chunk,更利于初步召回,减少噪音;我个人更倾向于使用语义分割,原理如下:
- 首先将文本划分成一个个句子,并计算第一个句子的向量;
- 接着计算第二个句子的向量,并和第一个句子进行比较,得到相似度;
- 接着计算第三个句子的向量,并和第二个句子进行比较,得到相似度,以此类推;
- 当句子之间的相似度低于某个阈值时,说明这里的话题可能存在转折,可以将这个地方作为分块的临界点。
LangChain 的 SemanticChunker 和 LlamaIndex 的 SemanticSplitterNodeParser 都实现了语义分块。 api:https://python.langchain.com/v0.1/docs/modules/data_connection/document_transformers/semantic-chunker/
(3)对于直接从知识库找不到的答案,RAG的效果也不咋地,比如:android逆向和windows逆向哪个更有前途? 怎么渗透一个web站点?知识库中上下文连贯性还需要解决,参考:https://blog.csdn.net/star1210644725/article/details/136241523
最后的最后
感谢你们的阅读和喜欢,作为一位在一线互联网行业奋斗多年的老兵,我深知在这个瞬息万变的技术领域中,持续学习和进步的重要性。
为了帮助更多热爱技术、渴望成长的朋友,我特别整理了一份涵盖大模型领域的宝贵资料集。
这些资料不仅是我多年积累的心血结晶,也是我在行业一线实战经验的总结。
这些学习资料不仅深入浅出,而且非常实用,让大家系统而高效地掌握AI大模型的各个知识点。如果你愿意花时间沉下心来学习,相信它们一定能为你提供实质性的帮助。
这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】
大模型知识脑图
为了成为更好的 AI大模型 开发者,这里为大家提供了总的路线图。它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
经典书籍阅读
阅读AI大模型经典书籍可以帮助读者提高技术水平,开拓视野,掌握核心技术,提高解决问题的能力,同时也可以借鉴他人的经验。对于想要深入学习AI大模型开发的读者来说,阅读经典书籍是非常有必要的。

实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

面试资料
我们学习AI大模型必然是想找到高薪的工作,下面这些面试题都是总结当前最新、最热、最高频的面试题,并且每道题都有详细的答案,面试前刷完这套面试题资料,小小offer,不在话下

640套AI大模型报告合集
这套包含640份报告的合集,涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。

这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】

更多推荐


所有评论(0)