@idevconn/ai-chat-be 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +138 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# 💬 iDEVconn AI Chat - Backend Module
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@idevconn/ai-chat-be)
|
|
4
|
+
[](https://nestjs.com/)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
**`@idevconn/ai-chat-be`** is a drop-in, highly-optimized NestJS module that provides an instant, docs-aware AI customer support backend.
|
|
8
|
+
|
|
9
|
+
**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.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## 🌟 Core Superpowers
|
|
14
|
+
|
|
15
|
+
### ⚡ Zero-DB RAG (Retrieval-Augmented Generation)
|
|
16
|
+
|
|
17
|
+
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.
|
|
18
|
+
|
|
19
|
+
### 🛡️ Enterprise-Grade Security Pipeline
|
|
20
|
+
|
|
21
|
+
Every user input and LLM output goes through a strict, multi-stage security pipeline:
|
|
22
|
+
|
|
23
|
+
- **Prompt Injection Guard:** Advanced pattern matching and Unicode deconfusion blocks exploits.
|
|
24
|
+
- **PII Protection:** Automatically scans for and redacts sensitive information (Emails, SSNs, Credit Cards, IPs).
|
|
25
|
+
- **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.
|
|
26
|
+
|
|
27
|
+
### 🔌 Multi-Provider Support
|
|
28
|
+
|
|
29
|
+
Instantly swap between industry-leading LLMs by just changing an environment variable. We natively support:
|
|
30
|
+
|
|
31
|
+
- Google Gemini (`gemini-2.5-flash`)
|
|
32
|
+
- OpenAI (`gpt-4o`)
|
|
33
|
+
- xAI Grok (`grok-3-mini`)
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## 🚀 Quick Start Guide
|
|
38
|
+
|
|
39
|
+
### 1. Installation
|
|
40
|
+
|
|
41
|
+
Install the backend module and the required NestJS throttler:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npm install @idevconn/ai-chat-be @nestjs/throttler
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 2. Register the Module
|
|
48
|
+
|
|
49
|
+
Import `AiChatModule` into your root `AppModule`:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { Module } from '@nestjs/common';
|
|
53
|
+
import { AiChatModule } from '@idevconn/ai-chat-be';
|
|
54
|
+
|
|
55
|
+
@Module({
|
|
56
|
+
imports: [
|
|
57
|
+
AiChatModule.forRoot({
|
|
58
|
+
// ─── LLM Provider (required) ───────────────────────────
|
|
59
|
+
provider: process.env.AI_CHAT_PROVIDER || 'gemini',
|
|
60
|
+
apiKey: process.env.AI_CHAT_PROVIDER_API_KEY,
|
|
61
|
+
|
|
62
|
+
// ─── RAG Settings (optional) ───────────────────────────
|
|
63
|
+
rag: {
|
|
64
|
+
docsPath: './chatdocs', // Folder containing your markdown files
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
// ─── Widget Authenticity ───────────────────────────────
|
|
68
|
+
auth: {
|
|
69
|
+
strategy: 'api-key',
|
|
70
|
+
key: process.env.AI_CHAT_WIDGET_API_KEY, // Shared secret with frontend
|
|
71
|
+
},
|
|
72
|
+
}),
|
|
73
|
+
],
|
|
74
|
+
})
|
|
75
|
+
export class AppModule {}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### 3. Add your Markdown Documents
|
|
79
|
+
|
|
80
|
+
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:
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
your-nest-app/
|
|
84
|
+
├── chatdocs/
|
|
85
|
+
│ ├── getting-started.md
|
|
86
|
+
│ ├── advanced-features.md
|
|
87
|
+
│ └── pricing-faq.md
|
|
88
|
+
├── src/
|
|
89
|
+
│ └── app.module.ts
|
|
90
|
+
└── .env
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 4. Setup Environment Variables
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
# Set to 'gemini', 'openai', or 'grok'
|
|
97
|
+
AI_CHAT_PROVIDER=gemini
|
|
98
|
+
AI_CHAT_PROVIDER_API_KEY=your_api_key_here
|
|
99
|
+
|
|
100
|
+
# Used to authenticate requests from your frontend widget
|
|
101
|
+
AI_CHAT_WIDGET_API_KEY=your_secure_shared_secret
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## ⚙️ Configuration Reference
|
|
107
|
+
|
|
108
|
+
### Provider Options
|
|
109
|
+
|
|
110
|
+
| Configuration Option | Environmental Variable | Purpose | Default |
|
|
111
|
+
| :------------------- | :--------------------------------- | :------------------------------ | :----------------------- |
|
|
112
|
+
| `provider` | `AI_CHAT_PROVIDER` | Swaps LLM strategy | `'gemini'` |
|
|
113
|
+
| `apiKey` | `AI_CHAT_PROVIDER_API_KEY` | API Key for the chosen provider | _(Required)_ |
|
|
114
|
+
| `model` | `AI_CHAT_PROVIDER_MODEL` | Specific LLM model selection | `'gemini-2.5-flash'` |
|
|
115
|
+
| `embeddingModel` | `AI_CHAT_PROVIDER_EMBEDDING_MODEL` | Embedding model override | `'gemini-embedding-001'` |
|
|
116
|
+
|
|
117
|
+
### RAG Options
|
|
118
|
+
|
|
119
|
+
| Configuration Option | Environmental Variable | Purpose | Default |
|
|
120
|
+
| :------------------- | :---------------------- | :--------------------------- | :--------- |
|
|
121
|
+
| `rag.docsPath` | `AI_CHAT_DOCS_PATH` | Path to your markdown folder | `'./docs'` |
|
|
122
|
+
| `rag.chunkSize` | `AI_CHAT_CHUNK_SIZE` | Words per vector chunk | `250` |
|
|
123
|
+
| `rag.overlapSize` | `AI_CHAT_CHUNK_OVERLAP` | Overlap to maintain context | `50` |
|
|
124
|
+
|
|
125
|
+
### Security & Limits
|
|
126
|
+
|
|
127
|
+
| Configuration Option | Environmental Variable | Purpose | Default |
|
|
128
|
+
| :---------------------------- | :--------------------- | :------------------------- | :------ |
|
|
129
|
+
| `limits.maxRequestsPerMinute` | `AI_CHAT_MAX_RPM` | Rate limit per IP address | `10` |
|
|
130
|
+
| `limits.maxInputTokens` | `AI_CHAT_MAX_INPUT` | Hard limit on prompt size | `2000` |
|
|
131
|
+
| `limits.maxOutputTokens` | `AI_CHAT_MAX_OUTPUT` | Maximum length of AI reply | `1000` |
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## 🔗 Pair with the Frontend Widget
|
|
136
|
+
|
|
137
|
+
This backend is designed to pair perfectly with our universal, framework-agnostic frontend widget.
|
|
138
|
+
Check out **[`@idevconn/ai-chat-client`](https://www.npmjs.com/package/@idevconn/ai-chat-client)** to complete the integration!
|
package/package.json
CHANGED