Metadata-Version: 2.4
Name: mentalsaathi-ai
Version: 0.2.1
Summary: LangChain-based AI engine for mental health support conversations
Author-email: Rhydham Mittal <rhydham937@gmail.com>
License: MIT
Requires-Python: >=3.11
Description-Content-Type: text/markdown
Requires-Dist: langchain-core
Requires-Dist: langchain-google-genai
Requires-Dist: python-dotenv
Requires-Dist: pydantic

# 🌿 MentalSaathi AI Engine

The AI backbone of [Mental Saathi](https://www.mentalsaathi.in) — a mental health platform built for Indian college students. This package handles risk detection, LLM chains, prompt management, and agent orchestration.

---

## Features

- **Risk Detection Agent** — analyses user-submitted text and returns a structured risk assessment (`risk_score`, `risk_level`, `summary`)
- **LangChain-powered chains** — modular, composable chains built on `langchain-core`
- **Groq LLM integration** — fast inference via `langchain-groq`
- **Pydantic output schemas** — structured, validated responses from every chain
- **Pluggable prompt system** — prompts and parsers are decoupled and easy to swap

---

## Project Structure

```
mentalsaathi.backend/ai/
├── agent/               # High-level agents (entry points)
│   └── risk_detection.py
├── chains/              # LangChain chains
│   └── calculate_risk.py
├── core/                # App settings & config
│   └── settings.py
├── llms/                # LLM instantiation
│   └── llm.py
├── prompts/             # Prompt templates & parsers
│   └── risk.py
└── schemas/             # Pydantic output schemas
    └── risk.py
```

---

## Installation

Requires Python 3.11+.

```bash
# Clone the repo
git clone https://github.com/your-org/mentalsaathi.git
cd mentalsaathi.backend

# Install the AI package in editable mode
pip install -e ".[dev]"
```

---

## Configuration

Create a `.env` file in `mentalsaathi.backend/`:

```env
GROQ_API_KEY=your_groq_api_key
GROQ_MODEL=llama3-8b-8192
```

All settings are loaded via `ai.core.Settings` (Pydantic `BaseSettings`).

---

## Usage

### Risk Detection

```python
from ai.agent import risk_agent

result = risk_agent.invoke({"user_input": "I've been feeling really hopeless lately..."})

print(result.risk_level)   # e.g. "HIGH"
print(result.risk_score)   # e.g. 78
print(result.summary)      # e.g. "User expresses signs of hopelessness..."
```

### Risk Levels

| Level      | Description                              |
|------------|------------------------------------------|
| `MINIMAL`  | No significant distress detected         |
| `LOW`      | Mild emotional difficulty                |
| `MODERATE` | Noticeable distress, worth monitoring    |
| `HIGH`     | Significant risk, prompt attention needed|
| `CRITICAL` | Immediate intervention recommended       |

---

## Dependencies

Key packages used:

| Package              | Purpose                        |
|----------------------|--------------------------------|
| `langchain-core`     | Chain & prompt abstractions    |
| `langchain-groq`     | Groq LLM integration           |
| `pydantic`           | Output schema validation       |
| `python-dotenv`      | Environment variable loading   |
| `rich`               | Pretty console output          |
| `langsmith`          | Tracing & observability        |

Full list in [`pyproject.toml`](./pyproject.toml).

---

## Development

```bash
# Run a quick smoke test on the risk agent
python -c "from ai.agent import risk_agent; print(risk_agent.invoke({'user_input': 'test'}))"
```

Make sure the package is installed from the repo root so absolute imports (`from ai.x import ...`) resolve correctly.

---

## License

MIT © 2026 Mental Saathi
