A retrieval-augmented answer can fail in three places: the retrieved context is off-topic, the answer says things the context does not support, or the answer does not address the question. Score all three from embeddings. Upstream steps (already done) split the answer into atomic claims, generated questions that the answer would answer, and embedded everything.
Implement rag_triad(query_emb, context_embs, claim_embs, gen_question_embs, support_threshold=0.8, context_threshold=0.5).
All inputs are lists of equal-length vectors (the query is one vector). Cosine similarity is , defined as 0 if either vector is all zeros.
support_threshold (no chunks means unsupported).faithfulness: fraction of claims supported; None if there are no claims.unsupported: sorted indices of unsupported claims.answer_relevance: mean cosine between the query and each generated question; 0.0 if there are none.context_relevance: fraction of context chunks whose cosine to the query is context_threshold; 0.0 if there are none.Return a dict with those four keys; floats rounded to 4 decimals.