An Express.js + MongoDB + TogetherAI backend that powers AI-driven chat responses, lead tracking, embeddings, and vector search for contextual knowledge retrieval.
- Powered by Meta-Llama-3.1-8B-Instruct-Turbo (TogetherAI)
- Context-aware prompt construction
- Confidence scoring:
- Response confidence
- Search confidence
- Content-quality adjustments
- AI response enriched with dynamic CTAs
- Generates embeddings using BAAI/bge-base-en-v1.5 (TogetherAI)
- Stores embeddings in MongoDB Atlas Vector Search
- Falls back to regex-based text search when vector search is unavailable
- Tracks CTA clicks (with IP + GeoIP lookup)
- Stores leads in MongoDB
- Exports leads as CSV
/api/health– Global health check/api/chat/health– Chat service status/api/track/health– Lead tracking service status
- CORS with production & development configs
- Helmet for secure HTTP headers
- Request logger (method, URL, IP, User-Agent, GeoIP location)
- Backend: Express.js
- Database: MongoDB Atlas
- AI Models: TogetherAI (LLaMA-3.1-8B, BAAI/bge-base-en-v1.5)
- Libraries: cors, helmet, dotenv, geoip-lite, request-ip
-
Clone repository
git clone <repo-url> cd backend
-
Install dependencies
npm install
-
Configure environment variables Create a
.envfile in the project root:NODE_ENV=development PORT=5000 # MongoDB MONGODB_URI=mongodb+srv://<user>:<password>@cluster0.mongodb.net MONGODB_DATABASE=business_analysis_portal MONGODB_COLLECTION=knowledge_base LEAD_COLLECTION=leads # TogetherAI TOGETHERAI_API_KEY=your_api_key_here
-
Run in development
npm run dev
-
Run in production
npm start
GET /→ Welcome message
GET /api/health→ API health status
POST /api/chat/message→ Process chat message and return AI responseGET /api/chat/health→ Chat service health
POST /api/track/lead→ Track CTA clickGET /api/track/export-csv→ Export leads as CSV fileGET /api/track/health→ Lead tracking service health
- User sends a message →
- Embedding is generated →
- Vector search in MongoDB →
- Confidence scoring is applied →
- AI response generated with TogetherAI →
- Lead tracking records CTA interactions
- Structured JSON errors
- Stack trace only visible in development mode
The request logger captures:
- HTTP Method & URL
- IP Address & GeoIP location
- User-Agent