惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - ExplorerMan

大模型sft微调参数优化2 大模型RAG实战,从被骂不靠谱到成为部门MVP,这是我的踩坑全记录【转】 推荐 Prompt 模板(大幅提升 JSON 质量) 渐进式SFT内化 [推荐]双塔模型(介绍) Agno - 轻量级Python多智能体系统框架 Open WebUI:打造友好且强大的自托管 AI 平台 【gradio】使用Gradio快速开发前端界面:基础知识 文本切割方案进化概览:从“机械切割”到“智能解构” SemanticChunker 语义相似拆分 基于LangChain 实现 Advanced RAG-后检索优化(上)-Reranker 基于LangChain 实现 Advanced RAG-后检索优化(下)-上下文压缩与过滤 多Agent协作入门:基于A2A协议的Agent通信(中) ollama部署与open-webui 0基础也能看懂!从0到1手把手教你本地部署大模型Ollama 什么是 AutoModel 大模型基础应用框架(ReACT\SFT\RAG)技术创新及零售业务落地应用 - ExplorerMan - 博客园 多模态Embedding模型:从文本到多模态的全面选型指南! rag 查询检索轮换
大模型RAG的上下文压缩与过滤
ExplorerMan · 2025-10-11 · via 博客园 - ExplorerMan

一、为什么要压缩 & 过滤
检索器一次拉回 top-k 篇文档,其中 80% 的 token 与问题无关 →

  1. 浪费 LLM 上下文窗口

  2. 无关内容引入幻觉
    目标:在“送进 LLM 之前”就把无用段落/句子/ token 丢掉或压成更短的表示。

二、整体两阶段流水线
query


[1. 检索] ─► 原始文档集合 D=[d1,…,dk]


[2. 压缩+过滤] ─► 精炼文档集合 D̂=[d̂1,…,d̂m] (m≤k, |d̂i|≤|di|)


[3. 生成] ──► 答案

三、四种常用压缩/过滤策略
A. FILTER – 保留与 query 相关的句子,其余扔掉
B. SUMMARY – 把长文做抽取/生成式摘要
C. PRECISION– 对结构化字段截断小数位、只保留关键键值
D. HYBRID – 先 A→再 B→必要时 C,兼顾精度与压缩率

四、伪代码实现(单文件即可跑通思路)

Python

复制

# 1. 基础数据结构
@dataclass
class Document:
    content: str
    score: float = 0.0          

五、关键技巧小结

  1. 句子级过滤:用 embedding 相似度或 cross-encoder 重排,速度快。

  2. 摘要失败兜底:摘要模型可能输出空,直接退回到原文截断或关键字段抽取。

  3. 压缩率计算:真实压缩比 = 原始总 token 数 / 精炼后 token 数,可在 _compress_all 里顺手统计。

  4. 并行化:map-reduce 思路,把长文档先切小块,过滤后再合并,能线性加速 。

  5. 训练式压缩:如果数据足,可参考 FlexRAG 用“可学习的摘要向量”做端到端压缩

把上面伪代码 copy 下来,把 RetrieverGenerator 换成你的实际实现,即可得到一个带“上下文压缩 + 过滤”的 RAG 原型。祝玩得开心!