How bhagwatjadhav9422/geniev2backend works
Purpose and Entry Points
This is a Node.js backend for authenticated, document-grounded conversational AI: users upload policy PDFs, the system indexes them asynchronously, and chat requests use retrieval-augmented generation (RAG). Process startup is split between src/index.js and Express application assembly in src/app.js.
HTTP API and Request Boundary
Route modules expose auth, chat, conversations, documents, uploads, and health endpoints. Controllers form the transport boundary and delegate business work to services:
• src/routes/chat.routes.js, src/controllers/chat.controller.js
• src/routes/upload.routes.js, src/controllers/upload.controller.js
• src/routes/conversation.routes.js, src/controllers/conversation.controller.js
Authentication, rate limiting, and multipart upload handling are cross-cutting middleware before controller execution:
• src/middleware/auth.middleware.js
• src/middleware/rateLimiter.middleware.js
• src/middleware/upload.middleware.js
Identity, Persistence, and Audit State
User identity and conversational/document metadata are modeled separately, likely in the configured database. Persistent state includes users, chat sessions/conversations/messages, documents, and audit logs:
• src/models/user.model.js, src/models/auditLog.model.js
• src/models/chatSession.model.js, src/models/conversation.model.js, src/models/message.model.js
• src/models/document.model.js, src/config/db.js
Document Ingestion Pipeline
Upload handling stores source files locally, then queues document processing rather than coupling parsing/indexing to the HTTP request. The background path is upload → queue → worker/processor → PDF extraction/chunking → vector indexing:
• src/services/upload.service.js, src/utils/fileStorage.js
• src/jobs/queues/document.queue.js, src/jobs/workers/document.worker.js, src/jobs/processors/document.processor.js
• src/utils/pdfParser.js, src/utils/chunker.js, src/services/rag/vectorstore.service.js
uploads/ is a local file-storage boundary containing UUID-scoped PDF directories; it requires persistent/shared storage if application instances are distributed.
RAG Chat and Memory Flow
Chat orchestration combines the incoming prompt with conversation history/memory and retrieved document context, optionally reranks results, builds the final prompt, calls the selected LLM provider, and persists the interaction:
• src/services/chat.service.js, src/services/memory.service.js, src/services/conversation.service.js
• src/services/rag/vectorstore.service.js, src/services/reranker.service.js
• src/utils/historyBuilder.js, src/utils/promptBuilder.js, src/config/prompt.config.js
This separates retrieval/index state from relational conversation state and provider-specific generation.
AI Provider Abstraction and Observability
A provider factory normalizes multiple model backends—OpenAI, Google Gemini, and AWS Bedrock—so chat orchestration is not tied to one vendor:
• src/services/providers/providerFactory.js, src/services/providers/index.js
• src/services/providers/openai.provider.js, src/services/providers/gemini.provider.js, src/services/providers/bedrock.provider.js
LangSmith integration suggests LLM tracing/evaluation observability via src/langsmith.js. Offline RAG quality evaluation assets are under evaluation/ragasRunner.js and evaluation/ragas_eval.py.
Runtime Infrastructure
The service depends on environment-driven configuration, a database, Redis-backed capabilities (cache and likely job queue), external LLM APIs, and local upload storage:
• src/config/env.js, src/loadEnv.js
• src/config/redis.js, src/services/cache.service.js
• src/config/logger.js, src/utils/logger.js
Deployment automation is defined in .github/workflows/deploy.yml.
Main entry points: chat.service.js Chat orchestration · chat.controller.js Controllers · upload.service.js Upload service · document.worker.js Document worker
Components 21
Runtime & API
- Node startup
src/index.js - Express app
src/app.js - API routes · Chat, upload, and conversation endpoints.
src/routes/chat.routes.js - Request middleware · Authentication, rate limiting, and multipart handling.
src/middleware/auth.middleware.js - Controllers
src/controllers/chat.controller.js
Document Ingestion
- Upload service
src/services/upload.service.js - Local PDF storage · UUID-scoped files under uploads/.
src/utils/fileStorage.js - Document queue
src/jobs/queues/document.queue.js - Document worker
src/jobs/workers/document.worker.js - Document processor · Extracts PDF text and creates chunks for indexing.
src/jobs/processors/document.processor.js
RAG & Chat
- Vector store service · Indexes chunks and retrieves document context.
src/services/rag/vectorstore.service.js - Chat orchestration · Combines memory, retrieval, prompting, and generation.
src/services/chat.service.js - Memory service
src/services/memory.service.js - Conversation service · Persists conversations and messages.
src/services/conversation.service.js - Reranker · Optional result reranking.
src/services/reranker.service.js - LLM provider factory · Selects OpenAI, Gemini, or Bedrock backends.
src/services/providers/providerFactory.js
State & Integrations
- Application database · Stores users, documents, conversations, messages, and audit logs.
src/config/db.js - Redis
src/config/redis.js - LLM APIs · OpenAI, Google Gemini, and AWS Bedrock.
- LangSmith tracing
src/langsmith.js - RAG evaluation
evaluation/ragasRunner.js
Connections 25
- Node startup → Express app: starts
- Express app → API routes: mounts
- API routes → Request middleware: passes through
- Request middleware → Controllers: authorizes and parses
- Controllers → Upload service: upload requests
- Controllers → Chat orchestration: chat requests
- Controllers → Conversation service: conversation requests
- Upload service → Local PDF storage: stores PDFs
- Upload service → Document queue: enqueues processing
- Document queue → Document worker: dispatches jobs
- Document worker → Document processor: runs
- Document processor → Local PDF storage: reads PDFs
- Document processor → Vector store service: indexes chunks
- Document worker → Redis: uses queue backend
- Chat orchestration → Memory service: loads history
- Chat orchestration → Vector store service: retrieves context
- Chat orchestration → Reranker: optionally refines
- Chat orchestration → LLM provider factory: generates response
- LLM provider factory → LLM APIs: calls selected provider
- Chat orchestration → Conversation service: persists interaction
- Memory service → Application database: reads messages
- Conversation service → Application database: stores conversation state
- Upload service → Application database: records document metadata
- Chat orchestration → LangSmith tracing: traces LLM flow
- RAG evaluation → Vector store service: evaluates RAG quality