Skip to content

yanmingyu92/ragql-nav

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

RagQL-Nav: Multi-Agent Clinical Data Query Framework

License: MIT Python 3.8+ PostgreSQL Neo4j

RagQL-Nav (Retrieval Augmented Generation with Query Language Navigation) is a multi-agent framework that bridges the gap between natural language queries and complex clinical trial data analysis. It intelligently routes queries between SQL and Neo4j databases, provides dual-system validation, and generates transparent decision trees for regulatory compliance.

🎯 Key Features

  • Intelligent Query Routing: Automatically determines whether SQL or Neo4j is optimal for each query component
  • Dual-System Validation: Cross-validates results between database systems for enhanced accuracy
  • Decision Tree Visualization: Provides transparent audit trails for regulatory compliance
  • ADaM Standards Compliance: Built specifically for Clinical Data Interchange Standards Consortium (CDISC) ADaM datasets
  • Multi-Agent Architecture: Leverages LangGraph for orchestrated query processing
  • Performance Optimization: Includes caching, parallelization, and selective validation

πŸ“Š Performance Highlights

Based on experimental evaluation with synthetic clinical trial data:

  • 91% accuracy on complex queries (12% improvement over single-system approaches)
  • Automatic resolution of 85% of discrepancies between systems
  • Transparent audit trails with decision tree documentation
  • Optimized performance for complex multi-domain queries

πŸ—οΈ Architecture Overview

Natural Language Query
         ↓
   Query Decomposition
         ↓
   Intelligent Router ──→ SQL Database (PostgreSQL)
         ↓                      ↓
   Dual Validation ←──── Neo4j Database
         ↓
   Decision Tree Generator
         ↓
   Final Result + Audit Trail

πŸ“ Project Structure

ragql_nav_project/
β”œβ”€β”€ ragql_nav/                 # Main application package
β”‚   β”œβ”€β”€ core/                  # Core RagQL-Nav components
β”‚   β”‚   β”œβ”€β”€ decomposer.py      # Query decomposition engine
β”‚   β”‚   β”œβ”€β”€ router.py          # Intelligent query router
β”‚   β”‚   β”œβ”€β”€ validator.py       # Dual-query validation
β”‚   β”‚   └── decision_tree.py   # Decision tree generator
β”‚   β”œβ”€β”€ database/              # Database management
β”‚   β”‚   β”œβ”€β”€ connections.py     # DB connection managers
β”‚   β”‚   β”œβ”€β”€ schema_manager.py  # Schema setup and management
β”‚   β”‚   └── data_loader.py     # Data loading utilities
β”‚   β”œβ”€β”€ agents/                # Agent clients
β”‚   β”œβ”€β”€ graph/                 # LangGraph workflow
β”‚   └── __main__.py           # CLI entry point
β”œβ”€β”€ servers/                   # Agent servers
β”œβ”€β”€ data/                      # Synthetic datasets
β”œβ”€β”€ evaluation/                # Evaluation framework
β”œβ”€β”€ config/                    # Configuration
└── docs/                      # Documentation

πŸš€ Quick Start

Prerequisites

  • Python 3.8+
  • PostgreSQL 13+
  • Neo4j 5.0+
  • DeepSeek API key (or compatible LLM provider)

Installation

  1. Clone the repository:

    git clone https://github.com/yanmingyu92/ragql-nav.git
    cd ragql-nav
  2. Run the setup script:

    python setup.py

    This will:

    • Check system dependencies
    • Create a virtual environment
    • Install Python packages
    • Set up configuration files
    • Create necessary directories
  3. Configure environment:

    cp .env.example .env
    # Edit .env with your actual configuration
  4. Set up databases:

    # Start PostgreSQL and Neo4j services
    # Then run:
    python -m ragql_nav.database.setup

Basic Usage

# Activate virtual environment
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Run a query
python -m ragql_nav "What is the incidence of severe neutropenia in patients who received the study drug for at least 14 days?"

# Run with checkpointing enabled
python -m ragql_nav "Calculate the mean change from baseline for hemoglobin in the active treatment group" --checkpoint

# Specify a custom thread ID
python -m ragql_nav "List all Grade 3 or higher adverse events" --thread-id my-analysis-001

πŸ“‹ Example Queries

RagQL-Nav supports various types of clinical data queries:

Simple Queries

python -m ragql_nav "How many subjects were randomized to the placebo arm?"
python -m ragql_nav "What is the distribution of subjects by sex?"

Moderate Complexity

python -m ragql_nav "What is the average age of subjects who experienced a serious adverse event?"
python -m ragql_nav "Calculate the mean change from baseline for hemoglobin in the active treatment group"

Complex Queries

python -m ragql_nav "Identify subjects who experienced Grade 2+ neutropenia followed by a fever within 7 days while actively receiving the study drug"
python -m ragql_nav "What is the incidence rate of major adverse cardiac events adjusted for total exposure time in each treatment arm?"

πŸ”§ Configuration

Environment Variables

Key configuration options in .env:

# LLM Configuration
GRAPHRAG_API_KEY=your_deepseek_api_key
BASE_URL=https://api.deepseek.com
LLM_MODEL_NAME=deepseek-chat

# Database Configuration
DB_HOST=localhost
DB_USER=ragql_user
DB_PASSWORD=your_password
DB_NAME=ragql_nav_clinical

