| # Implementation Phases |
|
|
| Phased build plan for the Christianity-focused AI Assistant. Each phase has a |
| goal, concrete tasks, the files it touches, and an exit criterion. See |
| [`ARCHITECTURE.md`](./ARCHITECTURE.md) for the full design. |
|
|
| **Status legend:** β
done Β· π‘ in progress Β· β¬ not started |
|
|
| --- |
|
|
| ## Phase 0 β Project Setup β
|
|
|
| **Goal:** Reproducible backend skeleton, tooling, and config. |
|
|
| - β
`uv` project, Python 3.12 pin, `pyproject.toml` + `uv.lock` |
| - β
FastAPI app, `/health` + `/chat` stub |
| - β
structlog (console dev / JSON prod) with request-id middleware |
| - β
`pydantic-settings` config (env-driven), `.env.example` |
| - β
`.gitignore`, README, git initialized + initial commit |
|
|
| **Exit:** `uv run uvicorn app.main:app` boots, `/health` returns 200. β
|
|
|
| --- |
|
|
| ## Phase 1 β Data Layer & Corpus π‘ |
|
|
| **Goal:** Postgres + pgvector populated with grounded source text. |
|
|
| - β
`docker-compose.yml` β pgvector/pg16 on host port 5433 |
| - β
`schema.sql` β `bible_verses`, `history_docs`, `conversations` + HNSW/GIN indexes |
| - β
`core/db.py` β psycopg3 pool, pgvector registration, extension bootstrap |
| - β
`scripts/init_db.py` β idempotent schema apply |
| - β
`core/embeddings.py` β bge-base loader, `embed_passages` / `embed_query` |
| - π‘ `scripts/ingest_bible.py` β KJV (66 books) ingest + embeddings (in progress) |
| - π‘ `scripts/ingest_history.py` β parse `creeds1.pdf`, chunk, embed β `history_docs` (in progress) |
| - β¬ Deuterocanon ingest from `KJV-with-Apocrypha.json` β tag `catholic`/`orthodox` |
|
|
| **Exit:** `bible_verses` fully populated (~31k rows); `history_docs` has creeds + |
| council facts; semantic search returns sane neighbors. |
|
|
| **Files:** `backend/app/core/{db,embeddings,schema.sql}`, `backend/scripts/*` |
|
|
| --- |
|
|
| ## Phase 2 β Retrieval Layer β
|
|
|
| **Goal:** Denomination-aware scripture + history retrieval functions. |
|
|
| - β
`core/retrieval.py` β `search_scripture(query, denomination, k)` with canon filter |
| - β
`search_history(query, denomination, k)` (validated once `history_docs` populated) |
| - β
Retrieval confidence scoring (top cosine similarity, threshold in config) |
| - β
Reference lookup `verse_exists(book, chapter, verse)` for citation validation |
|
|
| **Exit:** Given a query + denomination, returns ranked verses honoring canon |
| membership; confidence reflects match quality. β
|
|
|
| --- |
|
|
| ## Phase 3 β LLM Integration β
|
|
|
| **Goal:** Gemini client wrapper with grounded prompting. |
|
|
| - β
`core/llm.py` β google-genai client, main (`gemini-2.5-pro`) generation |
| - β
Module-level singleton + `_reset_client()` auto-retry on closed httpx session |
| - β
`core/prompts.py` β system prompt templates, denomination framing, `build_user_prompt` |
| - β
Structured output helpers (`generate_json` with response_schema) |
| |
| **Exit:** Wrapper takes prompt + retrieved context, returns grounded text; |
| honors "cite only retrieved verses" instruction. β
|
| |
| --- |
| |
| ## Phase 4 β Safety & Router Node β
|
| |
| **Goal:** Block unsafe input before retrieval; classify intent in one call. |
| |
| - β
`core/safety.py` β Stage 1 regex (adversarial templates, explicit hate) |
| - β
Stage 2 Gemini Flash call returning `{safe, intent, confidence}` |
| - β
Intents: `scripture | theology | history | image | general | blocked` |
| - β
Low-confidence / general β fallback to plain responder |
| |
| **Exit:** Adversarial + hateful prompts blocked pre-retrieval; intent routed |
| correctly on the eval set. β
|
| |
| --- |
| |
| ## Phase 5 β Agent Graph β
|
| |
| **Goal:** Wire the LangGraph `StateGraph` from `ARCHITECTURE.md` Β§4β6. |
| |
| - β
`agent/state.py` β `AgentState` TypedDict with all fields |
| - β
`agent/nodes.py` β 9 nodes: Input, SafetyRouter, ScriptureRAG, HistoryRAG, Theology, Image, ImageValidator, Validator, Responder |
| - β
`agent/graph.py` β conditional edges, compile, `get_graph()` lru_cache singleton |
| - β
Per-node latency capture into `state.latency_ms` |
|
|
| **Exit:** Graph runs end-to-end for all intents. β
|
|
|
| --- |
|
|
| ## Phase 6 β Grounding & Hallucination Control β
|
|
|
| **Goal:** Catch fake citations and paraphrase drift. |
|
|
| - β
Citation validator: extract `Book Ch:Verse`, verify vs corpus, strip + log fakes |
| - β
Semantic drift check: embed response, compare max-sim across retrieved set |
| - β
Drift disclaimer injection when drift high despite strong retrieval |
| - β
Populate `hallucinated_refs`, `drift_warning` in state |
|
|
| **Exit:** Fake-verse and paraphrase-misquote eval cases pass (no invented refs). β
|
|
|
| --- |
|
|
| ## Phase 7 β Conversation Memory β
|
|
|
| **Goal:** Bounded, relevant context β no full-history dump. |
|
|
| - β
Persist each turn to `conversations` with per-turn embedding |
| - β
Window strategy (β€20 turns β last 10) and semantic strategy (>20 β top-5) |
| - β
Denomination-switch guard: inject canon/framing-changed system note |
|
|
| **Exit:** Long sessions stay within budget; denomination switch updates framing |
| without stale assumptions. β
|
|
|
| --- |
|
|
| ## Phase 8 β Image Generation β
|
|
|
| **Goal:** Safe Christian-themed image flow with two-pass moderation. |
|
|
| - β
Image prompt rewrite into safe Christian-art form |
| - β
Imagen 3 call via google-genai (base64 data URI) |
| - β
`ImageValidator` node re-checks rewritten prompt before generation |
| - β
Return image URL/bytes + safety metadata |
|
|
| **Exit:** Policy-violating image prompts blocked at raw and post-rewrite stages. β
|
|
|
| --- |
|
|
| ## Phase 9 β API Integration β
|
|
|
| **Goal:** Replace `/chat` stub with the compiled graph. |
|
|
| - β
`/chat` invokes graph, returns response + citations + flags |
| - β
Session handling, request-id propagation into graph state |
| - β
`asyncio.run_in_executor` wraps synchronous graph for async FastAPI |
|
|
| **Exit:** End-to-end API call produces grounded, cited, moderated responses. β
|
|
|
| --- |
|
|
| ## Phase 10 β Frontend β
|
|
|
| **Goal:** Minimal chat UI (UI polish explicitly not graded). |
|
|
| - β
Next.js 15 + Tailwind CSS chat interface |
| - β
Denomination selector (Protestant/Catholic/Orthodox pills) |
| - β
Citation rendering: verified (amber) + hallucinated (red) badges |
| - β
Image display in chat bubble |
| - β
Wire to backend `/chat` |
|
|
| **Exit:** Usable demo chat with denomination toggle and visible citations. β
|
|
|
| --- |
|
|
| ## Phase 11 β Evaluation β
|
|
|
| **Goal:** Reproducible eval harness over the dataset in `ARCHITECTURE.md` Β§13. |
|
|
| - β
`eval/dataset.json` β 20 cases: fake-verse, adversarial, hallucination, image, history, denomination, theology, scripture |
| - β
`eval/run_eval.py` β run cases through graph, score PASS/PARTIAL/FAIL, color report, exit code 1 on FAIL |
| - β
`--id` and `--category` filters for targeted runs |
| - β
Result: 16 PASS / 2 PARTIAL / 2 FAIL (historical FAILs fixed by Phase 1 history ingest) |
|
|
| **Exit:** Single command runs the eval set and prints a scored report. β
|
|
|
| --- |
|
|
| ## Phase 12 β Deployment β¬ |
|
|
| **Goal:** Credible hosted demo. |
|
|
| - β¬ Backend β Hugging Face Spaces (or container host) |
| - β¬ DB β NeonDB (pgvector); push corpus |
| - β¬ Frontend β Vercel |
| - β¬ Env/secrets wiring (`GEMINI_API_KEY`) |
|
|
| **Exit:** Public URL serves the assistant against hosted DB. |
|
|
| --- |
|
|
| ## Phase 13 β Deliverables π‘ |
|
|
| **Goal:** Wrap up assignment artifacts. |
|
|
| - β
`docs/ARCHITECTURE.md` β full system design with all decisions |
| - β
`docs/HLD.md` β Mermaid diagrams + component table |
| - β
`docs/SYSTEM_DESIGN.md` β formal SDD with requirements, flows, trade-offs |
| - β
`README.md` β setup, run, eval instructions |
| - β¬ 5β8 min walkthrough script/recording |
| - β¬ Final eval run after full corpus ingest (target β₯18/20 PASS/PARTIAL) |
|
|
| **Exit:** Demo + repo + note + walkthrough ready to submit. |
|
|
| --- |
|
|
| ## Critical Path |
|
|
| ``` |
| Phase 1 (corpus) β Phase 2 (retrieval) β Phase 3 (LLM) β Phase 4 (safety/router) |
| β Phase 5 (graph) β Phase 6 (grounding) β Phase 9 (API) β Phase 11 (eval) |
| ``` |
|
|
| Phases 7 (memory), 8 (image), 10 (frontend) are parallelizable once the graph |
| (Phase 5) exists. Phases 12β13 are last. |
|
|