RAG System & Infrastructure
Retrieval-Augmented Generation for intelligent patient data queries
Retrieval-Augmented Generation (RAG) is an AI architecture that combines the power of large language models with real-time data retrieval. Instead of relying solely on pre-trained knowledge, RAG systems retrieve relevant information from databases and knowledge bases to provide accurate, context-aware responses.
In MedSense Analytics, our RAG system enables the AI assistant to answer questions about patients, sensor data, anomalies, and system architecture by dynamically retrieving relevant information from the database and project knowledge base.
RAG Architecture
System Components
Analyzes user queries to determine:
- Whether database access is needed
- Patient ID extraction from queries
- Query intent (summary, chart, anomaly, etc.)
- Chart type detection (cough, steps, keywords)
Patterns: "patient 1", "room 101", "show chart"
Retrieves relevant data from database:
- Patient summaries and statistics
- Time-series sensor data
- Anomaly flags and severity
- Medical history (medicines, past diseases)
- Global analytics and trends
SQLite / PostgreSQL
Constructs context for LLM:
- Formats patient data into readable text
- Includes relevant anomalies and analysis
- Adds project knowledge for system questions
- Structures data for optimal LLM understanding
- Patient summary
- Anomaly analysis
- Medical history
- Project knowledge
Generates intelligent responses:
- OpenAI GPT model (configurable)
- System prompts for medical context
- Clinically relevant interpretations
- Chart descriptions and insights
Temperature: 0.7
Max tokens: 600
Knowledge Base
The system maintains a comprehensive knowledge base about:
Hardware & Sensors
- Arduino Nano 33 BLE Sense Rev2 specifications
- Sensor details (BMI270, MP34DT06JTR, HS3003)
- Sensor to metric mappings
- Bluetooth Low Energy (BLE) protocol
ML Models & Detection
- Z-Score anomaly detection algorithm
- Severity levels and thresholds
- Detection pipeline workflow
API & Database
- REST API endpoints
- Database schema (SQLAlchemy)
- Data flow architecture
- Technology stack details
Data Processing
- Time-series aggregation
- Trend analysis methods
- Anomaly flagging logic
Query Processing Workflow
Key Features
Technical Implementation
| Component | Technology | Purpose |
|---|---|---|
| Query Classification | Python Regex, Keyword Matching | Extract intent and parameters from natural language |
| Data Retrieval | SQLAlchemy ORM | Query database for patient data, anomalies, trends |
| LLM Integration | OpenAI API (GPT-3.5/4) | Generate contextual, clinically relevant responses |
| Chart Rendering | Plotly.js | Interactive visualizations in chat interface |
| Markdown Rendering | Custom JavaScript Parser | Format LLM responses with bold, lists, code blocks |
| Context Management | Python Dict, JSON | Structure data for optimal LLM understanding |
Example Queries
- "Show me patient 1's summary"
- "What medicines is patient 2 taking?"
- "Show cough chart for patient 3"
- "What is patient 1's medical history?"
- "Should I be concerned about patient 2?"
- "What sensors are used?"
- "How does anomaly detection work?"
- "Explain the Z-score algorithm"
- "What is the data flow architecture?"
- "Tell me about the Arduino hardware"