LangChain-based AI engine for mental health support conversations
Project description
MentalSaathi AI
The AI engine powering MentalSaathi — an emotional support platform for Indian college students.
Handles the full conversation pipeline: emotion analysis, crisis detection, and human-sounding response generation — in Hinglish or English, whichever the student writes in.
How it works
Three LLM calls per message, in sequence:
Student message
│
▼
Stage 1 — FullAnalysisChain
Detects emotion, intensity, surface vs deep pain, root cause
│
▼
Stage 2 — CrisisDetectionChain
Safety gate: is this student in crisis right now?
├─ YES → CrisisCheck (crisis response + helplines)
└─ NO ──────────────────────────────────────────┐
▼
Stage 3 — ResponseGenerationChain
Warm, specific reply + grounding anchor
│
▼
ReliefResponse
Installation
pip install mentalsaathi-ai
Requires Python 3.11+. Set your Gemini API key before using:
# .env
GOOGLE_API_KEY=your_google_api_key
GEMINI_MODEL=gemini-2.5-flash-lite # optional, this is the default
Quickstart
One function
from mentalsaathi_ai import run
result = run("yaar bahut stressed hoon, result bahut bura aaya")
print(result) # the message the student will read
print(result.anchor) # grounding suggestion specific to their situation
With conversation history
result = run(
student_message="haa koi solution hai kya?",
conversation_context="Student: result bahut bura aaya\nTalkSaathi: Itni mehnat ke baad...",
)
Pipeline object
from mentalsaathi_ai import mentalsaathi_chain
result = mentalsaathi_chain("I don't see the point of studying anymore")
print(len(mentalsaathi_chain)) # 3 (number of LLM calls)
Return types
from mentalsaathi_ai.schemas import ReliefResponse, CrisisCheck
# Normal path
result.response # str — the message to show the student
result.anchor # str — one small grounding action, specific to their situation
# Crisis path
result.crisis_response # str — warm, direct crisis message
result.resources # list of helplines (iCall, AASRA, Vandrevala, etc.)
# Distinguish them
bool(result) # True → ReliefResponse, False → CrisisCheck
str(result) # student-facing text in both cases
Terminal chat (dev)
python -m mentalsaathi_ai.chains
Colored terminal interface with conversation memory. Type clear to reset history, quit to exit.
Package structure
mentalsaathi_ai/
├── __init__.py # top-level API: run(), mentalsaathi_chain
├── core/settings.py # env var config (GEMINI_MODEL, GOOGLE_API_KEY)
├── llms/llm.py # LLMFactory + brain singleton
├── schemas/ # Pydantic v2 output schemas
├── prompts/
│ ├── loader.py # loads .txt templates from disk
│ └── templates/ # all prompt templates as .txt files
├── chains/
│ ├── base_chain.py # abstract BaseChain with dunders
│ ├── full_analysis_chain.py
│ ├── crisis_detection_chain.py
│ ├── response_generation_chain.py
│ └── mentalsaathi_chain.py # orchestrates all three
└── agent/risk_detection.py # standalone risk scoring agent
Configuration
| Variable | Default | Description |
|---|---|---|
GOOGLE_API_KEY |
— | Required. Your Gemini API key |
GEMINI_MODEL |
gemini-2.5-flash-lite |
Any Gemini chat model |
Running tests
pip install mentalsaathi-ai pytest
pytest testing/unit testing/chain # 105 tests, no API calls needed
pytest testing/integration # real LLM calls, needs GOOGLE_API_KEY
Dependencies
| Package | Purpose |
|---|---|
langchain-core |
LCEL chain and prompt abstractions |
langchain-google-genai |
Gemini LLM integration |
pydantic |
Structured output validation |
python-dotenv |
.env loading |
License
MIT © 2026 Rhydham Mittal
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mentalsaathi_ai-0.2.2.tar.gz.
File metadata
- Download URL: mentalsaathi_ai-0.2.2.tar.gz
- Upload date:
- Size: 37.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d00c82bea56eb0222edddeab9acec32cbe0507cc408e5c0282806023b2d55e00
|
|
| MD5 |
51a02a55095728b5f80a7e48dddfc50a
|
|
| BLAKE2b-256 |
51848f82a139d95662a5bedfe9e760c1fa6c16a683f9fedf717245539484241d
|
File details
Details for the file mentalsaathi_ai-0.2.2-py3-none-any.whl.
File metadata
- Download URL: mentalsaathi_ai-0.2.2-py3-none-any.whl
- Upload date:
- Size: 59.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a4010c9d37b016aeb92825aa718a5eff25f2d095cd4f3b7e68b6afe2a5bb2348
|
|
| MD5 |
f225e80ae12254cc66faaf4c419b4eba
|
|
| BLAKE2b-256 |
f379a7ea017fd28710be3fb261b7218c332ffcabad7a725ce9588efd4b55fcbf
|