跳到内容

后处理器

基类: QueryComponent

后处理器组件。

参数

名称 类型 描述 默认值
postprocessor BaseNodePostprocessor

后处理器

必需
源码位于 llama-index-core/llama_index/core/postprocessor/types.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
class PostprocessorComponent(QueryComponent):
    """Postprocessor component."""

    model_config = ConfigDict(arbitrary_types_allowed=True)
    postprocessor: SerializeAsAny[BaseNodePostprocessor] = Field(
        ..., description="Postprocessor"
    )

    def set_callback_manager(self, callback_manager: CallbackManager) -> None:
        """Set callback manager."""
        self.postprocessor.callback_manager = callback_manager

    def _validate_component_inputs(self, input: Dict[str, Any]) -> Dict[str, Any]:
        """Validate component inputs during run_component."""
        # make sure `nodes` is a list of nodes
        if "nodes" not in input:
            raise ValueError("Input must have key 'nodes'")
        nodes = input["nodes"]
        if not isinstance(nodes, list):
            raise ValueError("Input nodes must be a list")
        for node in nodes:
            if not isinstance(node, NodeWithScore):
                raise ValueError("Input nodes must be a list of NodeWithScore")

        # if query_str exists, make sure `query_str` is stringable
        if "query_str" in input:
            input["query_str"] = validate_and_convert_stringable(input["query_str"])

        return input

    def _run_component(self, **kwargs: Any) -> Any:
        """Run component."""
        output = self.postprocessor.postprocess_nodes(
            kwargs["nodes"], query_str=kwargs.get("query_str")
        )
        return {"nodes": output}

    async def _arun_component(self, **kwargs: Any) -> Any:
        """Run component (async)."""
        # NOTE: no native async for postprocessor
        return self._run_component(**kwargs)

    @property
    def input_keys(self) -> InputKeys:
        """Input keys."""
        return InputKeys.from_keys({"nodes"}, optional_keys={"query_str"})

    @property
    def output_keys(self) -> OutputKeys:
        """Output keys."""
        return OutputKeys.from_keys({"nodes"})

input_keys 属性 #

input_keys: InputKeys

输入键。

output_keys 属性 #

output_keys: OutputKeys

输出键。

set_callback_manager #

set_callback_manager(callback_manager: CallbackManager) -> None

设置回调管理器。

源码位于 llama-index-core/llama_index/core/postprocessor/types.py
101
102
103
def set_callback_manager(self, callback_manager: CallbackManager) -> None:
    """Set callback manager."""
    self.postprocessor.callback_manager = callback_manager