# 05 — RAG and GPT-4o-mini

## Two corpora (never merge files)

| Store | Path | Role in this app |
|-------|------|------------------|
| Mortgage talk tracks | `data/knowledge/mortgage/embeddings.json` | How to speak, FAQ, objections, dispositions |
| Mortgage process/dataset | `data/verticals/mortgage/embeddings.json` | Steps, field why, SI BMI note, disease priority |
| BMI talk + tables | `data/knowledge/bmi/` and `data/verticals/bmi/` | **Only while `process.step === 3`** |
| Health talk + disease UW | `data/knowledge/health/` and `data/verticals/health/` | **Only while `process.step === 4` or 6** |

Retrieval: embed the user message with the **same** hash (or OpenAI embed) as ingest, cosine top-k (k=6–8).

## System prompt (mortgage chat)

Fixed instructions:

- You are a mortgage-protection interview assistant for this app.
- Use CONTEXT blocks only for wording and process rules.
- You are **not** the issuing company. No approval, no guaranteed premium.
- Not PMI, not refinance, not medical insurance (unless step 4 health interview).
- At steps 0–2 do **not** ask height, weight, or disease (align knowledge_base mortgage script).
- At step 3 ask only build + tobacco; use BMI RAG.
- At step 4 ask disease groups; use health RAG; emergency stop if chest pain / severe breathing / stroke-like symptoms now.
- After each turn, output a JSON patch of profile fields (schema in 03).
- Ask **one or two** questions, matching the current step’s missing **R** fields.
- If they only want a loan/refinance, stop the mortgage-protection path.

## Model I/O

Single `gpt-4o-mini` call with `response_format` json_object:

```json
{
  "reply_to_customer": "markdown or plain speech",
  "profile_patch": {},
  "suggested_step": 2,
  "stop_path": null,
  "emergency_stop": false
}
```

Server still **recomputes** `suggested_step` from `gates.ts` so the model cannot skip gates.

Temperature: `0.3`. Include last 8 messages + current profile JSON + missing fields list.

## Citations

Return `source_path` from retrieved chunks (e.g. `knowledge_base/mortgage_details/faq.md`, `html/vertical-processes/process-mortgage-protection.html`). Optional “Why we ask” expander for agents; customers can see a short “from our mortgage protection guide” without dumping internal codes.

## Step 6 generation

Do **not** let the model invent Decline/Level without retrieved verticals. Prefer a deterministic rules stub in v1 (BMI band + knockout flags) and let GPT **explain** the stub using RAG, not invent the code.
