Skip to content

Repository files navigation

🛡️ MedGuard: An LLM-Based Gatekeeper for Detecting Clinical Risks in Chinese Telemedicine Consultations

📋 Overview

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.


🏗️ Repository Structure

├── 📁 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

🔧 Installation

# Clone the repository
git clone https://github.com/your-org/medguard.git
cd medguard

# Install dependencies
pip install -r requirements.txt

🚀 Main Training Pipeline

1. 📚 Build Retrieval Database

The retrieval database is built under the retrieval_database directory.

1.1 Prepare Data

Create a JSONL file with the following format for the search server:

{
  "name": "Drug Name",
  "content": "Detailed information about the drug"
}

1.2 (Optional) Expand Drug Names

Use the expand script to augment drug names:

python retrieval_database/expand_names.py

1.3 Start Search Server

python retrieval_database/search_server.py

2. 🎯 Replace Reward Calculation in VERL

Replace the reward calculation in VERL with the MedFactV1 reward format from the agent directory.

3. 🏋️‍♂️ Start Training

Run the main server script to start the retrieval server, retrieval model, and training process:

python server.py --mode train

📊 Data Generation Process

1. 🧾 Generate Claim Data

Use a QA dataset to generate claim data:

python data_generation/generate_claim_data.py --input qa_dataset.jsonl --output claims.jsonl

2. ⚖️ Balance Data Distribution (Optional)

Smooth the data distribution to ensure balanced training:

python data_generation/balance_data.py --input claims.jsonl --output balanced_claims.jsonl

3. 🔄 Convert to Training Format

Use 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.parquet

🧩 Claim Decomposition

The 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.jsonl

📈 Evaluation

The 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

📄 License

This project is released under the MIT License. See the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages