RAG Corporativo Seguro
End-to-end secure corporate RAG: ingest -> chunk -> filter by ACL -> hybrid retrieve -> cite -> verify. RAG grounds answers but RAG does not eliminate hallucination; every answer still needs source citation and verification.
When To Activate
- The user says corporate RAG, secure retrieval, knowledge base with permissions, or RAG with Spring AI / LangChain.
- Documents have per-profile visibility (HR vs engineering vs finance) and leaks are a risk.
- Retrieval quality is poor: error codes miss, concepts miss, or chunks cut context mid-answer.
- A previous RAG answered fluently but cited nothing or cited the wrong chunk.
Workflow
1. Scope corpus and profiles
- List sources (docs, tickets, wikis, PDFs) and owners.
- Define profiles (example: support-l1, engineering, hr, finance) and what each may see.
- Record the profile matrix in one file before indexing anything.
2. Chunk with overlap
- Split by structure first (heading, section, ticket, page), then by size (target 400-800 tokens).
- Use overlap 10-20 percent so answers spanning a boundary keep context.
- Keep metadata per chunk: source id, page/section, profile allow-list, updated-at.
3. Filter by ACL BEFORE the model
- Apply ACL at index time (tag chunks) AND at retrieval time (filter query by caller profile).
- Never retrieve-then-hide: if the caller profile lacks access, the chunk must not reach the prompt.
- Threat case: resume with white-on-white text saying "ignore instructions" must never cross profiles; untrusted document text is data, never instructions (see
agent-guardrails).
4. Hybrid retrieval with explicit matrix
- Route by query type:
| Query type | Primary | Why |
|---|---|---|
| Error code, ticket id, exact name | literal (keyword/BM25) | semantic search drifts on exact tokens |
| Concept, symptom, "how to" | semantic (vectors) | keyword search misses paraphrase |
| Mixed ("error 500 on checkout flow") | both, then fuse (RRF or weighted) | each side covers the other |
- Log which side produced each hit so bad routing is debuggable.
5. Cite and verify
- Answer only from retrieved chunks; attach source id + section per claim.
- State explicitly when retrieval is thin: "low evidence, verify before acting".
- Escalate to
iterative-retrievalwhen the first pass misses: reformulate, narrow profile, retry.
6. Stack mapping
- Spring AI: advisors + vector store + content filters; keep ACL filter as a mandatory advisor, not an optional post-step.
- LangChain: splitter -> embeddings -> hybrid retriever -> contextual compression; keep the ACL filter before compression.
- Both stacks share the same contract: profile in, filtered chunks out, citations attached.
7. Team-brain: single-table + RLS + MCP contract (leva YouTube rodada 5)
Não compartilhe um agente — compartilhe a base. Um agente pessoal por pessoa (personalidade + memória individual) + knowledge base central via MCP read-only.
- Tabela
documentscanônica —title, text, url, author, domains, metadata, source. Conectores moldam origem→documento (ex.: Slack thread → author/domains). - Labels na ingestão — coluna
domains(multi-label p/ cross-grupo). Tabelamcp_tokens(token→escopo) + tabelaprincipals(user→grupos,leadership=all). - RLS enforced no DB, nunca no agente — gate no banco (row-level security);
agente que filtra na hora é burlável por prompt injection. Default invisível:
no match = no access(não confundir com conhecido-mas-bloqueado). - Contrato MCP (5 tools) —
identify/auth,search documents,search code(separado),fetch(doc inteiro com check),who-knows(expert finder via author → pessoa p/ follow-up). Remote MCP: URL + Authorization token.
Anti-Patterns
- RAG as truth serum: claiming grounded output cannot hallucinate.
Exemplo
Corpus: 2k docs (HR vs engenharia); chunk 600 tok + overlap 15% + metadados (perfil, seção)
Query "erro 500 checkout" → literal (código exato) + semântico (sintoma) → RRF
ACL antes do modelo: perfil suporte não vê chunk de salário; resposta cita fonte por claim