A Retrieval-Augmented Generation (RAG) system with comprehensive safety mechanisms, featuring a Maker-Checker loop for medical research queries.
link to colab : (https://colab.research.google.com/drive/15046CHmXFWNwgNzeipwTWTpg9Hmh-PA2?authuser=1#scrollTo=GX4tFqC48ltg)
This system demonstrates a complete agentic RAG pipeline that answers complex medical queries while ensuring safety, accuracy, and reliability. It uses multiple AI agents working in concert to validate inputs, retrieve relevant information, generate responses, and ensure quality through automated checking and refinement.
User Query β βββββββββββββββββββββββββββ β Input Validation β β Sanitize & Safety Check βββββββββββββββββββββββββββ β βββββββββββββββββββββββββββ β Document Retrieval β β Vector Search (ChromaDB) βββββββββββββββββββββββββββ β βββββββββββββββββββββββββββ β MAKER Agent β β Generate Response (Groq API) βββββββββββββββββββββββββββ β βββββββββββββββββββββββββββ β CHECKER Agent β β Validate Safety & Quality βββββββββββββββββββββββββββ β βββββββββββββββββββββββββββ β REFINER Agent β β Improve if Needed βββββββββββββββββββββββββββ β Final Response
- Input Validation: Detects and blocks malicious patterns (XSS, SQL injection, path traversal)
- Harmful Content Filtering: Prevents responses to dangerous queries
- Crisis Detection: Routes users to mental health resources when needed
- Medical Disclaimers: Automatically enforces professional medical advice disclaimers
- Maker-Checker Loop: Dual-agent validation with automatic refinement
- Maker Agent: Generates initial responses using Groq's LLM
- Checker Agent: Performs 6-point safety validation
- Refiner Agent: Improves responses based on checker feedback
- Vector database with semantic search (ChromaDB)
- Sentence transformer embeddings (all-MiniLM-L6-v2)
- Relevance scoring and source attribution
- Multi-document synthesis
- β Medical disclaimer presence
- β No diagnosis language
- β Source citations
- β Appropriate response length
- β No harmful content
- β Reasonable confidence scoring
pip install groq chromadb sentence-transformers numpyrag_system = AgenticRAGSystem()
result = rag_system.process_query("What are treatment options for diabetes?")
print(result['answer']) print(f"Confidence: {result['confidence']}") print(f"Sources: {result['sources']}")
What are treatment options for type 2 diabetes? Based on current medical research: Type 2 diabetes management includes lifestyle modifications (diet and exercise), oral medications (metformin as first-line), GLP-1 receptor agonists, SGLT2 inhibitors, and insulin therapy when needed...
Sources:
- ADA Clinical Practice Guidelines 2024 (Reliability: 95%)
Knowledge Base The system includes 8 medical documents covering:
Diabetes management
Hypertension treatment
COVID-19 information
Antibiotic usage
Cancer screening
Mental health
Nutrition guidelines
Vaccination recommendations
To add your own documents:
MEDICAL_KNOWLEDGE.append({
"id": "custom_001",
"content": "Your medical content here...",
"topic": "topic_name",
"source": "Source Name 2024",
"reliability": 0.95
})
π Use Cases
Medical Education: Help students learn about medical conditions
Patient Information: Provide general health information
Research Assistant: Quick access to medical guidelines
Health Literacy: Improve understanding of medical topics
Triage Support: Initial information gathering (not diagnosis)