AI Integration (Gemini)
TIP
Interactive Architecture Tour: Open Live Tour (Gemini AI Insights & Notes)
This document details the architecture, translation pipeline, prompt design, and safety guidelines for the Gemini AI subsystem in Scripture Habit.
1. Role & System Objectives
The AI subsystem operates as an encouraging facilitator that bridges multilingual barriers and reinforces personal study habits:
- Tone: Empathetic, supportive, plainspoken, and concise.
- Focus: Centered on daily personal application rather than speculative theological debate.
2. Model Configuration
- Model: Gemini 3.1 Flash-Lite
- Latency Optimization: Configured with minimal thinking levels (
thinkingLevel: "minimal") to optimize turnaround latency for message translations and reflection generation.
3. Translation Caching & Batching
To minimize API cost and latency, the system utilizes two optimization layers:
① MD5 Persistent Cache
Generates MD5 hash keys from OriginalText + TargetLanguage and queries the translation_cache collection. Cached translations resolve in under 50ms.
② Batch Translation Pipeline (/api/ai/translate-batch)
When loading message feeds with multiple foreign entries:
- Parallel Cache Lookup: Queries all message hashes concurrently via
Promise.all(). - Single Structured Dispatch: Aggregates cache-missed items into a single JSON payload for Gemini.
- Batch Persistence: Commits translations directly to respective Firestore message documents in an atomic batch write.
4. Reflection Letters (LetterBox) & Scriptural Persona Architecture
Analyzes recent study notes to generate personalized reflection letters written from the perspective of an AI embodying a figure from the Four Standard Works (Old Testament, New Testament, Book of Mormon, Doctrine & Covenants, Pearl of Great Price):
Pipeline Breakdown
Eligibility Evaluation & Cache Gate
If fewer than 2 new notes have been submitted since the previous generation, the API returns the cached letter to conserve quotas.Hybrid Scriptural Persona Matching
Prioritizes figures directly associated with studied chapters (e.g., 1 Nephi $\rightarrow$ Nephi, D&C 25 $\rightarrow$ Emma Smith). If no direct match exists, the model matches the note's spiritual theme to a relevant scriptural figure (excluding the Savior and living leaders).Church AI Guideline Enforcement
Executes generation against safety-engineered prompt directives, saving outputs tousers/{uid}/lettersbefore UI delivery.
5. Daily Scripture Comment Generation (scripts/generate-ai-daily-comments.ts)
Pre-generates daily reflection commentary across 11 languages for the Come, Follow Me curriculum:
- Dynamic 4 Everyday Lenses: Alternates among Relatable Human Struggles, Breath of Relief & Grace, Raw & Vivid Realness, and Unfiltered Soul to prevent tone fatigue.
- Engineering Principles: 100% English system directives with bilingual few-shot models to ensure high inference quality and natural localized phrasing.
6. Alignment with Church AI Guidelines (General Handbook 38.8.47)
In alignment with General Handbook Section 38.8.47 ("Appropriate Use of Artificial Intelligence"), prompts incorporate strict safety boundaries:
- Respect for Personal Revelation: Disclaimers emphasize that AI does not replace personal revelation from the Holy Ghost or ecclesiastical guidance.
- Transparency: Salutations and signatures explicitly identify the text as AI-generated roleplay.
- Priesthood Boundaries: Refrains from speculating on unrevealed doctrines, pronouncing forgiveness, or assessing worthiness.
- Professional Boundaries: Avoids providing medical, clinical mental health, legal, or financial counsel.
- Reverence for Sacred Matters: Prohibits impersonating Jesus Christ or living authorities, and avoids discussing sacred temple ordinance wording.
- Word of Wisdom Compliance: Strictly forbids references to coffee, tea, alcohol, or tobacco in metaphors, using wholesome habits instead.