NEO4J_URI=bolt://localhost:7687
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=your_neo4j_password

# Performance Settings
MAX_PARALLEL_QUERIES=4
QUERY_TIMEOUT_SECONDS=300
ENABLE_QUERY_CACHE=true

Router Configuration

The intelligent router uses weighted features to determine optimal database selection:

  • SQL Weights: [0.40, 0.10, 0.35, 0.05, 0.10] (tabular, relationship, aggregation, path, hierarchical)
  • Neo4j Weights: [0.15, 0.35, 0.10, 0.25, 0.15]

These can be adjusted in config/settings.py based on your specific use case.

πŸ“Š Evaluation Framework

RagQL-Nav includes a comprehensive evaluation framework with:

  • 150 test queries across three complexity levels
  • Synthetic ADaM datasets for reproducible testing
  • Multiple evaluation metrics: accuracy, system agreement, execution time
  • Automated benchmarking against baseline approaches

Run the evaluation:

python -m ragql_nav.evaluation.run_benchmark

πŸ—ƒοΈ Database Schemas

PostgreSQL Schema (ADaM Compliant)

  • ADSL: Subject-Level Analysis Dataset
  • ADAE: Adverse Events Analysis Dataset
  • ADCM: Concomitant Medications Analysis Dataset
  • ADEG: ECG Analysis Dataset
  • ADLB: Laboratory Test Results Analysis Dataset
  • ADVS: Vital Signs Analysis Dataset
  • ADTTE: Time-to-Event Analysis Dataset
  • ADEFF: Efficacy Analysis Dataset

Neo4j Graph Schema

  • Nodes: :Subject, :AdverseEvent, :LabTest, :Medication, :Treatment
  • Relationships: :EXPERIENCED, :HAS_LAB_RESULT, :TOOK_MEDICATION, :FOLLOWS
  • Temporal relationships for sequence analysis
  • Hierarchical structures for medical coding systems

πŸ” Decision Tree Visualization

Every query generates a decision tree showing:

  • Original query decomposition
  • Routing decisions and rationale
  • Execution results from each system
  • Validation outcomes
  • Final result selection

Example output files:

  • decision_tree_[thread_id].json: Structured decision data
  • decision_tree_[thread_id].md: Mermaid diagram for visualization

πŸš€ Advanced Usage

Custom Query Templates

Add custom SQL/Cypher templates in ragql_nav/templates/:

# SQL template example
ADVERSE_EVENT_INCIDENCE = """
SELECT
    arm,
    COUNT(DISTINCT usubjid) as subjects_with_ae,
    COUNT(DISTINCT usubjid) * 100.0 / (
        SELECT COUNT(DISTINCT usubjid)
        FROM adsl
        WHERE arm = ae.arm
    ) as incidence_rate
FROM adae ae
WHERE aesev = %(severity)s
GROUP BY arm;
"""

Performance Optimization

Enable performance features:

# Enable caching
export ENABLE_QUERY_CACHE=true

# Increase parallel execution
export MAX_PARALLEL_QUERIES=8

# Adjust validation thresholds
export VALIDATOR_NUMERIC_THRESHOLD=0.01

Audit Logging

Enable comprehensive audit logging:

export ENABLE_AUDIT_LOG=true
export AUDIT_LOG_FILE=ragql_nav_audit.log

πŸ§ͺ Development

Running Tests

# Install development dependencies
pip install -r requirements-dev.txt

# Run unit tests
pytest tests/

# Run integration tests
pytest tests/integration/

# Run evaluation benchmarks
python -m ragql_nav.evaluation.run_benchmark

Code Quality

# Format code
black ragql_nav/

# Lint code
flake8 ragql_nav/

# Type checking
mypy ragql_nav/

Adding New Features

  1. Query Templates: Add to ragql_nav/templates/
  2. Router Features: Extend ragql_nav/core/router.py
  3. Validation Logic: Modify ragql_nav/core/validator.py
  4. Database Schemas: Update ragql_nav/database/schema_manager.py

πŸ“š Documentation

🀝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

# Clone the repository
git clone https://github.com/yanmingyu92/ragql-nav.git
cd ragql-nav

# Create development environment
python -m venv venv-dev
source venv-dev/bin/activate

# Install development dependencies
pip install -r requirements-dev.txt
pip install -e .

# Run tests
pytest

πŸ“„ License

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

πŸ“– Citation

If you use RagQL-Nav in your research, please cite:

@article{ragql_nav_2024,
  title={Comparing SQL and Graph Database Query Methods for Answering Clinical Trial Questions with LLM-Powered Pipelines},
  author={[Author Name]},
  journal={[Journal Name]},
  year={2024},
  url={https://github.com/yanmingyu92/ragql-nav}
}

πŸ™ Acknowledgments

  • CDISC for ADaM standards
  • LangChain and LangGraph communities
  • Neo4j and PostgreSQL communities
  • Clinical research community for domain expertise

πŸ“ž Support

πŸ—ΊοΈ Roadmap

Version 1.1 (Planned)

  • Real-world clinical data validation
  • Additional LLM provider support
  • Enhanced visualization dashboard
  • REST API interface

Version 1.2 (Future)

  • Machine learning-based router optimization
  • Support for additional database systems
  • Advanced statistical analysis templates
  • Regulatory compliance certification

Built with ❀️ for the clinical research community

About

No description or website provided.

Topics

Resources

License

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages