@idevconn/ai-chat-be
v0.3.0
Published
Drop-in NestJS module for AI chat with multi-provider LLM, RAG, and SSE streaming
Downloads
626
Readme
💬 iDEVconn AI Chat - Backend Module
@idevconn/ai-chat-be is a drop-in, highly-optimized NestJS module that provides an instant, docs-aware AI customer support backend.
No vector database to host, no complex prompt engineering required. Simply point the module at a folder containing your Markdown documentation, and it handles the rest.
🌟 Core Superpowers
⚡ Zero-DB RAG (Retrieval-Augmented Generation)
We completely eliminated the need for external Vector Databases. On application boot, the module reads your local markdown files, generates vector embeddings, and stores them in a lightning-fast, secure in-memory indexing layer.
🛡️ Enterprise-Grade Security Pipeline
Every user input and LLM output goes through a strict, multi-stage security pipeline:
- Prompt Injection Guard: Advanced pattern matching and Unicode deconfusion blocks exploits.
- PII Protection: Automatically scans for and redacts sensitive information (Emails, SSNs, Credit Cards, IPs).
- Cosine Intent Routing: Compares user input embeddings to your document index to identify off-topic questions, automatically redirecting them before they even hit the LLM.
🔌 Multi-Provider Support
Instantly swap between industry-leading LLMs by just changing an environment variable. We natively support:
- Google Gemini (
gemini-2.5-flash) - OpenAI (
gpt-4o) - xAI Grok (
grok-3-mini)
🚀 Quick Start Guide
1. Installation
Install the backend module and the required NestJS throttler:
npm install @idevconn/ai-chat-be @nestjs/throttler2. Register the Module
Import AiChatModule into your root AppModule:
import { Module } from '@nestjs/common';
import { AiChatModule } from '@idevconn/ai-chat-be';
@Module({
imports: [
AiChatModule.forRoot({
// ─── LLM Provider (required) ───────────────────────────
provider: process.env.AI_CHAT_PROVIDER || 'gemini',
apiKey: process.env.AI_CHAT_PROVIDER_API_KEY,
// ─── RAG Settings (optional) ───────────────────────────
rag: {
docsPath: './chatdocs', // Folder containing your markdown files
},
// ─── Widget Authenticity ───────────────────────────────
auth: {
strategy: 'api-key',
key: process.env.AI_CHAT_WIDGET_API_KEY, // Shared secret with frontend
},
}),
],
})
export class AppModule {}3. Add your Markdown Documents
Create a chatdocs/ directory at your project root and drop your markdown guides, help pages, or FAQs in it. The RAG pipeline will index them automatically:
your-nest-app/
├── chatdocs/
│ ├── getting-started.md
│ ├── advanced-features.md
│ └── pricing-faq.md
├── src/
│ └── app.module.ts
└── .env4. Setup Environment Variables
# Set to 'gemini', 'openai', or 'grok'
AI_CHAT_PROVIDER=gemini
AI_CHAT_PROVIDER_API_KEY=your_api_key_here
# Used to authenticate requests from your frontend widget
AI_CHAT_WIDGET_API_KEY=your_secure_shared_secret⚙️ Configuration Reference
Provider Options
| Configuration Option | Environmental Variable | Purpose | Default |
| :------------------- | :--------------------------------- | :------------------------------ | :----------------------- |
| provider | AI_CHAT_PROVIDER | Swaps LLM strategy | 'gemini' |
| apiKey | AI_CHAT_PROVIDER_API_KEY | API Key for the chosen provider | (Required) |
| model | AI_CHAT_PROVIDER_MODEL | Specific LLM model selection | 'gemini-2.5-flash' |
| embeddingModel | AI_CHAT_PROVIDER_EMBEDDING_MODEL | Embedding model override | 'gemini-embedding-001' |
RAG Options
| Configuration Option | Environmental Variable | Purpose | Default |
| :------------------- | :---------------------- | :--------------------------- | :--------- |
| rag.docsPath | AI_CHAT_DOCS_PATH | Path to your markdown folder | './docs' |
| rag.chunkSize | AI_CHAT_CHUNK_SIZE | Words per vector chunk | 250 |
| rag.overlapSize | AI_CHAT_CHUNK_OVERLAP | Overlap to maintain context | 50 |
Security & Limits
| Configuration Option | Environmental Variable | Purpose | Default |
| :---------------------------- | :--------------------- | :------------------------- | :------ |
| limits.maxRequestsPerMinute | AI_CHAT_MAX_RPM | Rate limit per IP address | 10 |
| limits.maxInputTokens | AI_CHAT_MAX_INPUT | Hard limit on prompt size | 2000 |
| limits.maxOutputTokens | AI_CHAT_MAX_OUTPUT | Maximum length of AI reply | 1000 |
🔗 Pair with the Frontend Widget
This backend is designed to pair perfectly with our universal, framework-agnostic frontend widget.
Check out @idevconn/ai-chat-client to complete the integration!
