An AI-Powered Conversational Agent for Intelligent Mental Health Triage and Assessment
Miraat is a revolutionary mental health assessment platform that transforms traditional static diagnostic tools into an intelligent, empathetic conversational experience. Instead of overwhelming users with generic questionnaires, Miraat engages them in a dynamic dialogue that adapts in real-time, intelligently narrowing down 13 potential diagnostic paths to deliver precise, personalized assessments.
Traditional mental health screening tools suffer from:
- Poor User Experience: Static, one-size-fits-all questionnaires that feel impersonal and overwhelming
- Low Efficiency: Users must answer dozens of irrelevant questions
- Missed Nuances: No ability to probe deeper based on individual responses
- Privacy Concerns: Inadequate protection of sensitive user data
Miraat represents a complete architectural refurbishment of mental health assessment, introducing:
- AI Conversational Diagnostic Funnel: A multi-turn dialogue system that improves assessment efficiency by an estimated 75% over static forms
- Probabilistic Belief State Engine: Real-time confidence scoring that intelligently updates after every user response
- Grounded LLM Reasoning: Expert-written psychological knowledge base ensures reliable, explainable AI decisions
- Privacy-by-Design Architecture: Multi-layered security with PII redaction, AES-256 encryption, and automated crisis detection
- Secure History & Reporting: Encrypted session storage with on-demand PDF report generation
The core innovation of Miraat is its intelligent conversation flow:
graph TD
A[User shares initial concern] --> B[Priming Conversation 2 turns]
B --> C[Zero-Shot Classification]
C --> D[Top 3 Broad Category Hypotheses]
D --> E[Refinement Loop 3-5 turns]
E --> F{Confidence > 95%?}
F -->|No| G[Generate Differentiating Question]
G --> H[User Answers]
H --> I[Judge LLM Evaluates]
I --> J[Update Belief State]
J --> F
F -->|Yes - Broad Category| K[Start Subcategory Refinement]
K --> L[Repeat Refinement Loop]
L --> M{Subcategory Confidence > 95%?}
M -->|Yes| N[Render Targeted Assessment]
N --> O[Generate Personalized Report]
How It Works:
- Priming Phase: Engages users in 2 empathetic turns to gather rich context
- Initial Triage: Uses a Zero-Shot Classifier (BART-Large-MNLI) to generate top 3 hypotheses from 13 broad categories
- Refinement Loop: AI asks targeted questions (3-5 turns) to differentiate between candidates
- Belief State Updates: Probabilistic confidence scores update after each response
- Funnel Completion: Once a category reaches >95% confidence, moves to targeted assessment
Result: Users only answer questions that are directly relevant to their specific situation, reducing fatigue and improving accuracy.
Instead of rigid classification, Miraat maintains a dynamic "belief state":
# Example Belief State Evolution
Turn 1: {"Anxiety Disorders": 0.45, "Mood Disorders": 0.35, "Trauma Disorders": 0.20}
Turn 2: {"Anxiety Disorders": 0.72, "Mood Disorders": 0.18, "Trauma Disorders": 0.10}
Turn 3: {"Anxiety Disorders": 0.96, "Mood Disorders": 0.03, "Trauma Disorders": 0.01}
β
Threshold reached! β Proceed to subcategory refinementAdvantages:
- Transparent Decision Making: Every confidence score is logged and explainable
- User-Validated Path: The AI must justify its reasoning after each turn
- Adaptive Strategy: Questions are generated based on which candidates need differentiation
- No Premature Conclusions: System waits for high confidence before advancing
Miraat's AI is not a black box. It uses a "Grounded LLM" pattern:
Chain-of-Thought Question Generation:
System Prompt:
"You are given expert descriptions of psychological categories:
- Generalized Anxiety: 'Persistent worry about multiple concerns...'
- Social Anxiety: 'Fear of judgment in social settings...'
Based on the user's last answer, formulate ONE question that will
differentiate these two possibilities."
LLM Reasoning (logged):
"The user mentioned 'trembling around people'. This suggests a situational
trigger (social setting) rather than constant worry. I should ask if this
happens only in social contexts or also when alone."
Generated Question:
"Do you experience these feelings only when you're around others, or also
when you're by yourself?"
Judge LLM Evaluation:
Context:
- Candidates: ["Generalized Anxiety", "Social Anxiety"]
- Question: "Do you feel this way only around others or also when alone?"
- User Answer: "Mostly when I'm in groups, especially at parties."
LLM Verdict:
- Supported Category: "Social Anxiety"
- Confidence: 0.85
- Reasoning: "Answer indicates situational trigger specific to social
contexts, strongly aligning with Social Anxiety description."
Why This Matters:
- Reliability: AI reasoning is constrained by trusted clinical knowledge
- Auditability: Every decision includes human-readable reasoning
- Quality Assurance: Clinicians can review and validate the logic
Security is paramount in mental health applications. Miraat implements multiple layers of protection:
# Automatic redaction using spaCy NER + Regex patterns
Input: "Hi, I'm John Doe. My email is john@example.com. I'm depressed."
Output: "Hi, I'm [PERSON]. My email is [EMAIL]. I'm depressed."Protects:
- Names (PERSON, ORG, GPE, LOC)
- Email addresses
- Phone numbers (US/Indian formats)
- Credit card numbers (with Luhn validation)
# All sensitive data encrypted with AES-256 before database write
from cryptography.fernet import Fernet
# Encrypted fields:
- Conversation history (every turn)
- Final assessment reports
- Session metadataDatabase Schema:
CREATE TABLE test_history (
test_id SERIAL PRIMARY KEY,
user_name VARCHAR(50),
date TIMESTAMP,
encrypted_session_data TEXT, -- AES-256 encrypted JSON
encrypted_final_report TEXT -- AES-256 encrypted narrative
);# Rule-based crisis keyword detection
HIGH_RISK_KEYWORDS = [
"kill myself", "suicide", "suicidal", "end my life",
"want to die", "self-harm", "ending it all"
]
if check_for_crisis(user_input):
# Bypass normal flow β Display crisis support UI with helplines| Broad Category | Subcategories | Clinical Tests |
|---|---|---|
| Mood Disorders | Major Depressive Disorder, Bipolar Disorder | PHQ-9, MDQ |
| Anxiety Disorders | Generalized Anxiety, Social Anxiety | GAD-7, LSAS |
| Trauma Disorders | PTSD | PCL-5 |
| OCD | Obsessive-Compulsive Disorder | Y-BOCS |
| Personality Disorders | Borderline Personality Disorder | MSI-BPD |
| Eating Disorders | Eating Disorders | EAT-26 |
| Substance Use | Alcohol Use, Drug Use | AUDIT, DAST-10 |
| Psychotic Disorders | Schizophrenia | PANSS |
| Neurodevelopmental | Autism, ADHD | ASRS, VADRS |
| Impulse Control | Intermittent Explosive Disorder | IEDS |
| Emotional Well-being | General Well-being | WEMWBS |
| Suicidal Tendencies | Suicidal Ideation | C-SSRS |
After assessment completion, Miraat generates a structured report using LangChain:
Report Structure:
- Empathetic Opening: Validates user's expressed feelings with specific references to their conversation
- Clinical Findings: Explains test name, score, and interpretation in accessible language
- Recommended Next Steps: 2-3 actionable, gentle suggestions tailored to their results
- Professional Disclaimer: Clear guidance on seeking licensed professional help
Example Output:
Opening Summary:
"Thank you for sharing your experience with us. It's clear that you've been
dealing with persistent worry that affects your ability to focus at work and
disrupts your sleep..."
Assessment Findings:
"Your responses to the Generalized Anxiety Disorder 7 (GAD-7) assessment
indicate a score of 16, which falls into the 'Moderately Severe Anxiety'
range. This suggests that your anxiety symptoms are having a significant
impact on your daily functioning..."
Recommended Steps:
"Here are some gentle suggestions: Consider establishing a consistent sleep
routine, as you mentioned difficulty sleeping. You might also explore
mindfulness or relaxation techniques..."
graph TD
subgraph Client_Layer
A[User Browser] --> B[HTML CSS Tailwind UI]
B --> C[HTMX Dynamic Updates]
end
subgraph API_Layer
C --> D[FastAPI Endpoints]
D --> E{Route Handler}
E --> F[UI Conversation Turn]
E --> G[UI Submit Assessment]
E --> H[API v1 History]
end
subgraph Security_Layer
F --> I[PII Redaction spaCy]
I --> J[Safety Checker]
end
subgraph Core_Logic_Layer
J --> K[Conversation Service]
K --> L[Triage Service BART MNLI]
K --> M[LLM Orchestration Gemini]
M --> N[Belief State Engine]
N --> O[Assessment Service]
O --> P[Report Service]
end
subgraph Data_Layer
O --> Q[Encryption Service AES256]
Q --> R[PostgreSQL]
P --> R
end
subgraph AI_ML_Stack
L --> S[HuggingFace Transformers]
M --> T[LangChain]
T --> U[Google Gemini API]
I --> V[spaCy NER]
end
backend/
βββ app/ # Phase 2 - New Architecture
β βββ api/v1/ # API endpoints
β β βββ ui.py # Main conversation & assessment UI endpoints
β β βββ history.py # User history & PDF report endpoints
β βββ logic/ # Business logic orchestration
β β βββ conversation.py # Conversation flow management
β β βββ assessment.py # Assessment scoring logic
β βββ services/ # Core service implementations
β β βββ conversation_service.py # LLM conversation & question generation
β β βββ triage_service.py # Zero-shot classification
β β βββ assessment_service.py # Test delivery & scoring
β β βββ report_service.py # AI report generation
β β βββ security_service.py # AES encryption/decryption
β βββ privacy/
β β βββ redaction.py # PII redaction with spaCy
β βββ safety/
β βββ checker.py # Crisis keyword detection
βββ core_logic/ # Shared utilities & legacy components
β βββ Assessment/ # Test data & mappings
β β βββ mapping.json # 13 categories + clinical tests
β β βββ test_data.json # Full test question banks
β β βββ abbreviation_map.json # Test name mappings
β βββ LLM/
β β βββ llm_endpoint.py # Google Gemini configuration
β βββ ChatBot/ # General support chatbot (legacy)
β βββ Data/
β β βββ database.py # SQLAlchemy models
β β βββ schemas.py # Pydantic schemas
β βββ Accessories/
β βββ logger.py # Logging configuration
β βββ helplines.json # Crisis helpline database
βββ templates/ # Jinja2 HTML templates
β βββ assess.html # Main assessment interface
β βββ chatbot.html # General chatbot
β βββ history.html # User history viewer
β βββ partials/ # HTMX partial templates
β βββ init_chat_interface.html
β βββ chat_interface.html
β βββ assessment_workspace.html
β βββ results_workspace.html
βββ static/ # CSS/JS/Images
βββ main.py # FastAPI application entry point
βββ pyproject.toml # Dependencies
- Framework: FastAPI 0.115.5 (async API framework)
- Language: Python 3.10+
- Database: PostgreSQL 15 with SQLAlchemy ORM
- Authentication: JWT tokens with bcrypt password hashing
- LLM Orchestration: LangChain 0.3.9
- LLM Provider: Google Gemini 2.5 Flash (via
langchain_google_genai) - Zero-Shot Classification: HuggingFace Transformers (facebook/bart-large-mnli)
- Embeddings: Sentence-Transformers 3.3.1
- Deep Learning: PyTorch 2.5.1
- NER for PII: spaCy (en_core_web_sm)
- Encryption: Cryptography library (AES-256 Fernet)
- Password Hashing: Passlib with bcrypt
- Token Management: python-jose with cryptography
- Templating: Jinja2 3.1.4
- CSS Framework: TailwindCSS 3.x
- Dynamic Updates: HTMX (for seamless partial page updates)
- JavaScript: Vanilla JS for enhanced interactivity
- Containerization: Docker + Docker Compose
- Web Server: Uvicorn (ASGI server)
- Deployment Ready: Google Cloud Run, Cloud SQL, Secret Manager compatible
- Docker & Docker Compose installed
- 8GB+ RAM recommended (for ML models)
- Clone the repository
git clone https://github.com/vikassahu1/miraat.git
cd miraat- Create environment file
Create a .env file in the root directory:
# Database Configuration
DB_USER=miraat_user
DB_PASSWORD=your_secure_password
DB_NAME=miraat_db
DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}
# API Keys
GOOGLE_API_KEY=your_google_gemini_api_key_here
# Security Keys
SECRET_KEY=your_jwt_secret_key_here
ENCRYPTION_KEY=your_fernet_encryption_key_here
# Application
BASE_URL=http://localhost:8000Generate encryption key:
from cryptography.fernet import Fernet
print(Fernet.generate_key().decode())- Build and run with Docker Compose
docker-compose up --build- Access the application
Main Application: http://localhost:8000
Assessment Tool: http://localhost:8000/ui/
API Docs: http://localhost:8000/docs
- Download required spaCy model (if running locally without Docker)
python -m spacy download en_core_web_sm- Navigate to
http://localhost:8000/ui/ - Share your initial concern in the text box
- The AI will engage you in a brief conversation (2-5 questions)
- Complete the targeted assessment questionnaire
- Receive your personalized report
- Log in to your account at
/login - Navigate to
/history - View all past assessments with encrypted storage
- Download PDF reports of previous sessions
- Go to
/chatbotfor general mental health support - Have open-ended conversations without formal assessment
- Visit
/helplinesfor immediate crisis support numbers - Automatic crisis detection redirects high-risk users
POST /ui/conversation_turn
Content-Type: application/x-www-form-urlencoded
user_input=I've been feeling anxious lately
session_data_json=null
Response: HTML partial with AI questionPOST /ui/submit_assessment
Content-Type: application/x-www-form-urlencoded
session_data_json={...}
username=john_doe
answer_q1=2
answer_q2=3
...
Response: HTML partial with final reportGET /test-history?user_name=john_doe
Response: [
{
"test_id": 1,
"date": "2025-10-27T12:00:00",
"encrypted_session_data": "encrypted_string",
"encrypted_final_report": "encrypted_string"
}
]POST /api/v1/history/download-report
Content-Type: application/json
{
"test_id": 1,
"username": "john_doe"
}
Response: PDF file downloadcd backend
pytest tests/tests/
βββ test_complete_flow.py # End-to-end conversation flow
βββ test_endpoints.py # API endpoint validation
βββ test_conversation_service.py # Conversation logic
βββ test_assessment_direct.py # Assessment scoring
βββ test_subcategory_flow.py # Subcategory refinement
- 75% reduction in average questions asked compared to static forms
- 95% confidence threshold ensures high-quality diagnostic paths
- 3-5 turn average for conversation refinement (vs. 20+ static questions)
- Async FastAPI handles concurrent users efficiently
- LLM caching via LangChain reduces redundant API calls
- PostgreSQL JSONB provides flexible, indexed storage for session data
- AES-256 encryption for all sensitive data at rest
- spaCy NER + Regex achieves >95% PII detection rate
- Automated crisis detection with 0 false negatives on test corpus
- Multi-language support (Spanish, Hindi, French)
- Voice input/output for accessibility
- Real-time sentiment analysis during conversations
- Clinician dashboard for reviewing flagged cases
- Mobile app (React Native)
- Integration with EHR systems (HL7 FHIR)
- Longitudinal tracking (progress over time)
- Group therapy session recommendations
- Additional assessments: Add more clinical tests and diagnostic categories
- Improved redaction: Enhance PII detection patterns
- UI/UX: Improve frontend accessibility and design
- Testing: Expand test coverage and add edge cases
- Documentation: Translate docs or add tutorials
This project is licensed under the MIT License - see the LICENSE file for details.
-
Not a Replacement for Professional Care: Miraat is a screening tool, not a diagnostic instrument. Results should always be reviewed by licensed mental health professionals.
-
Crisis Situations: If you're experiencing a mental health emergency, please:
- Call your local emergency services (911 in US)
- Contact National Suicide Prevention Lifeline: 988 (US)
- Visit your nearest emergency room
-
Data Privacy: While we implement industry-standard security measures, users should be aware that no system is 100% secure. Avoid sharing extremely sensitive information.
-
Research Use: This tool is intended for research and educational purposes. Clinical deployment requires appropriate regulatory approvals (HIPAA, FDA, etc.).
- Google Gemini: For providing accessible LLM capabilities
- HuggingFace: For open-source transformer models
- LangChain Community: For excellent LLM orchestration tools
- spaCy Team: For robust NLP pipelines
Built with β€οΈ by Vikas
Empowering mental health through intelligent, empathetic AI
Website β’ Documentation β’ Demo