🛡️ MedGuard: An LLM-Based Gatekeeper for Detecting Clinical Risks in Chinese Telemedicine Consultations
MedGuard is an intelligent agent system based on Large Language Models, designed for clinical risk monitoring and alerting in Chinese telemedicine consultations. The system integrates autonomously planned retrieval-augmented generation with reinforcement learning-based fine-tuning to overcome key limitations of conventional LLMs, including difficulties in processing long conversational texts, over-affirmation bias, and hallucination.
MedGuard extracts atomic medical claims from doctor-patient dialogues and leverages a constructed authoritative medical knowledge base (containing 16,535 disease-related entries and 187,738 drug-related entries) for retrieval and verification, enabling precise risk identification and explainable reasoning output for critical aspects such as diagnosis and prescription.
├── 📁 agent-lightning/ # Agent Lightning framework for RL training
├── 📁 agent/ # Complete search agent implementation with baselines
├── 📁 MedGuard_EVAL/ # Evaluation benchmark dataset
├── 📁 retrieval_database/ # Retrieval database and search server
├── 📁 data_generation/ # Data generation scripts
├── 📁 conversation_decompose/ # Claim decomposition utilities
├── 🐍 server.py # Main server script
├── ⚙️ train.sh # Training script for RL fine-tuning
└── 📝 requirements.txt # Project dependencies
# Clone the repository
git clone https://github.com/your-org/medguard.git
cd medguard
# Install dependencies
pip install -r requirements.txtThe retrieval database is built under the retrieval_database directory.
Create a JSONL file with the following format for the search server:
{
"name": "Drug Name",
"content": "Detailed information about the drug"
}Use the expand script to augment drug names:
python retrieval_database/expand_names.pypython retrieval_database/search_server.pyReplace the reward calculation in VERL with the MedFactV1 reward format from the agent directory.
Run the main server script to start the retrieval server, retrieval model, and training process:
python server.py --mode trainUse a QA dataset to generate claim data:
python data_generation/generate_claim_data.py --input qa_dataset.jsonl --output claims.jsonlSmooth the data distribution to ensure balanced training:
python data_generation/balance_data.py --input claims.jsonl --output balanced_claims.jsonlUse the search_data_gen script to convert data to the required format for training:
python data_generation/search_data_gen.py --input balanced_claims.jsonl --output train_data.parquetThe conversation_decompose module handles the decomposition of medical conversations into individual claims for more precise fact-checking:
python conversation_decompose/decompose.py --input conversation.jsonl --output claims.jsonlThe MedGuardEval benchmark comprises:
- Real telemedicine conversations
- Reconstructed clinical cases
- Drug knowledge-infused synthetic dialogues
Evaluation metrics include:
- 🎯 Factual Accuracy
⚠️ Risk Detection Rate- 📝 Explainability Score
- ⏱️ Response Time
This project is released under the MIT License. See the LICENSE file for details.