Domain Templates
Each template specifies: corpus shape, chunking, embedding model, metadata schema, retrieval strategy, guardrails, eval criteria.
Template 1: Customer Support
Corpus Shape
- Help articles (500-2000 words), macros, product FAQs, historical tickets, release notes.
- High churn: content changes weekly.
- Often multilingual.
Chunking
- Help articles: Markdown header split, 800 chars, 150 overlap.
- Historical tickets: per-turn; one chunk = (question + resolution) pair.
- Release notes: per version, fixed-size 600 chars.
Embedding
text-embedding-3-small (cost-sensitive) or multilingual embed-multilingual-v3.0 (Cohere) if non-English.
Metadata Schema
{
"article_id": str, "category": str, "product": str,
"locale": str, "audience": Literal["customer","agent","internal"],
"updated_at": datetime, "deprecated": bool,
"resolution_success_rate": float # for ticket chunks
}
Retrieval
- Hybrid BM25 + dense; reranker.
- Hard filter:
deprecated=false, audience in allowed_audiences, locale in user_locales.
- Soft bias: recency decay (half-life 180 days); boost
resolution_success_rate for ticket chunks.
Guardrails
- Hallucination tolerance: low. Answer grounded strictly in retrieved content; fall back to "contact support" if confidence low.
- PII: strip customer names/emails from historical tickets before indexing.
- Response style: short, action-oriented, always cite article.
Eval
- Resolution rate (did the user stop asking?).
- Deflection rate (% of queries handled without human).
- Agent override rate (agent edits response before sending).
- First-contact resolution within N turns.
Template 2: Developer Documentation
Corpus Shape
- API docs (generated), tutorials, cookbook examples, SDK source code, GitHub issues.
- Heterogeneous: prose + code + reference tables.
Chunking
- Prose: Markdown header split, 1000 chars, 200 overlap.
- Code: Language-aware splitter, 1500 chars; preserve whole functions/classes.
- API reference: one chunk per endpoint or method.
Embedding
voyage-code-3 for code-heavy; text-embedding-3-large for mixed.
- Separate indexes for code vs prose; route per query intent.
Metadata Schema
{
"doc_id": str, "kind": Literal["prose","code","api","example"],
"language": str, "library": str, "version": str,
"section_path": list[str], "symbol": str | None,
"updated_at": datetime
}
Retrieval
- Query classifier: "how do I" -> prose+examples; "what does X return" -> api; "why does X fail" -> issues+examples.
- Version-aware filter from user's installed version.
- Code-aware reranker (cross-encoder trained on code-query pairs).
Guardrails
- Answer must include a runnable code snippet when a how-to is requested.
- Version lock: never return docs for a different major version unless explicitly asked.
- Hallucination tolerance: zero on API signatures. Verify symbol exists in indexed API reference before emitting.
Eval
- Code snippet runnability (execute in sandbox; pass/fail).
- API signature accuracy (does returned signature match indexed reference?).
- Link coverage (% answers that cite the canonical doc page).
- Offline benchmark: SWE-bench-retrieval, CodeRAG-bench.
Template 3: Legal
Corpus Shape
- Contracts, case law, statutes, internal memos, regulations.
- Stable content; layered (statute -> regulation -> case interpretation).
- Extreme precision needs; jurisdictional.
Chunking
- Contracts / statutes: section/clause aware; proposition-based for atomic facts.
- Case law: head-note + holding + reasoning separately indexed.
- Preserve section numbering and hierarchy in metadata.
Embedding
- Domain-tuned:
Nomic Embed Legal, Voyage voyage-law-2, or fine-tune on case retrieval corpus.
Metadata Schema
{
"doc_id": str, "kind": Literal["contract","statute","regulation","case","memo"],
"jurisdiction": str, "effective_date": date, "superseded_by": str | None,
"section_path": list[str], "clause_number": str | None,
"citation": str, "parties": list[str] | None,
"valid_from": date | None, "valid_to": date | None,
"authority_score": float
}
Retrieval
- Hybrid search mandatory; BM25 for exact statute lookups (e.g., "Section 11 USC 362").
- Strict filters:
jurisdiction, effective_date <= query_date, superseded_by is null.
- Time-travel support (
valid_at) for historical analysis.
- Hierarchical: first retrieve statute, then regulations, then cases interpreting it.
Guardrails
- Citations mandatory; every claim attributed.
- Never paraphrase statute wording; quote verbatim.
- "This is not legal advice" disclaimer.
- Superseded content filter at query time, with explicit override.
- PII: redact clients/parties if showing to other users.
Eval
- Citation correctness (cited source actually supports claim) — human-graded.
- Jurisdictional correctness.
- Temporal correctness (statute version active at query date).
- Attorney review score on a sample.
Template 4: Medical
Corpus Shape
- Clinical guidelines, drug monographs, clinical trials, EHR notes, patient education.
- Very high stakes; life-safety.
- Regulated: HIPAA in US, GDPR in EU.
Chunking
- Guidelines: propositional (each recommendation a chunk).
- Drug monographs: section-aware (indications, dosing, contraindications).
- Clinical trials: structured abstracts, one chunk per PICO field.
Embedding
BioBERT, PubMedBERT, or Nomic Embed Medical. Fine-tune on UMLS synonyms.
Metadata Schema
{
"doc_id": str, "kind": Literal["guideline","drug","trial","ehr","patient_ed"],
"icd10": list[str], "snomed_ct": list[str], "rxnorm": list[str],
"evidence_level": Literal["A","B","C","D"],
"population": dict, # age range, pregnancy, renal function
"updated_at": datetime, "authority": str,
"contraindications": list[str],
"phi_status": Literal["none","de-identified","identified"]
}
Retrieval
- Query augmentation with medical ontology expansion (UMLS/SNOMED synonyms).
- Strict filter on
authority for clinical-decision queries (e.g., only from approved guideline sources).
- Boost higher evidence levels.
- Population filter: match patient demographics explicitly.
Guardrails
- HIPAA: no PHI leaves the secure boundary; all operations logged.
- Answer template: always include evidence level, source, caveats.
- Hard refusals: dosing recommendations without patient context, diagnosis, emergency.
- Minimum authority threshold per query type (e.g., must be from NIH/guideline source).
- Version-pinned: drug monographs versioned; use latest unless queried historically.
- Disclaimer: "for informational purposes; not a substitute for clinician judgment".
- Audit log: every query + retrieval + response stored for compliance.
Eval
- Clinical accuracy (clinician-graded).
- Safety (any contraindication missed = fail).
- Citation match (cited source supports claim).
- Population-appropriate (pediatric vs adult dosing correct).
- Benchmark: MedQA, MedMCQA with retrieval-augmented variants.
Template 5: Financial
Corpus Shape
- 10-K/10-Q filings, earnings transcripts, analyst research, market data, internal research.
- Structured (tables, XBRL) + unstructured (MD&A narrative).
- Time-sensitive; regulatory.
Chunking
- 10-K/10-Q: section-aware (Item 1, 1A Risk Factors, 7 MD&A).
- Earnings transcripts: per speaker + topic shift (semantic chunking).
- Tables: preserve as structured data; NL2SQL hybrid (see
tabular-rag).
- Research reports: executive summary + sections.
Embedding
voyage-finance-2, FinE5, or FinBERT fine-tuned.
Metadata Schema
{
"doc_id": str, "kind": Literal["10k","10q","transcript","research","news","table"],
"ticker": str, "fiscal_period": str, # "2025-Q3"
"filing_date": date, "reporting_date": date,
"section": str, "speaker": str | None,
"currency": str, "is_restatement": bool,
"embargo_until": datetime | None # for non-public research
}
Retrieval
- Ticker filter mandatory for company-specific queries.
- Fiscal period filter or time-aware scoring.
- Restatement handling: when is_restatement=true, flag prior versions as superseded.
- Embargo: respect
embargo_until; filter at query time.
- Hybrid structured (numbers from tables) + unstructured (narrative) retrieval.
Guardrails
- Forward-looking statements flagged per Safe Harbor.
- No advice on buy/sell/hold — information only.
- Material non-public information: restrict by user entitlement.
- Numerical accuracy: extract numbers from the source (not paraphrased).
- Citation with page + paragraph precision.
- Audit: every research query logged per regulatory requirements (FINRA).
Eval
- Numerical extraction accuracy (reported numbers match filing exactly).
- Temporal correctness (did the query find the right fiscal period?).
- Forward-looking statement detection rate.
- Analyst QA score on a monthly sample.
- Embargo leak rate (should be zero).
Cross-Domain Patterns
| Aspect |
Support |
Dev Docs |
Legal |
Medical |
Financial |
| Hallucination tolerance |
Low |
Zero on API |
Very low |
Zero |
Very low |
| PII/PHI risk |
Medium |
Low |
High |
Very high |
Medium-high |
| Citation mandatory |
Preferred |
Yes |
Always |
Always |
Always |
| Temporal awareness |
Helpful |
Version |
Critical |
Important |
Critical |
| Authority signal |
Useful |
Vital |
Critical |
Critical |
Critical |
| Ontology augmentation |
Optional |
Optional |
Optional |
Vital (UMLS) |
Useful (tickers) |
| Regulatory audit |
No |
No |
Yes |
Yes (HIPAA) |
Yes (FINRA/SEC) |
Anti-Patterns (Cross-Domain)
| Anti-Pattern |
Fix |
| Generic chunking for specialized content |
Domain-aware splitters |
| Generic embedding model for specialized language |
Domain-tuned model (medical, legal, code, finance) |
| No authority / source hierarchy |
Canonical sources need explicit boost |
| No time/version filter |
Returns stale/superseded content |
| Same guardrails across domains |
Medical/legal need stricter refusal policy |
| No audit log in regulated domains |
Makes compliance demonstrably impossible |
| One index for mixed content |
Split per corpus type with router |
| Evaluation with generic benchmarks only |
Need domain experts for final grading |
Production Checklist (Per-Domain)
1---2name: domain-templates3description: Production RAG templates for five domains: customer-support, developer-docs, legal, medical, financial. Each covers corpus shape, chunking, embedding model choice, metadata schema, domain-specific guardrails (hallucination tolerance, PII, compliance), and evaluation criteria. USE WHEN: user mentions "RAG for support", "legal RAG", "medical RAG", "developer docs RAG", "financial RAG", "domain template RAG" DO NOT USE FOR: general architecture - use `rag-architecture`; evaluation methodology - use `rag-evaluation`; security specifics - use `rag-security`4---5# Domain Templates67Each template specifies: corpus shape, chunking, embedding model, metadata schema, retrieval strategy, guardrails, eval criteria.89## Template 1: Customer Support1011### Corpus Shape12- Help articles (500-2000 words), macros, product FAQs, historical tickets, release notes.13- High churn: content changes weekly.14- Often multilingual.1516### Chunking17- Help articles: Markdown header split, 800 chars, 150 overlap.18- Historical tickets: per-turn; one chunk = (question + resolution) pair.19- Release notes: per version, fixed-size 600 chars.2021### Embedding22- `text-embedding-3-small` (cost-sensitive) or multilingual `embed-multilingual-v3.0` (Cohere) if non-English.2324### Metadata Schema25```python26{27 "article_id": str, "category": str, "product": str,28 "locale": str, "audience": Literal["customer","agent","internal"],29 "updated_at": datetime, "deprecated": bool,30 "resolution_success_rate": float # for ticket chunks31}32```3334### Retrieval35- Hybrid BM25 + dense; reranker.36- Hard filter: `deprecated=false`, `audience in allowed_audiences`, `locale in user_locales`.37- Soft bias: recency decay (half-life 180 days); boost `resolution_success_rate` for ticket chunks.3839### Guardrails40- Hallucination tolerance: low. Answer grounded strictly in retrieved content; fall back to "contact support" if confidence low.41- PII: strip customer names/emails from historical tickets before indexing.42- Response style: short, action-oriented, always cite article.4344### Eval45- Resolution rate (did the user stop asking?).46- Deflection rate (% of queries handled without human).47- Agent override rate (agent edits response before sending).48- First-contact resolution within N turns.4950## Template 2: Developer Documentation5152### Corpus Shape53- API docs (generated), tutorials, cookbook examples, SDK source code, GitHub issues.54- Heterogeneous: prose + code + reference tables.5556### Chunking57- Prose: Markdown header split, 1000 chars, 200 overlap.58- Code: Language-aware splitter, 1500 chars; preserve whole functions/classes.59- API reference: one chunk per endpoint or method.6061### Embedding62- `voyage-code-3` for code-heavy; `text-embedding-3-large` for mixed.63- Separate indexes for code vs prose; route per query intent.6465### Metadata Schema66```python67{68 "doc_id": str, "kind": Literal["prose","code","api","example"],69 "language": str, "library": str, "version": str,70 "section_path": list[str], "symbol": str | None,71 "updated_at": datetime72}73```7475### Retrieval76- Query classifier: "how do I" -> prose+examples; "what does X return" -> api; "why does X fail" -> issues+examples.77- Version-aware filter from user's installed version.78- Code-aware reranker (cross-encoder trained on code-query pairs).7980### Guardrails81- Answer must include a runnable code snippet when a how-to is requested.82- Version lock: never return docs for a different major version unless explicitly asked.83- Hallucination tolerance: zero on API signatures. Verify symbol exists in indexed API reference before emitting.8485### Eval86- Code snippet runnability (execute in sandbox; pass/fail).87- API signature accuracy (does returned signature match indexed reference?).88- Link coverage (% answers that cite the canonical doc page).89- Offline benchmark: SWE-bench-retrieval, CodeRAG-bench.9091## Template 3: Legal9293### Corpus Shape94- Contracts, case law, statutes, internal memos, regulations.95- Stable content; layered (statute -> regulation -> case interpretation).96- Extreme precision needs; jurisdictional.9798### Chunking99- Contracts / statutes: section/clause aware; proposition-based for atomic facts.100- Case law: head-note + holding + reasoning separately indexed.101- Preserve section numbering and hierarchy in metadata.102103### Embedding104- Domain-tuned: `Nomic Embed Legal`, Voyage `voyage-law-2`, or fine-tune on case retrieval corpus.105106### Metadata Schema107```python108{109 "doc_id": str, "kind": Literal["contract","statute","regulation","case","memo"],110 "jurisdiction": str, "effective_date": date, "superseded_by": str | None,111 "section_path": list[str], "clause_number": str | None,112 "citation": str, "parties": list[str] | None,113 "valid_from": date | None, "valid_to": date | None,114 "authority_score": float115}116```117118### Retrieval119- Hybrid search mandatory; BM25 for exact statute lookups (e.g., "Section 11 USC 362").120- Strict filters: `jurisdiction`, `effective_date <= query_date`, `superseded_by is null`.121- Time-travel support (`valid_at`) for historical analysis.122- Hierarchical: first retrieve statute, then regulations, then cases interpreting it.123124### Guardrails125- Citations mandatory; every claim attributed.126- Never paraphrase statute wording; quote verbatim.127- "This is not legal advice" disclaimer.128- Superseded content filter at query time, with explicit override.129- PII: redact clients/parties if showing to other users.130131### Eval132- Citation correctness (cited source actually supports claim) — human-graded.133- Jurisdictional correctness.134- Temporal correctness (statute version active at query date).135- Attorney review score on a sample.136137## Template 4: Medical138139### Corpus Shape140- Clinical guidelines, drug monographs, clinical trials, EHR notes, patient education.141- Very high stakes; life-safety.142- Regulated: HIPAA in US, GDPR in EU.143144### Chunking145- Guidelines: propositional (each recommendation a chunk).146- Drug monographs: section-aware (indications, dosing, contraindications).147- Clinical trials: structured abstracts, one chunk per PICO field.148149### Embedding150- `BioBERT`, `PubMedBERT`, or `Nomic Embed Medical`. Fine-tune on UMLS synonyms.151152### Metadata Schema153```python154{155 "doc_id": str, "kind": Literal["guideline","drug","trial","ehr","patient_ed"],156 "icd10": list[str], "snomed_ct": list[str], "rxnorm": list[str],157 "evidence_level": Literal["A","B","C","D"],158 "population": dict, # age range, pregnancy, renal function159 "updated_at": datetime, "authority": str,160 "contraindications": list[str],161 "phi_status": Literal["none","de-identified","identified"]162}163```164165### Retrieval166- Query augmentation with medical ontology expansion (UMLS/SNOMED synonyms).167- Strict filter on `authority` for clinical-decision queries (e.g., only from approved guideline sources).168- Boost higher evidence levels.169- Population filter: match patient demographics explicitly.170171### Guardrails172- HIPAA: no PHI leaves the secure boundary; all operations logged.173- Answer template: always include evidence level, source, caveats.174- Hard refusals: dosing recommendations without patient context, diagnosis, emergency.175- Minimum authority threshold per query type (e.g., must be from NIH/guideline source).176- Version-pinned: drug monographs versioned; use latest unless queried historically.177- Disclaimer: "for informational purposes; not a substitute for clinician judgment".178- Audit log: every query + retrieval + response stored for compliance.179180### Eval181- Clinical accuracy (clinician-graded).182- Safety (any contraindication missed = fail).183- Citation match (cited source supports claim).184- Population-appropriate (pediatric vs adult dosing correct).185- Benchmark: MedQA, MedMCQA with retrieval-augmented variants.186187## Template 5: Financial188189### Corpus Shape190- 10-K/10-Q filings, earnings transcripts, analyst research, market data, internal research.191- Structured (tables, XBRL) + unstructured (MD&A narrative).192- Time-sensitive; regulatory.193194### Chunking195- 10-K/10-Q: section-aware (Item 1, 1A Risk Factors, 7 MD&A).196- Earnings transcripts: per speaker + topic shift (semantic chunking).197- Tables: preserve as structured data; NL2SQL hybrid (see `tabular-rag`).198- Research reports: executive summary + sections.199200### Embedding201- `voyage-finance-2`, FinE5, or FinBERT fine-tuned.202203### Metadata Schema204```python205{206 "doc_id": str, "kind": Literal["10k","10q","transcript","research","news","table"],207 "ticker": str, "fiscal_period": str, # "2025-Q3"208 "filing_date": date, "reporting_date": date,209 "section": str, "speaker": str | None,210 "currency": str, "is_restatement": bool,211 "embargo_until": datetime | None # for non-public research212}213```214215### Retrieval216- Ticker filter mandatory for company-specific queries.217- Fiscal period filter or time-aware scoring.218- Restatement handling: when is_restatement=true, flag prior versions as superseded.219- Embargo: respect `embargo_until`; filter at query time.220- Hybrid structured (numbers from tables) + unstructured (narrative) retrieval.221222### Guardrails223- Forward-looking statements flagged per Safe Harbor.224- No advice on buy/sell/hold — information only.225- Material non-public information: restrict by user entitlement.226- Numerical accuracy: extract numbers from the source (not paraphrased).227- Citation with page + paragraph precision.228- Audit: every research query logged per regulatory requirements (FINRA).229230### Eval231- Numerical extraction accuracy (reported numbers match filing exactly).232- Temporal correctness (did the query find the right fiscal period?).233- Forward-looking statement detection rate.234- Analyst QA score on a monthly sample.235- Embargo leak rate (should be zero).236237## Cross-Domain Patterns238239| Aspect | Support | Dev Docs | Legal | Medical | Financial |240|---|---|---|---|---|---|241| Hallucination tolerance | Low | Zero on API | Very low | Zero | Very low |242| PII/PHI risk | Medium | Low | High | Very high | Medium-high |243| Citation mandatory | Preferred | Yes | Always | Always | Always |244| Temporal awareness | Helpful | Version | Critical | Important | Critical |245| Authority signal | Useful | Vital | Critical | Critical | Critical |246| Ontology augmentation | Optional | Optional | Optional | Vital (UMLS) | Useful (tickers) |247| Regulatory audit | No | No | Yes | Yes (HIPAA) | Yes (FINRA/SEC) |248249## Anti-Patterns (Cross-Domain)250251| Anti-Pattern | Fix |252|---|---|253| Generic chunking for specialized content | Domain-aware splitters |254| Generic embedding model for specialized language | Domain-tuned model (medical, legal, code, finance) |255| No authority / source hierarchy | Canonical sources need explicit boost |256| No time/version filter | Returns stale/superseded content |257| Same guardrails across domains | Medical/legal need stricter refusal policy |258| No audit log in regulated domains | Makes compliance demonstrably impossible |259| One index for mixed content | Split per corpus type with router |260| Evaluation with generic benchmarks only | Need domain experts for final grading |261262## Production Checklist (Per-Domain)263264- [ ] Corpus shape characterized (count, size, churn rate, language)265- [ ] Chunking strategy picked per content type in the corpus266- [ ] Domain-tuned embedding model evaluated vs general-purpose267- [ ] Metadata schema declared and validated (Pydantic)268- [ ] Authority signal defined and populated269- [ ] Time / version filter wired270- [ ] Ontology augmentation (if applicable)271- [ ] Guardrail prompt templates checked by a domain expert272- [ ] Citation format enforced in the response schema273- [ ] Domain-specific eval set (>= 100 queries, expert-graded)274- [ ] Regulatory audit logging turned on where required275- [ ] Refusal criteria documented and tested276- [ ] Escalation path (human review) for low-confidence answers277- [ ] Re-evaluation cadence scheduled (quarterly for regulated domains)