FaithfulnessEvaluator gating
FullTask: Implement the Faithfulness-Drop Discipline
In the provided LlamaIndex + Qdrant glossary pipeline, a definition is only accepted if it is faithful to the retrieved document contexts. You must write a function that gates a generated answer using a FaithfulnessEvaluator instance.
Specification
Write a function:
def apply_faithfulness_gate(
definition: str,
contexts: list[str],
faithfulness: FaithfulnessEvaluator
) -> tuple[bool, bool]:
- Inputs:
definition: the generated answer string (e.g., a glossary definition).contexts: a list of text strings from the retrieved nodes (the source documents).faithfulness: an instance ofllama_index.core.evaluation.FaithfulnessEvaluator(already initialised with an LLM).
- Return: a tuple
(is_faithful: bool, error_occurred: bool):is_faithful:Trueif the evaluation passes (the answer is supported by the contexts),Falseotherwise.error_occurred:Trueif the evaluation call raised an exception (e.g., transport/parse error),Falseotherwise.
Behaviour
- Use
faithfulness.evaluate(response=definition, contexts=contexts).passingto check faithfulness. - If the call succeeds, return
(passing, False). - If the call raises any
Exception(catch withexcept Exception as exc:), print the following warning (exactly as shown) using an f-string:Then returnf"[glossary-li] WARNING faithfulness eval errored for term: {exc}; skipped"(False, True)— treat the error as a drop. - Do not crash the calling program; the error is handled gracefully.
Important
- Use exactly the real class
FaithfulnessEvaluatorand method.evaluate(...).passing. - The function must be self-contained and not rely on any global state.
- This is a blank-slate task; you start with an empty file.
Your code
Sources
- roadmap-kg/kg/glossary_llamaindex.py:215-223
- roadmap-kg/kg/glossary_llamaindex.py:267-311
- roadmap-kg/kg/glossary_llamaindex.py:384-420