python
1from statistics import mean
2
3examples = [
4 {"answer": "The refund window is 30 days.", "context": "Refunds are available for 30 days."},
5 {"answer": "Enterprise support is included.", "context": "Enterprise support requires a paid plan."},
6]
7
8def grounded_score(answer: str, context: str) -> float:
9 answer_terms = {term.lower().strip(".,") for term in answer.split() if len(term) > 3}
10 context_terms = {term.lower().strip(".,") for term in context.split()}
11 return len(answer_terms & context_terms) / max(len(answer_terms), 1)
12
13def evaluate(rows: list[dict[str, str]]) -> dict[str, float]:
14 scores = [grounded_score(row["answer"], row["context"]) for row in rows]
15 return {"mean_groundedness": round(mean(scores), 3)}
16
17print(evaluate(examples))