← Back to Projects

MedSafe AI Agent

Medsafe Checker helps healthcare professionals quickly identify potential drug interactions and provides referenced safety advice using RAG, Milvus hybrid search, and large language models.

FastAPI Milvus FastAPI BGE Embedding OpenAI 4o BGE reranker

Overview

MedSafe AI Agent is a comprehensive drug interaction checker that leverages large language models and retrieval-augmented generation (RAG) to help healthcare professionals quickly identify potential drug interactions and provide evidence-based safety recommendations. The system combines clinical knowledge bases with AI to deliver accurate, referenced medical advice.      

Key Features

  • Multi-Agent AI System: Implemented 4-stage autonomous reasoning pipeline (Planning → Execution → Reflection → Answer Generation) that dynamically routes queries and self-corrects through reflection. Automatically identifies potential adverse interactions between multiple medications using hybrid search across pharmaceutical databases and clinical literature .
  • Evidence-Based Recommendations: Provides risk assessments with detailed mechanisms, contraindications, and clinical guidance backed by cited medical sources.
  • Multi-language support (Chinese/English): Cross-lingual capabilities for global accessibility .
  • Evidence-Based Citations: Developed automatic citation system linking all recommendations to source documents with numbered references for complete audit trail .
  • Hybrid search combining BM25 and vector similarity: Advanced retrieval combining keyword matching and semantic understanding .
  • BGE reranking for improved relevance: Enhanced result ordering for more accurate information retrieval .

System Architecture


Problem Statement

Healthcare professionals face critical challenges when managing complex medication regimens and need reliable, fast access to drug interaction information.
  • Clinicians need rapid interaction checks across scattered PDFs, local databases, and the public web; manual reviews are slow and incomplete.
  • Existing lookup tools surface raw snippets without reasoning or context-specific risk guidance, increasing the chance of missed contraindications.
  • Hospitals and telehealth services require auditable, up-to-date decision support that merges internal formularies with the latest regulatory alerts while preserving compliance logs.

Core Technical Implementations

1. Hybrid Vector Retrieval System

Keyword search misses semantics; pure vector search misses exact drug names and dosages.
  • Parallel hybrid search - combining dense + sparse vectors
    • BGE-M3 dense (1024-dim): Captures semantic meaning.
    • Sparse (BM25-style): Matches exact medical terminology.
    • Milvus database performs both searches simultaneously
    • Patient population factors
  • RRF reranking:
    • Reciprocal Rank Fusion with weights (0.7 sparse, 1.0 dense).
    • Prioritizes exact drug name matching while capturing semantic relationships.
  • Retrieves top-20  candidates for safety-critical recall
  • 2. AI-Powered Interaction Analysis

    Single-pass retrieval can not handle complex multi-source queries.
  • Four-stage agent pipeline `Planner → Executor → Reflector → AnswerGenerator
  • Planning: GPT-4o analyzes intent, generates JSON execution plan. Decision logic: Local (standard info) | Web (latest research) | Both (complex interactions)
  • Execution: Parallel search across local DB + web, results aggregated in memory.
  • Reflection: Self-evaluation identifies gaps, generates additional queries.
  • Answer Generation: Synthesizes info with risk levels, mechanisms, recommendations.Inserts citations [1][2][3] for every statement.
  • 3. Web Search Integration with Vector Retrieval

    Local databases miss latest clinical research and guidelines.
  • Multi-step pipeline `Serper API → Crawler → Chunking → ChromaDB → Vector Search.
  • Search: Serper API returns top-10 URLs
  • Crawl: BeautifulSoup extracts clean text from medical sites.
  • Chunk: 300-char chunks with 50-char overlap (optimized for medical content).
  • Vectorize: BGE-M3 generates embeddings.
  • Store: ChromaDB temporary in-memory store.
  • Retrieve: Vector similarity search returns top-5 most relevant chunks.

© 2025 Qing Zhong