----------------------
Yes, you can create custom postprocessors in LlamaIndex. The base class for creating a custom postprocessor is
BaseNodePostprocessor
. Here is an example of a simple custom postprocessor:
from llama_index.core import QueryBundle
from llama_index.core.postprocessor.types import BaseNodePostprocessor
from llama_index.core.schema import NodeWithScore
class DummyNodePostprocessor(BaseNodePostprocessor):
def _postprocess_nodes(
self, nodes: List[NodeWithScore], query_bundle: Optional[QueryBundle]
) -> List[NodeWithScore]:
# subtracts 1 from the score
for n in nodes:
n.score -= 1
return nodes
(Source:
LlamaIndex Documentation ())
For postprocessing based on metadata, you can use the
MetadataReplacementPostProcessor
. This postprocessor replaces the node content with a field from the node metadata. If the field is not present in the metadata, then the node text remains unchanged. Here is an example of how to use it:
from llama_index.core.postprocessor import MetadataReplacementPostProcessor
postprocessor = MetadataReplacementPostProcessor(
target_metadata_key="window",
)
postprocessor.postprocess_nodes(nodes)
(Source:
LlamaIndex Documentation ())
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------