Skip to content
Rayyan9477Public

About

"Solace AI: Your Empathetic Digital Confidant": Solace AI is an empathetic mental health companion that understands your emotions and personality to provide personalized support through natural conversations. It creates a judgment-free space where you can express yourself freely and receive compassionate guidance tailored just for you.

Topics

Resources

Stars

22 stars

Watchers

1 watching

Forks

Latest commit

Β 

History

297 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌟 Solace-AI: Advanced Mental Health AI Companion Backend

Python FastAPI LangGraph PostgreSQL Kafka Docker HIPAA Ready License

Solace-AI is an enterprise-grade backend platform designed to provide evidence-based, clinically grounded, and personalized digital mental health support. Built as a distributed microservice system orchestrated via LangGraph, Solace-AI adheres to strict clinical safety guardrails, continuous personality adaptation, multi-tier cognitive memory, and zero-trust PHI privacy standards.


πŸ“‹ Table of Contents


🌟 Core Clinical Capabilities

  • Evidence-Based Interventions: Dynamic therapeutic planning and delivery covering Cognitive Behavioral Therapy (CBT), Dialectical Behavior Therapy (DBT), Acceptance and Commitment Therapy (ACT), Motivational Interviewing (MI), and Mindfulness. Implements Stepped Care (Levels 0–4) tailored to validated screener outcomes (PHQ-9, GAD-7).
  • AMIE-Inspired Diagnostic Reasoning: A 4-step Chain-of-Reasoning (Analyze $\rightarrow$ Hypothesize $\rightarrow$ Challenge $\rightarrow$ Synthesize) referencing DSM-5-TR and HiTOP dimensional models. Employs a dedicated Devil's Advocate agent to actively counter sycophancy and diagnostic confirmation bias.
  • Personality & Empathy Adaptation: Real-time continuous Big Five (OCEAN) trait scoring combined with a Mixture of Empathetic Listeners (MoEL across 32 emotion states) to dynamically align communication style ($r > 0.85$).
  • 5-Tier Cognitive Memory Hierarchy: Seamless transition from working memory to long-term semantic context (Input Buffer $\rightarrow$ Working Memory [Redis] $\rightarrow$ Session Memory [Redis + PostgreSQL] $\rightarrow$ Episodic Memory [PostgreSQL + Weaviate] $\rightarrow$ Semantic Memory). Memory follows Ebbinghaus decay with a critical safety override: crisis disclosures and risk factors never decay.
  • Always-Active 4-Layer Crisis Shield: Continuous risk screening mapped to the Columbia-Suicide Severity Rating Scale (C-SSRS). High and critical disclosures immediately provide verified local crisis resources (e.g., 988 Lifeline) bypassing generative LLM latency, alert on-call clinicians via Amazon SES, and commit durable records to a transactional outbox.

πŸ—οΈ System Architecture

Solace-AI is structured into 10 decoupled FastAPI microservices communicating via asynchronous Kafka event streams and REST/WebSocket APIs, with edge TLS terminated by Caddy.

flowchart TD
    Client["Client Applications<br/>(Web / Mobile Frontend)"]

    subgraph Edge ["Edge Layer (Ports 80 / 443)"]
        Caddy["Caddy Reverse Proxy<br/>(TLS Termination, Path Routing)"]
    end

    subgraph Services ["Microservices Layer"]
        Orchestrator["orchestrator_service<br/>(LangGraph 1.0 Orchestration)"]
        Safety["safety_service<br/>(4-Layer Crisis Shield)"]
        Therapy["therapy_service<br/>(CBT/DBT/ACT Stepped Care)"]
        Diagnosis["diagnosis_service<br/>(AMIE 4-Step Clinical Chain)"]
        Personality["personality_service<br/>(Big Five OCEAN & MoEL)"]
        Memory["memory_service<br/>(5-Tier Cognitive Memory)"]
        User["user-service<br/>(Auth, MFA, Consent, GDPR)"]
        Notification["notification-service<br/>(Clinician SES Paging)"]
        Analytics["analytics-service<br/>(Clinical Outcomes Tracking)"]
    end

    subgraph DataPlane ["Data & Event Plane"]
        Postgres[("PostgreSQL 16<br/>(FORCE Row-Level Security)")]
        Redis[("Redis 7<br/>(Session Cache & Rate Limiting)")]
        Kafka[("Apache Kafka<br/>(Transactional Outbox & Events)")]
        Weaviate[("Weaviate<br/>(Vector & Hybrid Search)")]
    end

    Client --> Caddy
    Caddy --> Orchestrator
    Caddy --> User

    Orchestrator <--> Safety
    Orchestrator <--> Therapy
    Orchestrator <--> Diagnosis
    Orchestrator <--> Personality
    Orchestrator <--> Memory

    Services <--> Postgres
    Services <--> Redis
    Services <--> Kafka
    Memory <--> Weaviate
    Notification --> Kafka
