This project demonstrates an evaluation framework for assessing the quality of doctor-patient dialogues in a medical chatbot using BERTScore, specifically designed for medical question relevance and clinical interview assessment.
This project creates a simulated medical training environment where:
- A LLaMA 3.1 Aloe Beta 8B model acts as a patient with a specific medical condition
- Medical students or professionals conduct diagnostic interviews by asking questions
- The system evaluates the quality and relevance of doctor's questions using BERTScore with clinical BERT embeddings
- Questions are compared against expert-written ground truth dialogues for the same medical case
Key Innovation: Uses domain-specific clinical BERT model for more accurate medical dialogue evaluation compared to general-purpose metrics.cal Chatbot Evaluation using BERTScore
This project demonstrates an evaluation framework for assessing the quality of doctor-patient dialogues in a medical chatbot using BERTScore, especially tailored for medical question relevance.
In a simulated medical training environment, a chatbot plays the role of a patient, while a medical student or professional asks diagnostic questions. The chatbot responds based on a fixed medical context. The goal of this project is to evaluate how relevant and appropriate the doctorβs questions are, by comparing them with a set of pre-defined expert (ground truth) questions.
- Python 3.8+
- CUDA-compatible GPU (recommended)
- Google Colab account (for easy setup)
-
Clone the repository:
git clone https://github.com/MeerBaloch7/Medical-Chatbot-Evaluation-using-BERTScore.git cd Medical-Chatbot-Evaluation-using-BERTScore -
Run in Google Colab (Recommended):
- Click the "Open in Colab" badge above
- Run all cells sequentially
- The notebook will automatically install all dependencies
-
Local Installation:
pip install transformers bitsandbytes trl accelerate torch bert-score sentence-transformers
-
Start the interactive chat:
# The notebook provides an interactive loop where you can: # - Ask diagnostic questions as a doctor # - Receive patient responses from the AI # - Type 'exit' to evaluate your performance
-
View evaluation results:
- Results are automatically saved to
chat_evaluation.json - Summary includes precision, recall, F1 scores, and alignment metrics
- Results are automatically saved to
-
Patient Simulation Engine
- Model: LLaMA 3.1 Aloe Beta 8B
- Role: Simulates patient responses based on medical context
- Context: Pre-defined medical case (e.g., carpal tunnel syndrome)
-
Question Alignment System
- Model:
sentence-transformers/all-MiniLM-L6-v2 - Purpose: Matches student questions to ground truth using cosine similarity
- Algorithm: Greedy matching to avoid duplicate alignments
- Model:
-
Clinical Evaluation Engine
- Model:
emilyalsentzer/Bio_ClinicalBERT - Metrics: BERTScore (Precision, Recall, F1)
- Specialization: Medical terminology and clinical context understanding
- Model:
BERTScore is an evaluation metric for natural language generation tasks that:
- Computes token-level similarity using contextual embeddings from BERT or other transformer models.
- Measures Precision, Recall, and F1 Score between candidate and reference texts.
- Goes beyond surface-level matching like BLEU or ROUGE by incorporating contextual and semantic similarity.
| Metric | Token-based | Context-aware | Synonym-aware | Suitable for Long Texts |
|---|---|---|---|---|
| BLEU | β Yes | β No | β No | β Poor |
| ROUGE | β Yes | β No | β No | β Somewhat |
| BERTScore | β No | β Yes | β Yes | β Yes |
We use emilyalsentzer/Bio_ClinicalBERT, a BERT-based model pretrained on clinical notes from the MIMIC-III dataset. Itβs well-suited for understanding medical terminology and clinical context, making it an excellent choice for evaluating medical dialogues.
-
Chatbot Simulation:
- A medical chatbot (LLama 3.1 Aloe Beta 8B) plays the role of a patient.
- A human user (doctor/student) asks diagnostic questions in a dialogue.
- The chatbot responds based on a fixed medical context.
-
Dialogue Collection:
- Each user-patient interaction is stored (doctorβs question and chatbotβs response).
-
Ground Truth:
- A JSON file contains the reference (expert-written) doctor dialogue for the same patient case.
-
Alignment & Matching:
- The system aligns user questions to ground truth questions using similarity matching.
- Top relevant question pairs are selected for scoring.
-
Evaluation using BERTScore:
- Each doctor question is scored against the best matching ground truth question using BERTScore (Precision, Recall, F1).
- A summary report is generated including:
- Per-question scores
- Overall averages
- Alignment accuracy (e.g., β8/10 correct, 80% alignedβ)
{
"results": [
{
"student_question": "hi, goerge, how you came today",
"ground_truth_question": "hey george how are you today i understand you're here for some numbness and tingling in your fingers and some pain in your wrist",
"patient_response": "I'm here because my wrist and hand have been hurting for a few months, and I've been having numbness and weakness, especially at night, which is really affecting my work as a landscaper.",
"bertscore_precision": 0.7905,
"bertscore_recall": 0.6965,
"bertscore_f1": 0.7406
},
{
"student_question": "what type of work you do, are you using your left hand mostly in your work chores",
"ground_truth_question": "what kind of work do you do",
"patient_response": "I'm a landscaper, so I use my hands a lot for physical labor, digging, pruning, and operating machinery, and my left hand is definitely more involved in these activities.",
"bertscore_precision": 0.7585,
"bertscore_recall": 0.8480,
"bertscore_f1": 0.8008
}
],
"summary": {
"average_precision": 0.7788,
"average_recall": 0.7592,
"average_f1": 0.7669,
"total_questions": 5
}
}- Average F1 Score: 0.77 (Good clinical relevance)
- Precision vs Recall: Balanced performance indicating comprehensive question coverage
- Medical Context Understanding: Successfully captures clinical terminology and patient presentation
Medical-Chatbot-Evaluation-using-BERTScore/
βββ scoring.ipynb # Main Jupyter notebook with full implementation
βββ ground_truth_doctor_dialogues.json # Expert dialogue reference for carpal tunnel case
βββ chat_evaluation.json # Sample evaluation results
βββ README.md # Project documentation
- Clinical Skills Assessment: Evaluate student interview techniques
- Standardized Training: Consistent evaluation across medical programs
- Progress Tracking: Monitor improvement in diagnostic questioning
- Dialogue Quality Analysis: Quantify medical conversation effectiveness
- AI Training Data: Generate labeled datasets for medical AI development
- Comparative Studies: Benchmark different questioning approaches
- Chatbot Evaluation: Assess medical AI assistant performance
- Training Simulation: Develop realistic patient interaction scenarios
- Quality Assurance: Validate medical dialogue systems
- Support for multiple medical conditions
- Real-time evaluation dashboard
- Integration with medical education platforms
- Multi-language support for global medical training
- Fine-tuning on domain-specific medical dialogues
- Integration with latest clinical language models
- Custom scoring metrics for different medical specialties
- Automated ground truth generation
- BERTScore Paper: BERTScore: Evaluating Text Generation with BERT
- Bio_ClinicalBERT: Publicly Available Clinical BERT Embeddings
- LLaMA Model: HPAI-BSC/Llama3.1-Aloe-Beta-8B
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
MeerBaloch7 - GitHub Profile
For questions or collaboration opportunities, please open an issue in this repository.