微信扫码
和创始人交个朋友
我要投稿
文章内容主要有:
• RAG 流程回顾,
• 语句窗口检索(SWR)的概念,
• SWR 详细实现,
• 如何优化和评估 SWR
回顾一下基础的 RAG 架构,这个架构下对于较小的内容块效果比较好。
刚刚提到,由于RAG对于较小块效果比较好,第一步还是将文件拆成比较小的块,当查询到相关的块之后, 我们围绕之前的语句,进行上下文窗口的扩展,讲较小语句的上下文一起发给 LLM,这就是语句窗口检索。
为了理解语句窗口检索,我画了个架构图,
如果你只是为了了解一下概念,那么读到这里就可以了,后面是实现程序和评估演示。
下面演示如何使用和评估语句窗口检索。
获取和解析文档,和之前一样的步骤:
import warnings
warnings.filterwarnings('ignore')
import utils
import os
import openai
openai.api_key = utils.get_openai_api_key()
from llama_index import SimpleDirectoryReader
documents = SimpleDirectoryReader(
input_files=["./eBook-How-to-Build-a-Career-in-AI.pdf"]
).load_data()
把文件合并成一个文档对象方便我们处理:
from llama_index import Document
document = Document(text="\n\n".join([doc.text for doc in documents]))
创建一个支持 SentenceWindow 的 NodeParser 节点处理器(窗口大小我们默认为3):
from llama_index.node_parser import SentenceWindowNodeParser
# create the sentence window node parser w/ default settings
node_parser = SentenceWindowNodeParser.from_defaults(
window_size=3,
window_metadata_key="window",
original_text_metadata_key="original_text",
)
用标准方法ServiceContext.from_defaults
构建Context,传入我们上一步创建的 node_parser
。
from llama_index.llms import OpenAI
llm = OpenAI(model="gpt-3.5-turbo", temperature=0.1)
from llama_index import ServiceContext
sentence_context = ServiceContext.from_defaults(
llm=llm,
embed_model="local:BAAI/bge-small-en-v1.5",
# embed_model="local:BAAI/bge-large-en-v1.5"
node_parser=node_parser,
)
使用过 VectorStoreIndex 构建 Index,
from llama_index import VectorStoreIndex
sentence_index = VectorStoreIndex.from_documents(
[document], service_context=sentence_context
)
持久化到磁盘,这里我们指定当前相对目录(后续可以从该目录恢复,就不用重复前面的流程了)。
sentence_index.storage_context.persist(persist_dir="./sentence_index")
from llama_index.indices.postprocessor import MetadataReplacementPostProcessor
postproc = MetadataReplacementPostProcessor(
target_metadata_key="window"
)
from llama_index.schema import NodeWithScore
from copy import deepcopy
scored_nodes = [NodeWithScore(node=x, score=1.0) for x in nodes]
nodes_old = [deepcopy(n) for n in nodes]
使用 PostProcess 处理原来的节点。
replaced_nodes = postproc.postprocess_nodes(scored_nodes)
from llama_index.indices.postprocessor import SentenceTransformerRerank
rerank = SentenceTransformerRerank(
top_n=2, model="BAAI/bge-reranker-base"
)
sentence_window_engine = sentence_index.as_query_engine(
similarity_top_k=6, node_postprocessors=[postproc, rerank]
)
window_response = sentence_window_engine.query(
"在人工智能领域建立职业生涯的关键是什么?"
)
最终回应:在人工智能领域建立职业生涯的关键包括学习基础技术技能、
参与项目、找到工作以及成为支持性社区的一部分。
使用使用同样的方法进行评估,同样是构建问题列表,评估两步。
eval_questions = []
with open('generated_questions.text', 'r') as file:
for line in file:
# Remove newline character and convert to integer
item = line.strip()
eval_questions.append(item)
from trulens_eval import Tru
def run_evals(eval_questions, tru_recorder, query_engine):
for question in eval_questions:
with tru_recorder as recording:
response = query_engine.query(question)
下面比较下不同参数下 SWR 的性能如何。
创建窗口大小为 1 的 index:
sentence_index_1 = build_sentence_window_index(
documents,
llm=OpenAI(model="gpt-3.5-turbo", temperature=0.1),
embed_model="local:BAAI/bge-small-en-v1.5",
sentence_window_size=1,
save_dir="sentence_index_1",
)
sentence_window_engine_1 = get_sentence_window_query_engine(
sentence_index_1
)
tru_recorder_1 = get_prebuilt_trulens_recorder(
sentence_window_engine_1,
app_id='sentence window engine 1'
)
创建窗口大小为 3 的 index:
sentence_index_3 = build_sentence_window_index(
documents,
llm=OpenAI(model="gpt-3.5-turbo", temperature=0.1),
embed_model="local:BAAI/bge-small-en-v1.5",
sentence_window_size=3,
save_dir="sentence_index_3",
)
sentence_window_engine_3 = get_sentence_window_query_engine(
sentence_index_3
)
tru_recorder_3 = get_prebuilt_trulens_recorder(
sentence_window_engine_3,
app_id='sentence window engine 3'
)
查看对比:
可以看到窗口大小为 3 的时候,评估效果的三个指标都表现很好。
实际的开发过程中,我们也是需要一次次调整参数,进行评估对比,找出最优的 RAG 方法和参数。
--- END ---
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费场景POC验证,效果验证后签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2025-02-22
TrustRAG:为AI知识检索打造更安全的防线
2025-02-22
DeepSeek+RAGFlow 本地部署避坑指南,一文搞定常见难题!
2025-02-22
基于DeepSeek的本地化知识库 RAGFlow 搭建(附带镜像链接)
2025-02-22
RAGFlow+DeepSeek-R1:14b落地案例分享(足够详细):机加工行业设备维保场景
2025-02-21
DeepRAG:LLM时代的智能检索革命(实测提升准确率21.99%)
2025-02-21
从零开始优化 RAG 流程的终极指南,解决检索增强生成的核心挑战
2025-02-20
本地运行DeepSeek R1 + RAG系统
2025-02-20
传统分块已死?Agentic Chunking拯救语义断裂,实测RAG准确率飙升40%,LLM开发者必看!
2024-09-04
2024-10-27
2024-07-18
2024-05-05
2024-06-20
2024-06-13
2024-07-09
2024-07-09
2024-05-19
2024-07-07
2025-02-15
2025-02-12
2025-02-05
2025-02-05
2025-01-24
2025-01-24
2025-01-20
2025-01-18