Loading

πŸ“‚ Project Structure

.
β”œβ”€β”€ services/                        # FastAPI Microservices
β”‚   β”œβ”€β”€ orchestrator_service/        # Multi-agent LangGraph workflow, REST & WS APIs
β”‚   β”œβ”€β”€ safety_service/              # Crisis keyword matching, guardrails, contraindications
β”‚   β”œβ”€β”€ therapy_service/             # Clinical unit selection, intervention planning
β”‚   β”œβ”€β”€ diagnosis_service/           # Symptom extraction, differential assessment
β”‚   β”œβ”€β”€ memory_service/              # Vector and episodic cognitive retrieval
β”‚   β”œβ”€β”€ personality_service/         # OCEAN trait detection and style adaptation
β”‚   β”œβ”€β”€ user-service/                # User accounts, OAuth (Google PKCE), JWT, MFA, GDPR erasure
β”‚   β”œβ”€β”€ notification-service/        # Clinician paging, alert delivery, SES notifications
β”‚   β”œβ”€β”€ analytics-service/           # Outcome measures tracking (PHQ-9, GAD-7, SRS)
β”‚   β”œβ”€β”€ config_service/              # (Frozen under D-19)
β”‚   └── shared/                      # Service-level shared modules
β”œβ”€β”€ src/                             # Shared Foundation Packages
β”‚   β”œβ”€β”€ solace_common/               # Core data models, schemas, shared interfaces
β”‚   β”œβ”€β”€ solace_events/               # Kafka event schemas, transactional outbox contracts
β”‚   β”œβ”€β”€ solace_infrastructure/       # IF-01 service runtime, ConnectionPoolManager, health probes
β”‚   β”œβ”€β”€ solace_security/             # IF-04 Service tokens, AES-256-GCM PHI codec, RLS context
β”‚   └── solace_testing/              # Test fixtures, mock providers, testcontainers harness
β”œβ”€β”€ deploy/                          # Deployment Configuration
β”‚   β”œβ”€β”€ env/                         # env-contract.yaml and strict environment schema readers
β”‚   └── secrets/                     # SOPS + age encrypted secret templates
β”œβ”€β”€ docker/                          # Base Docker files and database migration entrypoint
β”œβ”€β”€ infrastructure/                  # Caddy reverse proxy, Prometheus, Grafana, init-db
β”œβ”€β”€ migrations/                      # Alembic database migration chain (Slots 001–021)
β”œβ”€β”€ scripts/                         # Maintenance, test runners, and DB tooling
β”‚   β”œβ”€β”€ ci/                          # CI suite runner, image builder, clean checkout, secret scan
β”‚   └── db/                          # Database migration runner and TLS certificate generator
β”œβ”€β”€ system-design/                   # Detailed architectural specifications and diagrams
β”œβ”€β”€ docs/                            # Documentation hub, API handoff, and MVP remediation plans
└── tests/                           # Complete test suite (unit, realinfra, security, e2e)

πŸ”§ Technology Stack

Layer Component Description
Runtime & Framework Python 3.12, FastAPI, Uvicorn Async microservices with Pydantic v2 validation.
Agent Orchestration LangGraph 1.0, PostgreSQL Saver Directed cyclic graph coordinating clinical and safety agents.
Relational Storage PostgreSQL 16 Enforced Row-Level Security (FORCE RLS) via unprivileged solace_app role.
Event Streaming Apache Kafka Event-driven microservice integration with a transactional outbox.
Cache & Ephemeral Data Redis 7 Sub-10ms session working memory, token revocation, rate limiting.
Vector Database Weaviate Hybrid BM25 and semantic vector search for long-term memory.
Edge & Proxy Caddy v2 Automatic HTTPS, TLS termination, internal port isolation.
Secrets Management SOPS + age (D-07) Version-controlled, zero-plaintext encrypted configuration.
Package Management uv (Hash-Pinned) Deterministic lockfiles (requirements.lock, requirements-dev.lock).

