Java 后端面试题库 / 全部题解免费阅读理解原理,有条理地回答

怎样增加一个自定义后处理器?

先说出你的答案

定义边界、关键机制、一个反例;P8题再补容量、失败与验证。

查看完整题解

参考回答

直接回答

继承 BaseNodePostprocessor,实现 _postprocess_nodes,接收节点列表和可选查询对象,再返回过滤或重排后的列表。配置时将它传给查询引擎或直接调用。

讲清原理

后处理能整理已取的证据,例如去重、按日期筛过期条款或重排。过滤过猛时会把有用证据一起丢掉,阈值要在代表性数据上试。

例子与易错点

示例提供两条带分数的候选,用同一阈值过滤后留下较高分条目。单测明确覆盖没有分数的节点该怎样处理。

from llama_index.core.postprocessor.types import BaseNodePostprocessor
from llama_index.core.schema import NodeWithScore, TextNode

class MinScore(BaseNodePostprocessor):
    cutoff: float

    def _postprocess_nodes(self, nodes, query_bundle=None):
        return [hit for hit in nodes if hit.score is not None and hit.score >= self.cutoff]

candidates = [
    NodeWithScore(node=TextNode(id_="strong", text="Direct evidence"), score=0.8),
    NodeWithScore(node=TextNode(id_="weak", text="Related comment"), score=0.3),
    NodeWithScore(node=TextNode(id_="unrated", text="Unrated result")),
]
kept = MinScore(cutoff=0.7).postprocess_nodes(candidates)
assert [hit.node.node_id for hit in kept] == ["strong"]
print("strong match kept; weak and unrated candidates excluded")

相关题:第 33 题、第 48 题、第 55 题、第 56 题、第 57 题。

这道题,你掌握得怎么样?

掌握程度由你主动标记,阅读和收藏不会自动改变状态。