RAG-Based Mental Health Chatbot
A modular, contextually-aware chatbot designed to provide supportive and safe mental health interactions. Incorporates a custom PyTorch BiLSTM emotion classifier, few-shot intent routing, a Qdrant hybrid (dense/sparse) vector retrieval pipeline, and LLM synthesis with telemetry and strict safety guardrails.
Problem Statement
Providing digital mental health support requires balancing high-speed responses with rigorous safety, clinical bounding, and empathetic tone. Simple retrieval systems fail to recognize nuanced emotional states, while unconstrained LLMs risk hallucinating medical diagnoses or advice. The system must accurately classify intent, detect emotions, and bound generated responses within safe, pre-validated counselor guidelines.
Solution
Built an end-to-end RAG-based mental health support chatbot. Engineered a multi-stage classification pipeline that routes input queries through language detection, few-shot intent validation, and a custom PyTorch-based emotion classifier. Designed a two-stage hybrid search using Qdrant (dense BGE embeddings + sparse SPLADE features) followed by cross-encoder reranking. The final response synthesizes pre-validated counselor guidance while enforcing safety guardrails.
Architecture
The chatbot uses an orchestrated pipeline where incoming queries are translated, verified, and mapped to emotional states. A retrieval service queries Qdrant to pull relevant context, which is compiled alongside safety system prompts for inference, with Langfuse observing cost and latency.
Key Features
- ▸Multi-stage input routing: language detection, intent filtering, and emotion classification
- ▸Custom PyTorch BiLSTM model using spaCy tokenization for real-time emotion mapping
- ▸Two-stage hybrid vector search (dense BGE + sparse SPLADE) on Qdrant Cloud
- ▸Cross-encoder reranking to ensure semantic context matching
- ▸Hardcoded safety guardrails preventing diagnostic generation and off-topic conversations
- ▸Langfuse observability tracing for cost, token usage, and step-level latency
- ▸Multi-interface execution supporting Gradio UI, FastAPI REST endpoints, and CLI
Challenges
- ⚡Preventing the LLM from stepping into clinical diagnostics. Solved by framing LLM prompts to only summarize counselor advice and output supportive guidance.
- ⚡Achieving high retrieval precision on varied patient queries. Solved by implementing query rewriting to distill search keywords before running hybrid database queries.
- ⚡Integrating separate custom PyTorch models and remote APIs in a low-latency pipeline. Solved by optimizing preprocessing sequences and running classification checks in parallel.
Results & Metrics
Successfully deployed an interactive Gradio interface on Hugging Face Spaces
Established complete observability tracing for generation steps and token costs
Engineered a safe response bounding mechanism that filters out-of-scope inputs
Lessons Learned
- 💡Hybrid search (combining sparse keyword matching and dense semantic search) retrieves significantly more context-aligned counselor responses than single-strategy setups.
- 💡Real-time logging of user sentiment and emotion states provides a structured base for evaluating overall conversation safety.
- 💡Embedding a fast translation step at the boundary allows the core pipeline to run on optimized English-only models while supporting multiple languages.
Case Study Overview
Case Study: RAG-Based Mental Health Support Chatbot
This project implements a clinical assistant designed to provide safe, empathetic, and context-aligned support. The chatbot acts as a first-line interaction model, parsing incoming text, identifying client sentiment, and using retrieved counselor logs to ground LLM-generated responses within safe boundaries.
Technical Architecture & Message Processing Flow
The query flow utilizes a pipeline that executes classification steps before querying the vector store or prompting the generative model:
┌───────────────────────────────────────────────────────────┐
│ User Message Input │
└────────────────────────────┬──────────────────────────────┘
│
▼
┌───────────────────────────────────────────────────────────┐
│ Language Detection & Translation (FastText / Scikit) │
└────────────────────────────┬──────────────────────────────┘
│ (English Text)
▼
┌───────────────────────────────────────────────────────────┐
│ Intent Classifier (Few-shot filter: greeting vs distress) │
└──────────────┬─────────────────────────────┬──────────────┘
│ │
(Out of Scope / Greeting) (Distress Query)
│ │
▼ ▼
┌──────────────────────────────┐ ┌───────────────────────┐
│ Static Response Route │ │ Emotion Classifier │ (PyTorch BiLSTM Sentiment)
└──────────────────────────────┘ └───────────┬───────────┘
│
▼
┌───────────────────────┐
│ Query Rewriter (Groq) │ (Search Distillation)
└───────────┬───────────┘
│
▼
┌───────────────────────┐
│ Qdrant Hybrid Search │ (Dense BGE + Sparse SPLADE)
└───────────┬───────────┘
│ (Retrieved Counselor Advice)
▼
┌───────────────────────┐
│ Cross-Encoder Rerank │ (Semantic Re-ordering)
└───────────┬───────────┘
│
▼
┌───────────────────────┐
│ LLM Response Generator│ (Empathetic Synthesis)
└───────────┬───────────┘
│ (Target Language Translation)
▼
┌───────────────────────────────────────────────────────────┐
│ Empathetic Output Response │
└───────────────────────────────────────────────────────────┘Core Ingestion & Retrieval Methodology
1. Multi-Stage Input Processing
- Language Check: Incoming requests are processed by a hierarchical language detector. If a query is in a non-English script, it is translated to English to ensure database lookup compatibility.
- Emotion Routing: A custom PyTorch Bidirectional LSTM (BiLSTM) model, trained on client conversation logs, categorizes user sentiment into pre-defined emotional profiles. This allows the generator to adapt the tone of its final message.
2. Two-Stage Hybrid Vector Search
- Dense Embeddings: Queries are mapped to dense vectors using
BAAI/bge-base-en-v1.5to capture general semantic intent. - Sparse Embeddings: Sparse embeddings are generated using the
SPLADEmodel (prithivida/Splade_PP_en_v1) to index exact keyword matching and capture technical terms or distinct concepts. - Hybrid Search: Dense and sparse queries are evaluated concurrently against the
Qdrantvector storage database.
3. Cross-Encoder Reranking
- The raw results from the hybrid query are routed through a cross-encoder (
cross-encoder/ms-marco-MiniLM-L-6-v2) to measure the precise semantic compatibility between the user's current situation and the retrieved counselor actions. - The top-scoring items are fed into the LLM system prompt as the sole reference material, enforcing a strict boundary to prevent off-topic generations.
4. Telemetry & Observability
- The entire workflow uses decorators connected to
Langfuse. This enables real-time tracing of processing latency, database response times, LLM output speed, and token cost accumulation, ensuring visibility over production runtimes.
Technologies
Links
Related Projects
Arabic Speech-to-Text & LLM NER Pipeline
Developed an end-to-end Arabic dialect transcription, text preprocessing, and structured Named Entity Recognition (NER) pipeline using Whisper, MARBERT, and LangChain.
Knowva AI Language Platform
An AI-driven language learning platform featuring adaptive proficiency exams, real-time Whisper STT, Gemini-powered content customization, and WAV2Lip lip-sync.