# RAG Service

## Overview
The RAG (Retrieval-Augmented Generation) Service powers context-aware responses across all connectors (GDrive, Gmail, GChat, Database, Web). It retrieves relevant context from the enterprise knowledge base and augments LLM prompts to generate accurate, grounded answers.

## Responsibilities
- Unify retrieval across all data sources and connectors.
- Maintain domain- and user-aware retrieval policies (RBAC, data visibility).
- Provide ranking, deduplication, chunking, and citation metadata.
- Offer APIs for query -> retrieve -> augment -> generate pipeline.

## Technical Specifications

### Ingestion & Indexing
- Normalize inputs from connectors into chunks with metadata (source, owner, ACLs, timestamps).
- Create and maintain vector indexes and keyword indexes as needed.
- Store references for citations and traceability.

### Retrieval
- Hybrid retrieval (BM25 + vector similarity) with re-ranking.
- Multi-tenant filtering by `user_id`, `space_id`, and `acl`.
- Query expansion and synonym support where applicable.

### Augmentation
- Assemble context windows honoring token budgets and diversity.
- Insert source citations and safety disclaimers in prompts where needed.

### Generation
- Provide a standard interface to the LLM layer (e.g., Gemini) with configurable parameters.
- Return answers with structured metadata: used chunks, confidence, token counts.

### Observability
- Emit telemetry for recall/precision, latency, and token usage.
- Log retrieval/generation steps for auditability.

## Interfaces
- REST/GraphQL endpoints for `search`, `ask`, and `explain`.
- Internal SDK used by the Enterprise Brain Agent and Orchestration.

## Security
- Enforce RBAC and data governance at query time.
- PII redaction options for stored prompts and logs.