πŸš€ Getting Started & Local Development

1. Prerequisites

  • Python 3.12
  • Docker Engine & Docker Compose v2 (with at least 8 GB RAM allocated)
  • uv package manager:
    pip install "uv==0.8.2"

2. Environment Setup

Create a local Python virtual environment and synchronize dependencies from the hash-pinned lockfile:

python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\Activate.ps1
uv pip sync --require-hashes requirements-dev.lock

Copy the development environment template:

cp .env.example .env

3. Start Data Plane & Services

Launch the backing infrastructure (Postgres, Redis, Kafka, Weaviate, and Caddy) with Docker Compose:

docker compose up -d

Apply database migrations:

make migrate
# or: python -m scripts.db.migrate upgrade head

Verify service health:

curl http://localhost:8000/health  # Orchestrator
curl http://localhost:8001/health  # User Service
curl http://localhost:8002/health  # Safety Service

πŸ§ͺ Testing & Quality Assurance

Solace-AI enforces strict Test-Driven Development (TDD). All suites run under process-level isolation to prevent cross-test contamination.

# Run unit test suites
make test-unit

# Run real-infrastructure tests (Postgres, Kafka, Weaviate testcontainers)
make test-realinfra

# Run on-demand End-to-End suite
make test-e2e PROFILE=dev

# Run repository secret scan (SOPS + age hygiene)
python scripts/ci/secret_scan.py

# Run critical lint check
make lint

πŸ”„ CI/CD Pipelines

The repository features comprehensive automation powered by GitHub Actions:

  • Continuous Integration (.github/workflows/ci.yml):

    • Dynamic Matrix Planning: Discovers all 28+ test suites and 10 container images dynamically.
    • Process-Isolated Execution: Runs unit tests, real-infrastructure suites, and syntax checks concurrently.
    • In-Container Module Probing: Builds images from repository root context and verifies startup and import resolution inside the container.
    • Fast Caching & Path Filtering: Leverages actions/cache@v4 on ~/.cache/uv and skips runner dispatch on docs-only changes.
    • On-Demand E2E: End-to-end tests run on manual dispatch across dev or strict profiles.
  • Continuous Delivery (.github/workflows/cd.yml):

    • Triggers on release tags (v*.*.*) or manual deployment workflow dispatch.
    • Builds and publishes container images to GitHub Container Registry (ghcr.io) tagged with semantic release versions, commit SHAs, and latest.

πŸ›‘οΈ Security, Privacy & Compliance

  • Zero-Trust PHI Protection: Personal Health Information (PHI) is encrypted at rest using AES-256-GCM via phi_codec.py with user ID as Additional Authenticated Data (AAD).
  • PostgreSQL FORCE RLS: All application queries connect as solace_app with Row-Level Security enforced. No user can read or modify another user's records. Work operates strictly within acting_as_user(user_id).
  • Inter-Service Authentication (IF-04): Microservices communicate using short-lived cryptographically signed service tokens (ServiceTokenManager) with on-behalf-of (obo) claims. User credentials never enter LangGraph state.
  • Fail-Closed Strict Environments: In staging or production, missing secrets, loopback database addresses, or unverified TLS connections immediately abort startup.

πŸ“– Documentation Hub


⚠️ Clinical Disclaimer

Solace-AI is designed as an AI-powered conversational support companion and mental health screening aid. It is not an FDA-cleared medical device, nor a substitute for professional clinical diagnosis or medical care. Individuals experiencing acute psychological distress or suicidal ideation are immediately routed to emergency services and crisis helplines (such as 988 Suicide & Crisis Lifeline).

About

"Solace AI: Your Empathetic Digital Confidant": Solace AI is an empathetic mental health companion that understands your emotions and personality to provide personalized support through natural conversations. It creates a judgment-free space where you can express yourself freely and receive compassionate guidance tailored just for you.

Topics

Resources

Stars

22 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages