<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sampathmannam</title>
    <description>The latest articles on DEV Community by sampathmannam (@sampathmannam).</description>
    <link>https://dev.to/sampathmannam</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3992482%2Fc842e203-c680-477e-b0d9-37a7e85ce013.png</url>
      <title>DEV Community: sampathmannam</title>
      <link>https://dev.to/sampathmannam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sampathmannam"/>
    <language>en</language>
    <item>
      <title>Running a 1.5B-Parameter LLM Entirely On-Device for Mental Health — The NilaMind Architecture</title>
      <dc:creator>sampathmannam</dc:creator>
      <pubDate>Tue, 14 Jul 2026 16:26:18 +0000</pubDate>
      <link>https://dev.to/sampathmannam/running-a-15b-parameter-llm-entirely-on-device-for-mental-health-the-nilamind-architecture-15ni</link>
      <guid>https://dev.to/sampathmannam/running-a-15b-parameter-llm-entirely-on-device-for-mental-health-the-nilamind-architecture-15ni</guid>
      <description>&lt;p&gt;&lt;em&gt;How I built a mental health companion that never connects to the internet, and why the most important safety decisions have nothing to do with the AI.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Mental health conversations are the most private conversations you can have. If you're telling something to an AI about suicidal thoughts, trauma, or shame, you need to know — really know — that those words don't go anywhere.&lt;/p&gt;

&lt;p&gt;But every AI companion app I could find sends your words to a cloud server. Your most vulnerable moments, stored on someone else's machine, processed by someone else's model, governed by someone else's privacy policy. That felt wrong.&lt;/p&gt;

&lt;p&gt;So I built &lt;strong&gt;NilaMind&lt;/strong&gt; — an open-source Android app that runs a 1.5B-parameter language model entirely on your phone. No cloud. No account. No analytics. Not even your voice leaves the device.&lt;/p&gt;

&lt;p&gt;Here's how every piece of the architecture works, and why the safety design matters more than the model.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Stack at a Glance
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layer&lt;/th&gt;
&lt;th&gt;Technology&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;UI&lt;/td&gt;
&lt;td&gt;React 19, Tailwind 4, Vite 6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mobile Shell&lt;/td&gt;
&lt;td&gt;Capacitor 8 (Android)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LLM Runtime&lt;/td&gt;
&lt;td&gt;llama.cpp (C++, JNI bridge)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Default Model&lt;/td&gt;
&lt;td&gt;Qwen2.5-1.5B-Instruct Q4_K_M GGUF&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Voice Input&lt;/td&gt;
&lt;td&gt;Vosk WASM (on-device STT)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Crisis Classifier&lt;/td&gt;
&lt;td&gt;MiniLM via ONNX Runtime Web&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Storage&lt;/td&gt;
&lt;td&gt;Dexie (IndexedDB) + AES-256-GCM&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Identity&lt;/td&gt;
&lt;td&gt;BIP39 (no-account recovery phrase)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Running llama.cpp on Android
&lt;/h2&gt;

&lt;p&gt;The inference engine is &lt;a href="https://github.com/ggml-org/llama.cpp" rel="noopener noreferrer"&gt;llama.cpp&lt;/a&gt;, compiled natively for ARM64 via CMake and loaded through a Capacitor plugin bridge.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;llama-cpp-capacitor&lt;/code&gt; plugin packages the full llama.cpp C++ source (~108 files, from &lt;code&gt;ggml.c&lt;/code&gt; to &lt;code&gt;chat.cpp&lt;/code&gt;) and compiles it via Gradle's &lt;code&gt;externalNativeBuild&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="n"&gt;externalNativeBuild&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;cmake&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="s2"&gt;"src/main/CMakeLists.txt"&lt;/span&gt;
        &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="s2"&gt;"3.22.1"&lt;/span&gt;
        &lt;span class="n"&gt;ndkVersion&lt;/span&gt; &lt;span class="s2"&gt;"29.0.13113456"&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The CMakeLists compiles &lt;code&gt;libllama-cpp-arm64.so&lt;/code&gt; with ARMv8-A optimizations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;target_compile_options&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;llama-cpp-arm64 PRIVATE
    -march=armv8-a
    -mtune=cortex-a76
    -O3 -DNDEBUG -fno-finite-math-only -funroll-loops
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The resulting shared library (~6 MB, stripped) is loaded via JNI, with a TypeScript bridge exposing a chat-first API to the React layer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Model Selection
&lt;/h3&gt;

&lt;p&gt;The default model is &lt;strong&gt;Qwen2.5-1.5B-Instruct&lt;/strong&gt;, quantized to Q4_K_M (~1.1 GB). I chose it over the earlier Gemma-3-1B default because it has an Apache-2.0 license (fully F-Droid-compatible), better instruction-following at this size, and no gated access.&lt;/p&gt;

&lt;p&gt;The model is downloaded once on first run with SHA-256 integrity verification. After that, the app works fully offline. It stays resident in memory via an Android foreground service so every reply after the first one is fast.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Safety Architecture (This Is the Part That Actually Matters)
&lt;/h2&gt;

&lt;p&gt;The most important design decision: &lt;strong&gt;the crisis safety layer is deterministic and model-independent.&lt;/strong&gt; The AI is never asked to judge whether someone is in crisis.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why This Matters
&lt;/h3&gt;

&lt;p&gt;Mental health AI has a documented failure mode: large models can be &lt;strong&gt;sycophantic&lt;/strong&gt; — they validate whatever the user says, including dangerous delusions. A manic person declaring "I'm going to invest my life savings in crypto tonight" might get an encouraging "That sounds exciting!" from an unguarded model.&lt;/p&gt;

&lt;p&gt;A 1.5B model is ~15% worse at suicide-risk assessment than specialized tools. Expecting it to handle crisis detection is irresponsible.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Two-Layer Safety Gate
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Layer 1 — Keyword Scanner (deterministic floor):&lt;/strong&gt;&lt;br&gt;
A regex + substring scanner checks every user input and every model output. It covers crisis statements across English, Hindi, Tamil, and Telugu, including negation-first patterns, euphemistic disclosures, and manic risk markers (grandiosity, impulsive spending, medication stopping).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Layer 2 — MiniLM Classifier (additive, soft):&lt;/strong&gt;&lt;br&gt;
A small ONNX model (~90 MB) scores each input on crisis risk. It catches what keywords miss — subtle emotional disclosures, indirect statements. It's additive: if the keyword scanner fires, the classifier output doesn't matter. If only the classifier fires with high confidence, it elevates gently.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Anti-Sycophancy Rule
&lt;/h3&gt;

&lt;p&gt;Rule 6 of the output gate explicitly blocks the model from validating &lt;strong&gt;manic grandiosity, impulsivity, or paranoia&lt;/strong&gt;. If the user says "I'm going to invest my life savings in crypto tonight, I have a special gift for predicting markets," the reply is gate-replaced regardless of what the model generated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Failing Closed
&lt;/h3&gt;

&lt;p&gt;Every model reply passes through the output gate. If any rule fires, the reply is replaced with a safe fallback that redirects to crisis resources. The model never knows any of this happened — the gate is entirely external.&lt;/p&gt;




&lt;h2&gt;
  
  
  On-Device Voice: STT That Never Leaves the Phone
&lt;/h2&gt;

&lt;p&gt;Voice input uses &lt;strong&gt;Vosk&lt;/strong&gt; compiled to WASM. The small English model (Apache 2.0, ~40 MB) runs entirely on-device. Your spoken words are transcribed locally and never sent to a speech recognition API.&lt;/p&gt;

&lt;p&gt;The trade-off: Vosk is less accurate than Google's cloud STT, especially in noisy environments. An optional setting lets users switch to the system recognizer, but the default is on-device.&lt;/p&gt;




&lt;h2&gt;
  
  
  Encrypted Storage: AES-256-GCM
&lt;/h2&gt;

&lt;p&gt;All user data — conversations, mood logs, check-ins, sleep data, safety plans — is stored in IndexedDB and encrypted at rest. The encryption key is either non-extractable (Android Keystore) or PIN-derived (zero-knowledge mode).&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Not Just Use GPT-4?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Privacy is the product.&lt;/strong&gt; If my words go to a server, I've already lost. It doesn't matter what the privacy policy says — can I verify it? With a cloud API, you can't. With an on-device model, the network traffic observable on your phone is the proof.&lt;/p&gt;

&lt;p&gt;The 1.5B model is small, but the app is designed around its limits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The model listens and reflects&lt;/strong&gt; — short, warm, human responses&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The app handles the "what to do"&lt;/strong&gt; — CBT worksheets, DBT skills, grounding exercises — deterministically, not generated&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Safety is never the model's job&lt;/strong&gt; — the deterministic gate owns that&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Test Suite
&lt;/h2&gt;

&lt;p&gt;~2,550 tests across 230+ test files. Every safety feature has paired benign-control tests — a false crisis fire on a calm chat is itself harmful. Coverage includes adversarial keyword tests in 4 languages, anti-sycophancy validation, encrypted storage round-trips, and passthrough hydration.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I'd Do Differently
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Model quality at 1.5B is the bottleneck.&lt;/strong&gt; The model can be repetitive and formulaic. The next generation of 1.5B-3B models trained for emotional conversation will be a game-changer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CMake-based Android builds are finicky.&lt;/strong&gt; Every NDK update breaks something in the 108-file llama.cpp source tree.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Vosk accuracy varies across accents and background noise.&lt;/strong&gt; This is the price of 100% on-device voice.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Testing crisis safety is inherently limited.&lt;/strong&gt; You can test exhaustively in code, but you can't ethically run a clinical trial as a solo developer. The safety layer is designed to fail-closed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Get It
&lt;/h2&gt;

&lt;p&gt;NilaMind is free and open source (Apache 2.0).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/sampathmannam/nilamind" rel="noopener noreferrer"&gt;github.com/sampathmannam/nilamind&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;APK:&lt;/strong&gt; &lt;a href="https://github.com/sampathmannam/nilamind/releases" rel="noopener noreferrer"&gt;GitHub Releases&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Auto-updates:&lt;/strong&gt; Install via &lt;a href="https://obtainium.imranr.dev" rel="noopener noreferrer"&gt;Obtainium&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; NilaMind is not a medical device, not therapy, and not a crisis service. It's an experimental self-help tool. If you are in crisis, contact emergency services or a crisis line.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;It's shared openly so others can learn from it and build on it. If you're struggling, you're not alone.*&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>llamacpp</category>
      <category>opensource</category>
      <category>android</category>
    </item>
  </channel>
</rss>
