Bases: BaseNodePostprocessor
Fixed Recency post-processor.
This post-processor does the following steps orders nodes by date.
Assumes the date_key corresponds to a date field in the metadata.
Source code in llama-index-core/llama_index/core/postprocessor/node_recency.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 | class FixedRecencyPostprocessor(BaseNodePostprocessor):
"""Fixed Recency post-processor.
This post-processor does the following steps orders nodes by date.
Assumes the date_key corresponds to a date field in the metadata.
"""
top_k: int = 1
date_key: str = "date"
@classmethod
def class_name(cls) -> str:
return "FixedRecencyPostprocessor"
def _postprocess_nodes(
self,
nodes: List[NodeWithScore],
query_bundle: Optional[QueryBundle] = None,
) -> List[NodeWithScore]:
"""Postprocess nodes."""
if query_bundle is None:
raise ValueError("Missing query bundle in extra info.")
# sort nodes by date
node_dates = pd.to_datetime(
[node.node.metadata[self.date_key] for node in nodes]
)
sorted_node_idxs = np.flip(node_dates.argsort())
sorted_nodes = [nodes[idx] for idx in sorted_node_idxs]
return sorted_nodes[: self.top_k]
|