Skip to content

AMG-RAG (Agentic Medical Graph-RAG) is a comprehensive framework that automates the construction and continuous updating of Medical Knowledge Graphs (MKGs), integrates reasoning, and retrieves current external evidence for medical Question Answering (QA).

License

Notifications You must be signed in to change notification settings

MrRezaeiUofT/AMG-RAG

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AMG-RAG: Agentic Medical Graph-RAG

Python 3.8+ License: MIT Paper

Overview

AMG-RAG (Agentic Medical Graph-RAG) is a comprehensive framework that automates the construction and continuous updating of Medical Knowledge Graphs (MKGs), integrates reasoning, and retrieves current external evidence for medical Question Answering (QA). Our approach addresses the challenge of rapidly evolving medical knowledge by dynamically linking new findings and complex medical concepts.

AMG-RAG Demo

πŸš€ Key Features

  • 🧠 Enhanced Knowledge Graph Construction: Advanced entity extraction with confidence scoring (1-10 scale)
  • πŸ”„ Bidirectional Relationship Analysis: Comprehensive relationship mapping with confidence scoring
  • 🎯 Context-Aware Entity Processing: LLM-generated descriptions with medical context integration
  • πŸ“š Multi-source Evidence Retrieval: Integrates PubMed search, Wikipedia, and vector database retrieval
  • πŸ”— Chain-of-Thought Reasoning: Structured reasoning synthesis with evidence integration
  • ⚑ Real-time Graph Updates: Dynamically incorporates latest medical literature and research
  • πŸ“Š Entity Summarization: Enhanced entity understanding with relevance-based confidence scoring

πŸ“ˆ Performance

Our evaluations on standard medical QA benchmarks demonstrate superior performance:

Dataset Score Metric
MEDQA 74.1% F1 Score
MEDMCQA 66.34% Accuracy

AMG-RAG surpasses both comparable models and those 10 to 100 times larger, while enhancing interpretability for medical queries.

πŸ—οΈ Architecture

The enhanced AMG-RAG system consists of six key components:

1. Enhanced Entity Extraction

  • Structured output with relevance scoring (1-10 scale)
  • Context-aware entity descriptions
  • Confidence scoring based on relevance and external sources

2. Advanced Knowledge Graph Construction

  • Bidirectional relationship analysis (Aβ†’B and Bβ†’A)
  • Medical relationship types (treats, causes, symptom_of, risk_factor_for, etc.)
  • Evidence-based confidence scoring

3. Multi-source Evidence Retrieval

  • PubMed API integration for latest research
  • Wikipedia fallback for additional context
  • Vector database semantic search

4. Entity Summarization

  • LLM-generated enhanced summaries
  • Relevance-based confidence updates
  • Context integration for better understanding

5. Chain-of-Thought Reasoning

  • Structured reasoning synthesis with evidence integration
  • Graph-based path exploration
  • Confidence propagation through reasoning chains

6. Final Answer Generation

  • Multi-evidence integration for answer selection
  • Confidence scoring and explanation generation

πŸ› οΈ Installation

Prerequisites

  • Python 3.8+
  • OpenAI API key (or Ollama for local inference)
  • PubMed API key (optional, for higher rate limits)

Quick Install

# Clone the repository
git clone https://github.com/MrRezaeiUofT/AMG-RAG.git
cd AMG-RAG

# Install core dependencies
pip install langchain langchain-community langchain-openai
pip install transformers langgraph pandas numpy requests wikipedia networkx python-decouple

# Optional: Install additional packages
pip install langchain-huggingface langchain-chroma langchain-ollama

Environment Setup

Create a .env file in the root directory:

OPENAI_API_KEY=your_openai_api_key_here
pubmed_api=your_pubmed_api_key_here

πŸš€ Quick Start

Basic Usage

from AMG_with_KG import AMG_RAG_System

# Initialize the system
system = AMG_RAG_System(use_openai=True, openai_key="your-api-key")

# Sample medical question
question_data = {
    "question": "A 45-year-old man presents with severe chest pain...",
    "options": {
        "A": "Unstable angina",
        "B": "Acute inferior wall myocardial infarction",
        "C": "Acute anterior wall myocardial infarction",
        "D": "Aortic dissection",
        "E": "Pulmonary embolism"
    },
    "answer": "B"
}

