How khixar/enterprise-rag works
Enterprise RAG is a multi-tenant HR document copilot: users create tenants, upload documents, and query their tenant’s material for grounded answers with citations or an escalation when evidence is irrelevant. The query API invokes a LangGraph workflow that retrieves vector and BM25 candidates, fuses rankings, classifies relevance, then answers or escalates; answering reranks candidates and uses an LLM. Ingestion parses and chunks documents, embeds them, and stores tenant-scoped records. The README also documents Langfuse tracing. Endpoint details beyond the sampled query route are inferred from the README and tree; their internal wiring is shown only at the service boundary.
Main entry points: session.py PostgreSQL + pgvector · nodes.py Retrieve node · graph.py Agent graph · main.py FastAPI app
Components 24
API and tenancy
- FastAPI app
app/main.py - Tenant endpoints
app/api/v1/endpoints/tenants.py - Tenant service
app/services/tenant_service.py - Query endpoint
app/api/v1/endpoints/query.py
Document ingestion
- Document endpoints
app/api/v1/endpoints/documents.py - Ingestion service
app/services/document_service.py - Parse and chunk
app/services/parsing.py
Query agent
- Agent graph
app/agent/graph.py - Retrieve node
app/agent/nodes.py - Classify node
app/agent/nodes.py - Answer node
app/agent/nodes.py - Escalate node
app/agent/nodes.py
Search and storage
- Embedding service
app/services/embedding_service.py - BM25 search
app/services/bm25_service.py - Rank fusion
app/services/rrf.py - Reranker service
app/services/reranker_service.py - PostgreSQL + pgvector
app/db/session.py - Document records
app/models/document.py - Chunk records
app/models/chunk.py - Tenant records
app/models/tenant.py
External integrations
- OpenAI
- Cohere
- Langfuse
Other components
- Copilot user
Connections 32
- Copilot user → FastAPI app: sends requests
- FastAPI app → Tenant endpoints: routes tenant calls
- FastAPI app → Document endpoints: routes uploads
- FastAPI app → Query endpoint: routes questions
- Tenant endpoints → Tenant service: manages tenants
- Tenant service → PostgreSQL + pgvector: reads and writes
- Document endpoints → Ingestion service: ingests files
- Ingestion service → Parse and chunk: extracts and chunks
- Ingestion service → Embedding service: embeds chunks
- Ingestion service → PostgreSQL + pgvector: persists records
- PostgreSQL + pgvector → Document records: stores
- PostgreSQL + pgvector → Chunk records: stores
- PostgreSQL + pgvector → Tenant records: stores
- Query endpoint → Agent graph: invokes graph
- Agent graph → Retrieve node: dispatches retrieval
- Retrieve node → Embedding service: embeds question
- Retrieve node → BM25 search: requests keyword search
- Retrieve node → PostgreSQL + pgvector: searches vectors
- Retrieve node → Rank fusion: fuses rankings
- BM25 search → PostgreSQL + pgvector: searches tenant chunks
- Agent graph → Classify node: dispatches classification
- Classify node → OpenAI: classifies relevance
- Agent graph → Answer node: dispatches answer
- Agent graph → Escalate node: dispatches fallback
- Answer node → Reranker service: reranks candidates
- Answer node → OpenAI: generates grounded answer
- Embedding service → OpenAI: requests embeddings
- Reranker service → Cohere: reranks text
- Query endpoint → Langfuse: traces query
- Retrieve node → Langfuse: records retrieval
- Classify node → Langfuse: records routing
- Answer node → Langfuse: records answer