Spaces:
Runtime error
Runtime error
Update evaluate.py
Browse files283 unmodified lines28428528628728828929029129229310 unmodified lines3043053063073083094 unmodified lines3143153163173183193203214 unmodified lines32632732832933033133233333415 unmodified lines3503513523533543553562 unmodified lines359360361362363364365283 unmodified lineselif not expected_sources_set and not actual_sources_set:context_precision, context_recall = 1.0, 1.0print("\n" + "-"*20 + " ANSWER & CORRECTNESS EVALUATION " + "-"*20)print(f" - Ground Truth Answer: {ground_truth_answer}")print(f" - Generated Answer: {answer_text}")print("-" * 59)answer_correctness_score = Noneif ground_truth_answer and "ERROR" not in answer_text:10 unmodified linesprint(f"ERROR during answer correctness judging: {e}")faithfulness = Nonesource_docs = response.get("source_documents", [])if source_docs and "ERROR" not in answer_text:context_blob = "\n---\n".join([doc.page_content for doc in source_docs])4 unmodified linesdata = _parse_judge_json(raw)if data:denom = data.get("supported", 0) + data.get("contradicted", 0) + data.get("not_enough_info", 0)if denom > 0: faithfulness = round(data.get("supported", 0) / denom, 3)elif data.get("ignored", 0) > 0: faithfulness = 1.0except Exception as e:print(f"ERROR during faithfulness judging: {e}")4 unmodified lines"behavior_f1": f"{behavior_metrics['f1_score']:.2f}", "emotion_f1": f"{emotion_metrics['f1_score']:.2f}","topic_f1": f"{topic_metrics['f1_score']:.2f}", "context_f1": f"{context_metrics['f1_score']:.2f}","generated_answer": answer_text, "sources": sources_pretty, "source_count": len(actual_sources_set),"latency_ms": latency_ms, "faithfulness": faithfulness,"context_precision": context_precision, "context_recall": context_recall,"answer_correctness": answer_correctness_score,"category": category,"error_class": error_class})15 unmodified linestf1_mean = to_f(df["topic_f1"]).mean() * 100cf1_mean = to_f(df["context_f1"]).mean() * 100# Add the NLU metrics to the summary f-stringsummary_text = f"""## Evaluation Summary- **Routing Accuracy**: {pct:.2f}%- **Behaviour F1 (avg)**: {bf1_mean:.2f}%2 unmodified lines- **Context F1 (avg)**: {cf1_mean:.2f}%- **RAG: Context Precision**: {(to_f(df["context_precision"]).mean() * 100):.1f}%- **RAG: Context Recall**: {(to_f(df["context_recall"]).mean() * 100):.1f}%- **RAG: Answer Correctness (LLM-judge)**: {(to_f(df["answer_correctness"]).mean() * 100):.1f}%"""# --- END OF MODIFICATION ---283 unmodified lines28428528628728828929029129229329410 unmodified lines3053063073083093103114 unmodified lines3163173183193203213223233243253263273284 unmodified lines33333433533633733833934034134215 unmodified lines3583593603613623633643653663673683693703713722 unmodified lines375376377378379380381382383384385283 unmodified lineselif not expected_sources_set and not actual_sources_set:context_precision, context_recall = 1.0, 1.0# TURN DEBUG on Answer Correctness# print("\n" + "-"*20 + " ANSWER & CORRECTNESS EVALUATION " + "-"*20)# print(f" - Ground Truth Answer: {ground_truth_answer}")# print(f" - Generated Answer: {answer_text}")# print("-" * 59)answer_correctness_score = Noneif ground_truth_answer and "ERROR" not in answer_text:10 unmodified linesprint(f"ERROR during answer correctness judging: {e}")faithfulness = Nonehallucination_rate = Nonesource_docs = response.get("source_documents", [])if source_docs and "ERROR" not in answer_text:context_blob = "\n---\n".join([doc.page_content for doc in source_docs])4 unmodified linesdata = _parse_judge_json(raw)if data:denom = data.get("supported", 0) + data.get("contradicted", 0) + data.get("not_enough_info", 0)if denom > 0:faithfulness = round(data.get("supported", 0) / denom, 3)hallucination_rate = 1.0 - faithfulnesselif data.get("ignored", 0) > 0:faithfulness = 1.0hallucination_rate = 0.0except Exception as e:print(f"ERROR during faithfulness judging: {e}")4 unmodified lines"behavior_f1": f"{behavior_metrics['f1_score']:.2f}", "emotion_f1": f"{emotion_metrics['f1_score']:.2f}","topic_f1": f"{topic_metrics['f1_score']:.2f}", "context_f1": f"{context_metrics['f1_score']:.2f}","generated_answer": answer_text, "sources": sources_pretty, "source_count": len(actual_sources_set),"faithfulness": faithfulness, "hallucination_rate": hallucination_rate,"context_precision": context_precision, "context_recall": context_recall,"answer_correctness": answer_correctness_score,"latency_ms": latency_ms,"category": category,"error_class": error_class})15 unmodified linestf1_mean = to_f(df["topic_f1"]).mean() * 100cf1_mean = to_f(df["context_f1"]).mean() * 100# Calculate the mean for Faithfulnessfaith_mean = to_f(df["faithfulness"]).mean() * 100# --- CHANGE 6: Calculate the mean for the new metric ---halluc_mean = to_f(df["hallucination_rate"]).mean() * 100rag_with_sources_pct = (df["source_count"] > 0).mean() * 100 if "source_count" in df else 0# Add the NLU metrics to the summary f-string# Choose to use Hallucination - **RAG: Faithfulness**: {faith_mean:.1f}%summary_text = f"""## Evaluation Summary- **Routing Accuracy**: {pct:.2f}%- **Behaviour F1 (avg)**: {bf1_mean:.2f}%2 unmodified lines- **Context F1 (avg)**: {cf1_mean:.2f}%- **RAG: Context Precision**: {(to_f(df["context_precision"]).mean() * 100):.1f}%- **RAG: Context Recall**: {(to_f(df["context_recall"]).mean() * 100):.1f}%- **RAG Answers w/ Sources**: {rag_with_sources_pct:.1f}%- **RAG: Hallucination Rate**: {halluc_mean:.1f}% (Lower is better)- **RAG: Answer Correctness (LLM-judge)**: {(to_f(df["answer_correctness"]).mean() * 100):.1f}%- **RAG: Avg Latency (ms)**: {to_f(df["latency_ms"]).mean():.1f}"""# --- END OF MODIFICATION ---
evaluate.py+28-8