AutoRAG Threshold Cutoff 节点实战:基于上游检索分数剔除低相关段落
2026/9/18 18:58:32 网站建设 项目流程

AutoRAG Threshold Cutoff 节点实战:基于上游检索分数剔除低相关段落

【免费下载链接】AutoRAGAutoRAG: Now your agent can find anything in your computer. It gets smarter if you are using it frequently.项目地址: https://gitcode.com/GitHub_Trending/au/AutoRAG

导读

Threshold Cutoff 是 AutoRAG(legacy 版)passage filter(段落过滤)节点下最轻量的过滤模块之一:它不调用任何 embedding 模型、不重新计算相似度,而是直接基于上一个检索/重排节点产出的retrieve_scores,用一条阈值规则决定哪些段落被保留。本文从模块定位、参数语义、源码实现到完整 YAML 配置与测试验证,逐步拆解这个模块的用法与内部原理,读完即可在 AutoRAG 项目配置中正确接入并使用它。

一、模块定位:passage filter 节点中的“纯规则”过滤

在 AutoRAG 的检索流水线中,passage filter 节点的作用是删除与查询相关性不足的段落,而不是压缩或改写段落。与 passage reranker 不同,过滤模块不保证返回固定数量的段落——可能一条都没被过滤,也可能被过滤到只剩 1 条,具体取决于阈值与分数分布(见 legacy/docs/source/nodes/passage_filter/passage_filter.md)。