# Get answer with reasoning
result = system.answer_question(question_data)

print(f"Answer: {result['answer']}")
print(f"Confidence: {result['confidence']:.2f}")
print(f"Explanation: {result['explanation']}")

Knowledge Graph Exploration

# Access the knowledge graph
kg = system.kg

# Explore entities
for entity_name, entity in kg.entities.items():
    print(f"Entity: {entity_name}")
    print(f"Type: {entity.entity_type}")
    print(f"Confidence: {entity.confidence:.2f}")
    print(f"Description: {entity.description[:100]}...")

# Explore relationships
for relation in kg.relations:
    print(f"{relation.source} --[{relation.relation_type}]--> {relation.target}")
    print(f"Confidence: {relation.confidence:.2f}")

πŸ“Š Data Format

The system expects input data in JSONL format:

{
  "question": "Medical question text",
  "options": {
    "A": "Option A text",
    "B": "Option B text", 
    "C": "Option C text",
    "D": "Option D text"
  },
  "answer": "B",
  "answer_idx": 1,
  "meta_info": "Additional metadata"
}

βš™οΈ Configuration

Model Selection

# OpenAI (recommended)
system = AMG_RAG_System(use_openai=True, openai_key="your-api-key")

# Local Ollama
system = AMG_RAG_System(use_openai=False)

Knowledge Graph Parameters

# Entity extraction settings
max_entities = 8              # Maximum entities per question
relevance_threshold = 5       # Minimum relevance score (1-10)

# Relationship analysis settings
confidence_threshold = 0.3    # Minimum confidence for relationships
max_relationship_depth = 2    # Maximum exploration depth

# Search parameters
pubmed_max_results = 3        # Max PubMed articles
wikipedia_sentences = 3       # Wikipedia summary length

πŸ“ Project Structure

AMG-RAG/
β”œβ”€β”€ AMG-with-KG.py          # Main AMG-RAG system
β”œβ”€β”€ Simple_AMG_RAG.py       # Simplified version
β”œβ”€β”€ create_VDB.py           # Vector database utilities
β”œβ”€β”€ dataset/                # Input datasets
β”‚   β”œβ”€β”€ MEDQA/             # MEDQA dataset
β”‚   β”œβ”€β”€ MedMCQA/           # MedMCQA dataset
β”‚   └── PubMedQA/          # PubMedQA dataset
β”œβ”€β”€ results/                # Output results
β”œβ”€β”€ new_VDB/               # Vector database storage
β”œβ”€β”€ requirements.txt        # Dependencies
β”œβ”€β”€ .env                   # Environment variables
└── README.md             # This file

πŸ†• What's New in v2.0

  • 🎯 Relevance Scoring: 1-10 scale for entity importance
  • πŸ”„ Bidirectional Relationships: Comprehensive Aβ†’B and Bβ†’A analysis
  • 🌐 Context Integration: PubMed and Wikipedia context for better understanding
  • πŸ“ Entity Summarization: LLM-generated enhanced descriptions

πŸ“„ Citation

If you use AMG-RAG in your research, please cite our paper:

@misc{rezaei2025agenticmedicalknowledgegraphs,
      title={Agentic Medical Knowledge Graphs Enhance Medical Question Answering: Bridging the Gap Between LLMs and Evolving Medical Knowledge}, 
      author={Mohammad Reza Rezaei and Reza Saadati Fard and Jayson L. Parker and Rahul G. Krishnan and Milad Lankarany},
      year={2025},
      eprint={2502.13010},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2502.13010}, 
}

πŸ“œ License

This project is licensed under the Apache-2.0 License - see the LICENSE file for details.

πŸ™ Acknowledgments

πŸ†˜ Support

For questions, issues, or support:

  1. Check the Issues page
  2. Create a new issue with detailed information
  3. Contact the maintainers

⚠️ Note: This is research software. Please validate results thoroughly before any clinical application.

About

AMG-RAG (Agentic Medical Graph-RAG) is a comprehensive framework that automates the construction and continuous updating of Medical Knowledge Graphs (MKGs), integrates reasoning, and retrieves current external evidence for medical Question Answering (QA).

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages