mellea.stdlib.requirements.rag
Requirements for RAG (Retrieval-Augmented Generation) workflows.
Classes
CLASS GroundednessRequirement
Requirement that validates LLM responses are grounded by citations.
This requirement implements a sophisticated 4-step validation pipeline to ensure that LLM responses are fully grounded by citations to provided documents:
- Citation Generation: Generate citations for the response using the find_citations adapter function.
- Citation Necessity: Identify which response spans require citations (vs. conversational/inference text that doesn't need citations).
- Citation Support: For spans that need citations, assess the level of citation support: fully, partially, or not supported.
- Groundedness Output: Declare response grounded iff all spans needing citations are fully supported.
Important: The backend must implement AdapterMixin and must actually
provide the citation adapter used by the find_citations adapter function (the
AdapterMixin check alone does not guarantee that; if the adapter is missing,
validate() returns an errored result rather than a verdict). The backend must
also support the two LLM-as-Judge calls (generate_from_context with a chat
context and ModelOption.MAX_NEW_TOKENS).
If validation cannot run (setup problem, adapter or backend failure, or
unreadable judgment output), the returned ValidationResult has error set
and reason set to None, instead of reporting an ordinary failure.
Args:
documents: Optional documents to validate against. Can be Document objects or strings (will be converted to Documents). If provided, these documents will be used instead of documents attached to messages in the context. Default is None (use context documents).allow_partial_support: Whether to accept partially supported spans as grounded. If False (default), response is grounded iff all spans needing citations are FULLY supported. If True, response is grounded if spans are fully or partially supported.max_new_tokens: Maximum tokens for LLM judgment outputs. Increase this if LLM outputs are being truncated (particularly for complex responses with many spans). Default is 500.description: Custom description for the requirement. If None, generates a default description.
Methods:
FUNC validate
validate(self, backend: Backend, ctx: Context) -> ValidationResult
Validate groundedness of the response using the 4-step pipeline.
Args:
backend: Backend to use for citation detection and LLM judgment. Must implementAdapterMixinand provide the citation adapter used by the find_citations adapter function. The backend must also support the two LLM-as-Judge calls (generate_from_contextwith a chat context andModelOption.MAX_NEW_TOKENS).ctx: Context containing the conversation historyformat: Unused for this requirementmodel_options: Unused for this requirement
Returns:
- ValidationResult with pass/fail status and detailed reason. When
- validation could not run or could not reach a verdict, an errored
- result (
reason=None,errorset) is returned instead.