Threshold Cutoff 属于该类节点下最简单的实现,其官方定义为:根据“上一步结果”的分数(previous result's scores)过滤 contents、scores 与 ids。它的设计灵感来自 Similarity Threshold Cutoff,两者的关键差异在于:

  • Similarity Threshold Cutoff:需要用 embedding 模型把 query 与每个 content 重新向量化,再计算余弦相似度作为过滤依据(见 legacy/autorag/nodes/passagefilter/similarity_threshold_cutoff.py 的_pure实现);
  • Threshold Cutoff:直接复用上游节点已经算好的分数,纯规则运算,零额外模型开销、零推理延迟,适合嵌入到多模块对比的配置中作为“低成本基线”参与评估。

二、模块参数详解

Threshold Cutoff 只暴露两个参数(见 legacy/docs/source/nodes/passage_filter/threshold_cutoff.md):

参数类型必填默认值语义
thresholdfloat✅ 是过滤阈值。默认方向下,分数低于该值的段落会被过滤掉;模块必须设置此参数才能运行
reverseboolFalse若为True,表示“分数越低越好”,此时过滤规则反转:分数高于阈值的段落会被过滤掉

注意:文档中“If the score is below the threshold, the content will be filtered out”针对的是默认的reverse=False场景(分数越高越好,常见于相似度/相关性分数);当某些检索器的分数语义是“距离越小越相关”(例如部分稀疏检索或距离型度量)时,应把reverse设为True

三、源码级原理:逐行拆解_pure__row_pure

核心实现位于 legacy/autorag/nodes/passagefilter/threshold_cutoff.py(类ThresholdCutoff(BasePassageFilter))。整个过滤链路分两层:

3.1 入口pure:从 DataFrame 取上游结果

@result_to_dataframe(["retrieved_contents", "retrieved_ids", "retrieve_scores"]) def pure(self, previous_result: pd.DataFrame, *args, **kwargs): _, contents, scores, ids = self.cast_to_run(previous_result) return self._pure(contents, scores, ids, *args, **kwargs)
  • cast_to_run由基类 legacy/autorag/nodes/passagefilter/base.py 提供:它会先validate_qa_dataset校验数据,再断言上游结果必须包含query列,随后通过cast_retrieve_infos从 DataFrame 中取出retrieved_contentsretrieve_scoresretrieved_ids三列。
  • 输出通过@result_to_dataframe装饰器重新封装为同名三列,保持与下游节点(如 passage compressor)的输入契约一致。

3.2 批量层_pure:索引映射,三列同步裁剪

remain_indices = list( map(lambda x: self.__row_pure(x, threshold, reverse), scores_list) ) remain_content_list = list( map(lambda c, idx: [c[i] for i in idx], contents_list, remain_indices) ) # ... 对 scores_list、ids_list 做同样的索引映射

对每个 query,先用__row_pure算出应保留的索引列表,再对 contents、scores、ids 三列做相同的索引切片,保证三列始终一一对应、长度一致。

3.3 单行规则__row_pure:阈值过滤 + 保底逻辑

if reverse: remain_indices = [ i for i, score in enumerate(scores_list) if score <= threshold ] default_index = scores_list.index(min(scores_list)) else: remain_indices = [ i for i, score in enumerate(scores_list) if score >= threshold ] default_index = scores_list.index(max(scores_list)) return remain_indices if remain_indices else [default_index]

这是整个模块最核心的 6 行逻辑,蕴含两个关键行为:

  1. 比较方向
    • reverse=False:保留score >= threshold的索引(分数越高越相关);
    • reverse=True:保留score <= threshold的索引(分数越低越相关)。
  2. 保底机制(文档中的 📣 要点):如果某条 query 的所有分数都低于(或高于)阈值,remain_indices为空列表,此时模块强制保留分数最优的那一条——默认方向取max(scores_list),反向取min(scores_list)。这保证了过滤后每个 query 至少保留 1 条段落,不会出现“该 query 无任何上下文可给 LLM”的空结果。

四、配置文件写法:从最小配置到完整节点

4.1 最小可运行配置

与官方文档 threshold_cutoff.md 中的示例一致:

modules: - module_type: threshold_cutoff threshold: 0.85

4.2 在节点上下文中完整使用

参照 AutoRAG 官方示例配置 legacy/sample_config/rag/korean/non_gpu/full_korean.yaml 中的 passage_filter 节点,将多个过滤模块并列,由 AutoRAG 的寻优策略自动选出最佳模块:

node_lines: - node_line_name: retrieve_node_line nodes: - node_type: passage_filter strategy: metrics: [ retrieval_f1, retrieval_recall, retrieval_precision ] speed_threshold: 5 modules: - module_type: pass_passage_filter # 不启用过滤的对照组 - module_type: similarity_threshold_cutoff # 基于重算余弦相似度的阈值过滤 threshold: 0.85 - module_type: similarity_percentile_cutoff percentile: 0.6 - module_type: threshold_cutoff # 本文主角:纯分数阈值过滤 threshold: 0.85 - module_type: percentile_cutoff percentile: 0.6

几点实战提示:

  • 务必搭配pass_passage_filter对照组:该模块是“不使用任何过滤”的基线,用于验证过滤是否真的带来收益(见 passage_filter.md 中关于pass_passage_filter的说明);
  • threshold取值应与上游检索器分数的量纲匹配。若上游是相似度(0~1),可参考0.85这类经验值;若上游分数区间不同,建议先用percentile_cutoffsimilarity_percentile_cutoff这类与量纲无关的模块做对比(参见 percentile_cutoff.md 与 similarity_percentile_cutoff.md);
  • strategy.metrics使用retrieval_f1retrieval_recallretrieval_precision,用于评估每个过滤模块并挑选最优结果(对应 legacy/autorag/nodes/passagefilter/run.py 中run_passage_filter_node的评估、filter_by_thresholdselect_best流程)。

五、测试用例验证:行为与边界条件

AutoRAG 为 Threshold Cutoff 提供了专门的单元测试与节点级测试,位于 legacy/tests/autorag/nodes/passagefilter/test_threshold_cutoff.py,可用来精确理解其行为边界:

scores_example = [[0.1, 0.8, 0.1, 0.5], [0.1, 0.2, 0.7, 0.3]]
  • 正向过滤threshold=0.6时,第一行保留0.8,第二行保留0.7,其余低于阈值的分数(如0.10.5)被剔除;
  • reverse 模式threshold=0.4, reverse=True时,第一行保留两个0.1的段落,验证了反向比较逻辑;
  • numpy 输入兼容:测试传入np.array类型的分数也能正常运行,说明模块对数值类型有良好的容错;
  • 保底机制:测试基类 legacy/tests/autorag/nodes/passagefilter/test_passage_filter_base.py 中的base_passage_filter_test断言了过滤后每个 query 的 contents/ids/scores 长度均大于 0,与__row_pure中“至少保留 1 条”的保底实现相互印证;
  • 节点级测试test_threshold_cutoff_node通过ThresholdCutoff.run_evaluator直接跑完整评估流程,断言输出 DataFrame 包含retrieved_contentsretrieved_idsretrieve_scores三列——这正对应 run.py 中节点调度的真实数据契约。

六、适用场景与使用建议

综合源码与官方文档,可以给出以下选型建议:

  • 适合使用 Threshold Cutoff 的场景:上游检索/重排分数本身质量可靠、量纲稳定(如 BM25 归一化分数、重排器分数),且希望以零额外计算成本快速剔除低分噪声段落;
  • 不适合的场景:上游分数不可靠、跨模块分数量纲不一致时,阈值难以统一设定,此时更推荐similarity_percentile_cutoff(按相似度分位数过滤)或percentile_cutoff(按分数分位数过滤),它们对分数分布更鲁棒;
  • 始终关注保底行为:模块会为分数全部不达标的 query 强制保留最优段落,因此它不会产生“空上下文”,但也不要指望它保证输出条数的上界——过滤后的段落数量取决于分数分布,这正是 passage filter 与 reranker 的本质区别(reranker 通过top_k固定输出条数,见 passage_filter.md)。

如果希望进一步了解该模块的 API 签名与自动化文档,可参考 legacy/docs/source/api_spec/autorag.nodes.passagefilter.rst;模块注册入口见 legacy/autorag/nodes/passagefilter/init.py。

【免费下载链接】AutoRAGAutoRAG: Now your agent can find anything in your computer. It gets smarter if you are using it frequently.项目地址: https://gitcode.com/GitHub_Trending/au/AutoRAG

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询