nothingworry commited on
Commit
e7b6af9
Β·
1 Parent(s): d74c0dc

update the readme files

Browse files
Files changed (1) hide show
  1. README.md +410 -122
README.md CHANGED
@@ -6,6 +6,23 @@
6
 
7
  ---
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  ## Overview
10
 
11
  **IntegraChat** is an enterprise-grade, multi-tenant AI platform that demonstrates the full capabilities of the **Model Context Protocol (MCP)** in a production-style environment. Built with enterprise governance and observability in mind, IntegraChat combines autonomous tool-using agents, RAG retrieval, live web search, and admin compliance under strict tenant isolation.
@@ -14,6 +31,47 @@ This platform showcases how MCP can power intelligent, governed, multi-tenant AI
14
 
15
  ---
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  ## Features
18
 
19
  ### Core Capabilities
@@ -58,184 +116,320 @@ This platform showcases how MCP can power intelligent, governed, multi-tenant AI
58
 
59
  ---
60
 
61
- ## How to Run the Space
 
62
  ### Prerequisites
63
 
64
- 1. **Backend services running**:
65
- - FastAPI API (`uvicorn backend.api.main:app --port 8000`)
66
- - Unified MCP server (port 8900) as described in `backend/README.md`
67
- - Optional: Ollama / Groq credentials for the LLM client
68
-
69
- **Quick Start**: Run `start.bat` (Windows) to launch all services automatically.
70
- 2. **Python 3.10+** with the dependencies in `requirements.txt`
71
- 3. **Google Custom Search credentials** for live web search:
72
- - Enable the *Custom Search API* in the [Google Cloud Console](https://console.cloud.google.com/) and create an API key β†’ set it as `GOOGLE_SEARCH_API_KEY` in `.env`
73
- - Create a Programmable Search Engine that searches the entire web and copy its *Search engine ID* β†’ set it as `GOOGLE_SEARCH_CX_ID` in `.env`
74
- - Restart the backend after updating `.env` so the new variables are picked up
75
 
76
- ### Installation
77
 
78
- 1. **Install dependencies**:
79
  ```bash
80
- pip install -r requirements.txt
81
  ```
82
 
83
- 2. **Start the Gradio app**:
84
  ```bash
85
- python app.py
 
 
 
 
 
 
86
  ```
87
 
88
- 3. **Access the interface**:
89
- - Local: `http://localhost:7860`
90
- - The app will automatically connect to the backend at `http://localhost:8000`
91
-
92
- ### Usage
93
-
94
- The Gradio UI exposes four tabs once you launch `app.py`:
95
-
96
- 1. **Chat** – enter your Tenant ID, ask questions, and see multi-tool MCP responses with autonomous tool orchestration.
97
 
98
- 2. **Document Ingestion** – toggle between Raw Text, URL, or File Upload to populate the tenant RAG index. View and manage your ingested documents with delete functionality.
 
 
 
 
 
 
 
 
99
 
100
- 3. **Knowledge Base Library** – comprehensive document management interface with:
101
- - **Statistics Dashboard**: Visual cards showing total documents, document types (Text, PDF, FAQ, Link), and average length
102
- - **Interactive Charts**: Plotly pie chart displaying document type distribution
103
- - **Semantic Search**: Search your knowledge base with relevance scoring
104
- - **Type Filtering**: Filter documents by type (all, text, pdf, faq, link)
105
- - **Document Management**: View all documents in a table with preview, delete individual documents, or delete all at once
106
- - **Auto-refresh**: Document lists automatically update after ingestion or deletion
107
 
108
- 4. **Admin Analytics** – comprehensive analytics dashboard with visualizations:
109
- - **Statistics Cards**: Total queries, active users, red flags, and RAG searches
110
- - **Interactive Bar Charts**:
111
- - Tool Usage Count (RAG, Web, Admin tools)
112
- - Average Tool Latency (performance metrics)
113
- - RAG Quality Metrics (hits, scores, recall indicators)
114
- - **Tool Usage Table**: Detailed breakdown of tool performance with counts, latency, success/error rates, and token usage
115
- - **Formatted Summary**: Key metrics displayed in an easy-to-read format
116
- - Click "πŸ”„ Fetch Analytics Snapshot" to load the latest data
117
 
118
- 5. **Admin Rules & Compliance** – comprehensive rule management with:
119
- - **Text Input**: Paste rules one per line (comment lines starting with # are automatically ignored)
120
- - **File Upload**: Upload rules from TXT, PDF, DOC, or DOCX files with drag-and-drop support
121
- - **LLM Enhancement**: Rules are automatically enhanced by LLM to identify edge cases, improve patterns, and suggest severity levels
122
- - **Chunk Processing**: Large rule sets processed in chunks (5 rules at a time) to avoid timeouts
123
- - **Rule-Based Behavior**: Rules are checked FIRST before normal processing - brief response rules (low severity) return quick responses, blocking rules (high severity) block requests
124
- - **Streaming Responses**: Chat responses stream word-by-word for better user experience
125
- - **Refresh Button**: Refresh rules directly from the Rule Set table
126
 
127
- **Tip:** Every action requires a tenant ID. The tenant ID is now managed centrally and persists across page refreshes. The Knowledge Base Library and Admin Analytics tabs feature beautiful, modern UI with dark theme styling and interactive Plotly visualizations.
 
 
 
 
 
 
 
128
 
129
- ### Frontend (Next.js) Operator Console
130
 
131
- The companion Next.js frontend (`frontend/`) now exposes dedicated pages for each workflow:
 
 
 
 
132
 
133
- | URL | Description |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  | --- | --- |
135
- | `/` | Landing page with hero + quick access panels |
136
  | `/ingestion` | Data ingestion walkthrough (text/URL/files) with document management |
137
- | `/chat` | Chat console wrapper around the MCP agent |
138
- | `/analytics` | Analytics overview and explainer |
139
- | `/admin-rules` | Admin rule ingestion explainer |
140
- | `/knowledge-base` | View all ingested documents with search, filter, and delete functionality |
141
 
142
  **Key Features:**
143
- - **Centralized Tenant ID Management** – Tenant ID is managed globally via React Context and persists in localStorage
144
- - **Document Management** – View, search, filter, and delete documents from the knowledge base
145
- - **Improved Error Handling** – Clear error messages with retry options for failed operations
146
- - **Real-time Updates** – Document lists automatically refresh after ingestion or deletion
 
147
 
148
- Run the console locally with:
149
  ```bash
150
  cd frontend
151
  npm install
152
  npm run dev
153
  ```
154
- Then open `http://localhost:3000`. The navbar links on the landing page route to each section, and you can link directly to those URLs for demo purposes. The tenant ID selector is available in the navbar on all pages.
155
 
156
  ---
157
 
158
  ## API Endpoints
159
 
 
 
 
 
160
  ### Agent Endpoints
161
 
162
- | Purpose | Method & Path | Description |
163
  | --- | --- | --- |
164
- | Chat with agent | `POST /agent/message` | Main chat endpoint with `tenant_id`, `message`, optional history |
165
- | Chat with agent (streaming) | `POST /agent/message/stream` | Streams response word-by-word using Server-Sent Events (SSE). Returns status messages and tokens as they're generated |
166
- | Agent debug | `POST /agent/debug` | Returns detailed debugging info: reasoning trace, tool selection, intent classification |
167
- | Agent plan | `POST /agent/plan` | Returns tool selection plan without execution (intent, tool scores, planned steps) |
168
 
169
  ### RAG Endpoints
170
 
171
- | Purpose | Method & Path | Description |
172
  | --- | --- | --- |
173
- | Ingest document | `POST /rag/ingest-document` | Accepts `source_type`, `content`, metadata (filename, URL, doc_id). Supports raw text, URLs, PDFs, DOCX, TXT, and Markdown files |
174
- | Ingest file | `POST /rag/ingest-file` | Multipart upload with `x-tenant-id` header (PDF/DOCX/TXT/MD) |
175
- | List documents | `GET /rag/list?tenant_id={id}&limit={n}&offset={n}` | Returns all documents for a tenant with pagination. Requires `x-tenant-id` header or `tenant_id` query parameter |
176
- | Delete document | `DELETE /rag/delete/{document_id}?tenant_id={id}` | Deletes a specific document by ID. Requires `x-tenant-id` header or `tenant_id` query parameter |
177
- | Delete all documents | `DELETE /rag/delete-all?tenant_id={id}` | Deletes all documents for a tenant. Requires `x-tenant-id` header or `tenant_id` query parameter |
 
 
178
 
179
  ### Admin & Governance Endpoints
180
 
181
- | Purpose | Method & Path | Description |
182
  | --- | --- | --- |
183
- | List rules | `GET /admin/rules?detailed=true` | Get all rules (use `detailed=true` for regex/severity metadata) |
184
- | Add rule | `POST /admin/rules?enhance=true` | Add single rule with optional `pattern` (regex), `severity` (low/medium/high/critical), `description`. Set `enhance=true` for LLM enhancement |
185
- | Add rules bulk | `POST /admin/rules/bulk?enhance=true` | Add multiple rules at once. Processed in chunks of 5 to avoid timeouts. LLM enhancement applied automatically |
186
- | Upload rules file | `POST /admin/rules/upload-file?enhance=true` | Upload rules from file (TXT, PDF, DOC, DOCX). Text extracted server-side, rules processed with LLM enhancement |
187
- | Delete rule | `DELETE /admin/rules/{rule}` | Delete a specific rule |
188
- | List violations | `GET /admin/violations?days=30&limit=50` | Get red-flag violations with timestamps and confidence scores |
189
- | Tool logs | `GET /admin/tools/logs?tool_name=rag&days=7` | Get detailed tool usage logs with latency and token counts |
190
- | Manage tenants | `GET/POST/DELETE /admin/tenants` | Tenant management endpoints (placeholder implementation) |
191
- | Setup Supabase table | `POST /admin/setup/table` | Create admin_rules table in Supabase if it doesn't exist |
192
 
193
  ### Analytics Endpoints
194
 
195
- | Purpose | Method & Path | Description |
196
  | --- | --- | --- |
197
- | Overview | `GET /analytics/overview?days=30` | Comprehensive analytics: total queries, tool usage, red-flag count, RAG quality |
198
- | Tool usage | `GET /analytics/tool-usage?days=30` | Detailed tool usage stats: counts, latency, tokens, success/error rates |
199
- | Red flags | `GET /analytics/redflags?limit=50&days=30` | Recent red-flag violations for tenant |
200
- | Activity | `GET /analytics/activity?days=30` | Tenant activity summary: queries, active users, last query timestamp |
201
- | RAG quality | `GET /analytics/rag-quality?days=30` | RAG quality metrics: avg hits, scores, latency (recall/precision indicators) |
 
 
202
 
203
- All calls are proxied through the FastAPI backend running at `http://localhost:8000`. Ensure those services are online before launching the Space.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
 
205
  ---
206
 
207
- ## Architecture Highlights
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
  ### Enterprise-Grade Features
210
 
211
- 1. **Autonomous Multi-Step Planning**: The agent uses LLM-powered planning to determine optimal tool sequences, with memory of previous tool outputs in multi-step workflows.
212
 
213
  2. **Regex-Based Governance**: Admin rules support regex patterns with fallback to keyword matching and semantic similarity scoring for flexible policy enforcement.
214
 
215
- 3. **Comprehensive Analytics**: All tool usage, RAG searches, LLM calls, and red-flag violations are logged to SQLite with indexed queries for fast analytics retrieval.
216
 
217
- 4. **Enhanced RAG Pipeline**: Documents are chunked with optimal size (400-600 tokens) and enriched with metadata (source URL, timestamp, document type) for better retrieval.
218
 
219
- 5. **Structured Error Handling**: All errors are logged with context, and the system gracefully falls back (e.g., if RAG fails β†’ use LLM-only, if web fails β†’ skip web).
220
 
221
- ### Data Storage
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
 
223
- IntegraChat supports **dual-backend storage** with automatic fallback:
224
 
225
- - **Supabase (Production/Preferred)**:
226
- - `admin_rules` table - Admin rules with regex patterns and severity
227
- - `tool_usage_events`, `redflag_violations`, `rag_search_events`, `agent_query_events` - Analytics tables
228
- - Automatically used when `SUPABASE_URL` and `SUPABASE_SERVICE_KEY` are configured
229
- - Supports Row Level Security (RLS) for multi-tenant isolation
230
- - Scalable, production-ready with automatic backups
231
 
232
- - **SQLite (Development Fallback)**:
233
- - `data/admin_rules.db` - Admin rules (local)
234
- - `data/analytics.db` - Analytics events (local)
235
- - Used automatically when Supabase credentials are not available
236
- - Perfect for local development and testing
237
 
238
- **Migration**: Use `python migrate_sqlite_to_supabase.py` to copy existing SQLite data to Supabase. See `SUPABASE_SETUP.md` for detailed setup instructions.
 
 
 
 
 
 
 
 
 
239
 
240
  ---
241
 
@@ -269,6 +463,79 @@ See `SUPABASE_SETUP.md` and `SUPABASE_MIGRATION_COMPLETE.md` for detailed instru
269
 
270
  ---
271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
  ## Testing & Diagnostics
273
 
274
  IntegraChat ships with several helper scripts to validate the full stack end-to-end:
@@ -331,17 +598,38 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
331
 
332
  ## Technical Stack
333
 
334
- - **Backend**: FastAPI with async/await for high-performance MCP orchestration
335
- - **Frontend**: Gradio interface with Plotly visualizations + Next.js operator console
336
- - **UI Libraries**: Plotly for interactive charts, Gradio for web interface
337
- - **LLM Integration**: Ollama (local) or Groq (cloud) via configurable backend with streaming support
338
- - **Vector Store**: pgvector (via Supabase) or SQLite embeddings
339
- - **Analytics**: Supabase (production) or SQLite (development) with indexed queries for fast analytics
340
- - **Rules Storage**: Supabase (production) or SQLite (development) with automatic detection and fallback
341
  - **MCP Server**: Unified MCP server (port 8900) exposing all tools via namespaces
342
- - **Database**: PostgreSQL with pgvector extension for RAG embeddings, SQLite for analytics
343
- - **File Processing**: Support for TXT, PDF, DOC, DOCX with server-side text extraction (PyPDF2, python-docx)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
344
  - **Streaming**: Server-Sent Events (SSE) for real-time word-by-word response streaming
 
 
345
 
346
  ## Recent Enhancements
347
 
 
6
 
7
  ---
8
 
9
+ ## πŸ“‹ Table of Contents
10
+
11
+ - [Overview](#overview)
12
+ - [Quick Start](#quick-start)
13
+ - [Features](#features)
14
+ - [Installation & Setup](#installation--setup)
15
+ - [Usage](#usage)
16
+ - [API Endpoints](#api-endpoints)
17
+ - [Architecture](#architecture)
18
+ - [Supabase Setup & Migration](#supabase-setup--migration)
19
+ - [Troubleshooting](#troubleshooting)
20
+ - [Testing & Diagnostics](#testing--diagnostics)
21
+ - [Technical Stack](#technical-stack)
22
+ - [License](#license)
23
+
24
+ ---
25
+
26
  ## Overview
27
 
28
  **IntegraChat** is an enterprise-grade, multi-tenant AI platform that demonstrates the full capabilities of the **Model Context Protocol (MCP)** in a production-style environment. Built with enterprise governance and observability in mind, IntegraChat combines autonomous tool-using agents, RAG retrieval, live web search, and admin compliance under strict tenant isolation.
 
31
 
32
  ---
33
 
34
+ ## πŸš€ Quick Start
35
+
36
+ ### Windows Users
37
+ ```bash
38
+ # 1. Install dependencies
39
+ pip install -r requirements.txt
40
+
41
+ # 2. Configure environment (copy and edit .env)
42
+ cp env.example .env
43
+ # Edit .env with your credentials (Supabase, LLM, etc.)
44
+
45
+ # 3. Start all services
46
+ start.bat
47
+ ```
48
+
49
+ ### Manual Setup
50
+ ```bash
51
+ # 1. Install dependencies
52
+ pip install -r requirements.txt
53
+
54
+ # 2. Configure environment
55
+ cp env.example .env
56
+ # Edit .env with your credentials
57
+
58
+ # 3. Start FastAPI backend (Terminal 1)
59
+ uvicorn backend.api.main:app --port 8000 --reload
60
+
61
+ # 4. Start unified MCP server (Terminal 2)
62
+ python backend/mcp_server/server.py
63
+
64
+ # 5. Start Gradio UI (Terminal 3)
65
+ python app.py
66
+ ```
67
+
68
+ Then access:
69
+ - **Gradio UI**: `http://localhost:7860`
70
+ - **FastAPI Docs**: `http://localhost:8000/docs`
71
+ - **Next.js Frontend** (optional): `cd frontend && npm install && npm run dev` β†’ `http://localhost:3000`
72
+
73
+ ---
74
+
75
  ## Features
76
 
77
  ### Core Capabilities
 
116
 
117
  ---
118
 
119
+ ## Installation & Setup
120
+
121
  ### Prerequisites
122
 
123
+ - **Python 3.10+** with pip
124
+ - **PostgreSQL** (with pgvector extension) or **Supabase** for RAG storage
125
+ - **Supabase** (recommended) or SQLite for admin rules and analytics
126
+ - **Ollama** (local) or **Groq API** credentials for LLM
127
+ - **Google Custom Search API** (optional, for web search):
128
+ - Enable Custom Search API in [Google Cloud Console](https://console.cloud.google.com/)
129
+ - Create API key β†’ set as `GOOGLE_SEARCH_API_KEY` in `.env`
130
+ - Create Programmable Search Engine β†’ set ID as `GOOGLE_SEARCH_CX_ID` in `.env`
 
 
 
131
 
132
+ ### Step-by-Step Installation
133
 
134
+ 1. **Clone and navigate to the project**:
135
  ```bash
136
+ cd IntegraChat
137
  ```
138
 
139
+ 2. **Create and activate virtual environment** (recommended):
140
  ```bash
141
+ # Windows
142
+ python -m venv venv
143
+ venv\Scripts\activate
144
+
145
+ # Linux/Mac
146
+ python3 -m venv venv
147
+ source venv/bin/activate
148
  ```
149
 
150
+ 3. **Install Python dependencies**:
151
+ ```bash
152
+ pip install -r requirements.txt
153
+ ```
 
 
 
 
 
154
 
155
+ 4. **Configure environment variables**:
156
+ ```bash
157
+ cp env.example .env
158
+ # Edit .env with your credentials:
159
+ # - SUPABASE_URL and SUPABASE_SERVICE_KEY (for production storage)
160
+ # - POSTGRESQL_URL (for RAG vector database)
161
+ # - OLLAMA_URL/OLLAMA_MODEL or GROQ_API_KEY (for LLM)
162
+ # - GOOGLE_SEARCH_API_KEY and GOOGLE_SEARCH_CX_ID (optional, for web search)
163
+ ```
164
 
165
+ 5. **Set up Supabase** (recommended for production):
166
+ - Create a Supabase project at [supabase.com](https://supabase.com)
167
+ - Run `supabase_admin_rules_table.sql` in Supabase SQL Editor
168
+ - Run `supabase_analytics_tables.sql` in Supabase SQL Editor
169
+ - Copy your project URL and service role key to `.env`
170
+ - Verify setup: `python verify_supabase_setup.py`
 
171
 
172
+ 6. **Start the services**:
 
 
 
 
 
 
 
 
173
 
174
+ **Option A: Windows Quick Start** (recommended for Windows):
175
+ ```bash
176
+ start.bat
177
+ ```
178
+ This automatically starts:
179
+ - FastAPI backend on port 8000
180
+ - Unified MCP server on port 8900
 
181
 
182
+ **Option B: Manual Start**:
183
+ ```bash
184
+ # Terminal 1: FastAPI backend
185
+ uvicorn backend.api.main:app --port 8000 --reload
186
+
187
+ # Terminal 2: Unified MCP server
188
+ python backend/mcp_server/server.py
189
+ ```
190
 
191
+ 7. **Launch the UI**:
192
 
193
+ **Gradio Interface** (full-featured):
194
+ ```bash
195
+ python app.py
196
+ ```
197
+ Access at `http://localhost:7860`
198
 
199
+ **Next.js Frontend** (optional, modern UI):
200
+ ```bash
201
+ cd frontend
202
+ npm install
203
+ npm run dev
204
+ ```
205
+ Access at `http://localhost:3000`
206
+
207
+ ## Usage
208
+
209
+ ### Gradio Interface (`app.py`)
210
+
211
+ The Gradio UI provides a comprehensive interface with five main tabs:
212
+
213
+ #### 1. **Chat** πŸ’¬
214
+ - Enter your Tenant ID and start chatting with the MCP-powered agent
215
+ - Real-time streaming responses (word-by-word using SSE)
216
+ - Autonomous tool orchestration (RAG, Web, Admin, LLM)
217
+ - Multi-step planning with memory of previous tool outputs
218
+
219
+ #### 2. **Document Ingestion** πŸ“š
220
+ - **Raw Text**: Paste text directly
221
+ - **URL**: Ingest content from web URLs
222
+ - **File Upload**: Upload PDF, DOCX, TXT, or Markdown files
223
+ - Rich metadata support (filename, URL, document ID, custom JSON)
224
+ - View and manage ingested documents
225
+
226
+ #### 3. **Knowledge Base Library** πŸ“–
227
+ - **Statistics Dashboard**: Visual cards showing document counts by type
228
+ - **Interactive Charts**: Plotly pie chart for document type distribution
229
+ - **Semantic Search**: Search knowledge base with relevance scoring
230
+ - **Type Filtering**: Filter by document type (text, PDF, FAQ, link)
231
+ - **Document Management**: View, preview, and delete documents
232
+ - **Auto-refresh**: Lists update automatically after operations
233
+
234
+ #### 4. **Admin Analytics** πŸ“Š
235
+ - **Statistics Cards**: Total queries, active users, red flags, RAG searches
236
+ - **Interactive Bar Charts**:
237
+ - Tool Usage Count (RAG, Web, Admin, LLM)
238
+ - Average Tool Latency (performance metrics)
239
+ - RAG Quality Metrics (hits, scores, recall indicators)
240
+ - **Tool Usage Table**: Detailed performance breakdown
241
+ - **Formatted Summary**: Key metrics in easy-to-read format
242
+ - Click "πŸ”„ Fetch Analytics Snapshot" to load latest data
243
+
244
+ #### 5. **Admin Rules & Compliance** πŸ›‘οΈ
245
+ - **Text Input**: Paste rules one per line (comments starting with # are ignored)
246
+ - **File Upload**: Upload rules from TXT, PDF, DOC, or DOCX files
247
+ - **LLM Enhancement**: Automatic rule enhancement (edge cases, pattern improvements, severity suggestions)
248
+ - **Chunk Processing**: Large rule sets processed in chunks (5 at a time)
249
+ - **Rule-Based Behavior**: Rules checked FIRST - brief responses or blocking based on severity
250
+ - **Streaming Responses**: Real-time word-by-word streaming
251
+ - **Refresh Button**: Update rules table directly
252
+
253
+ > **πŸ’‘ Tip:** Every action requires a Tenant ID. The Tenant ID persists across page refreshes and is managed centrally.
254
+
255
+ ### Next.js Frontend (`frontend/`)
256
+
257
+ The modern Next.js operator console provides dedicated pages for each workflow:
258
+
259
+ | Route | Description |
260
  | --- | --- |
261
+ | `/` | Landing page with hero section and quick access panels |
262
  | `/ingestion` | Data ingestion walkthrough (text/URL/files) with document management |
263
+ | `/chat` | Chat console with MCP agent integration |
264
+ | `/analytics` | Analytics overview with visualizations |
265
+ | `/admin-rules` | Admin rule management interface |
266
+ | `/knowledge-base` | Document library with search, filter, and delete functionality |
267
 
268
  **Key Features:**
269
+ - **Centralized Tenant ID Management** – Global React Context with localStorage persistence
270
+ - **Document Management** – Full CRUD operations for knowledge base
271
+ - **Improved Error Handling** – Clear error messages with retry options
272
+ - **Real-time Updates** – Automatic refresh after operations
273
+ - **Modern UI** – Tailwind CSS with responsive design
274
 
275
+ **To run:**
276
  ```bash
277
  cd frontend
278
  npm install
279
  npm run dev
280
  ```
281
+ Then open `http://localhost:3000`. The tenant ID selector is available in the navbar on all pages.
282
 
283
  ---
284
 
285
  ## API Endpoints
286
 
287
+ All endpoints are served by the FastAPI backend at `http://localhost:8000`. Most endpoints require the `x-tenant-id` header for tenant isolation.
288
+
289
+ > **πŸ“– API Documentation**: Interactive Swagger docs available at `http://localhost:8000/docs` when the backend is running.
290
+
291
  ### Agent Endpoints
292
 
293
+ | Method | Endpoint | Description |
294
  | --- | --- | --- |
295
+ | `POST` | `/agent/message` | Main chat endpoint with `tenant_id`, `message`, optional history |
296
+ | `POST` | `/agent/message/stream` | Streaming chat endpoint using Server-Sent Events (SSE). Returns tokens word-by-word |
297
+ | `POST` | `/agent/debug` | Detailed debugging info: reasoning trace, tool selection, intent classification |
298
+ | `POST` | `/agent/plan` | Tool selection plan without execution (intent, tool scores, planned steps) |
299
 
300
  ### RAG Endpoints
301
 
302
+ | Method | Endpoint | Description |
303
  | --- | --- | --- |
304
+ | `POST` | `/rag/ingest-document` | Ingest document with `source_type`, `content`, metadata. Supports raw text, URLs, PDFs, DOCX, TXT, Markdown |
305
+ | `POST` | `/rag/ingest-file` | Multipart file upload (PDF/DOCX/TXT/MD) with `x-tenant-id` header |
306
+ | `GET` | `/rag/list?tenant_id={id}&limit={n}&offset={n}` | List all documents for a tenant with pagination |
307
+ | `DELETE` | `/rag/delete/{document_id}?tenant_id={id}` | Delete a specific document by ID |
308
+ | `DELETE` | `/rag/delete-all?tenant_id={id}` | Delete all documents for a tenant |
309
+
310
+ **Note:** RAG endpoints support both `x-tenant-id` header and `tenant_id` query parameter.
311
 
312
  ### Admin & Governance Endpoints
313
 
314
+ | Method | Endpoint | Description |
315
  | --- | --- | --- |
316
+ | `GET` | `/admin/rules?detailed=true` | Get all rules (use `detailed=true` for regex/severity metadata) |
317
+ | `POST` | `/admin/rules?enhance=true` | Add single rule with optional `pattern` (regex), `severity`, `description`. Set `enhance=true` for LLM enhancement |
318
+ | `POST` | `/admin/rules/bulk?enhance=true` | Add multiple rules at once (processed in chunks of 5). LLM enhancement applied automatically |
319
+ | `POST` | `/admin/rules/upload-file?enhance=true` | Upload rules from file (TXT, PDF, DOC, DOCX). Text extracted server-side |
320
+ | `DELETE` | `/admin/rules/{rule}` | Delete a specific rule |
321
+ | `GET` | `/admin/violations?days=30&limit=50` | Get red-flag violations with timestamps and confidence scores |
322
+ | `GET` | `/admin/tools/logs?tool_name=rag&days=7` | Get detailed tool usage logs with latency and token counts |
323
+ | `GET/POST/DELETE` | `/admin/tenants` | Tenant management endpoints |
324
+ | `POST` | `/admin/setup/table` | Create admin_rules table in Supabase if it doesn't exist |
325
 
326
  ### Analytics Endpoints
327
 
328
+ | Method | Endpoint | Description |
329
  | --- | --- | --- |
330
+ | `GET` | `/analytics/overview?days=30` | Comprehensive analytics: total queries, tool usage, red-flag count, RAG quality |
331
+ | `GET` | `/analytics/tool-usage?days=30` | Detailed tool usage stats: counts, latency, tokens, success/error rates |
332
+ | `GET` | `/analytics/redflags?limit=50&days=30` | Recent red-flag violations for tenant |
333
+ | `GET` | `/analytics/activity?days=30` | Tenant activity summary: queries, active users, last query timestamp |
334
+ | `GET` | `/analytics/rag-quality?days=30` | RAG quality metrics: avg hits, scores, latency (recall/precision indicators) |
335
+
336
+ ### Request Headers
337
 
338
+ Most endpoints require:
339
+ - `x-tenant-id`: Tenant identifier for multi-tenant isolation
340
+ - `Content-Type: application/json`: For POST requests with JSON payloads
341
+
342
+ ### Example Request
343
+
344
+ ```bash
345
+ curl -X POST http://localhost:8000/agent/message \
346
+ -H "Content-Type: application/json" \
347
+ -H "x-tenant-id: tenant123" \
348
+ -d '{
349
+ "message": "What is our refund policy?",
350
+ "tenant_id": "tenant123"
351
+ }'
352
+ ```
353
 
354
  ---
355
 
356
+ ## Architecture
357
+
358
+ ### System Overview
359
+
360
+ IntegraChat follows a modular architecture with clear separation of concerns:
361
+
362
+ ```
363
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
364
+ β”‚ Frontend UI β”‚ (Gradio + Next.js)
365
+ β”‚ Port 7860/3000 β”‚
366
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
367
+ β”‚
368
+ β–Ό
369
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
370
+ β”‚ FastAPI Backendβ”‚ (API Gateway)
371
+ β”‚ Port 8000 β”‚
372
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
373
+ β”‚
374
+ β”œβ”€β”€β–Ί Unified MCP Server (Port 8900)
375
+ β”‚ β”œβ”€β”€ RAG Tools (search, ingest, list, delete)
376
+ β”‚ β”œβ”€β”€ Web Tools (search)
377
+ β”‚ └── Admin Tools (rules, violations)
378
+ β”‚
379
+ β”œβ”€β”€β–Ί PostgreSQL/Supabase (RAG Vector Store)
380
+ β”œβ”€β”€β–Ί Supabase/SQLite (Rules & Analytics)
381
+ └──► LLM Backend (Ollama/Groq)
382
+ ```
383
 
384
  ### Enterprise-Grade Features
385
 
386
+ 1. **Autonomous Multi-Step Planning**: LLM-powered planning determines optimal tool sequences with memory of previous tool outputs in multi-step workflows.
387
 
388
  2. **Regex-Based Governance**: Admin rules support regex patterns with fallback to keyword matching and semantic similarity scoring for flexible policy enforcement.
389
 
390
+ 3. **Comprehensive Analytics**: All tool usage, RAG searches, LLM calls, and red-flag violations are logged with indexed queries for fast analytics retrieval.
391
 
392
+ 4. **Enhanced RAG Pipeline**: Documents chunked optimally (400-600 tokens) and enriched with metadata (source URL, timestamp, document type) for better retrieval.
393
 
394
+ 5. **Structured Error Handling**: All errors logged with context, with graceful fallbacks (e.g., RAG fails β†’ LLM-only, web fails β†’ skip web).
395
 
396
+ ### Data Storage Architecture
397
+
398
+ IntegraChat uses **dual-backend storage** with automatic fallback for production flexibility:
399
+
400
+ #### Supabase (Production/Preferred)
401
+
402
+ **When to use:** Production deployments, multi-user environments, scalable applications
403
+
404
+ **Storage:**
405
+ - `admin_rules` - Admin rules with regex patterns and severity levels
406
+ - `tool_usage_events` - Tool invocation logs with latency and token tracking
407
+ - `redflag_violations` - Red-flag violation events with timestamps
408
+ - `rag_search_events` - RAG search metrics and quality indicators
409
+ - `agent_query_events` - Agent query logs and analytics
410
+
411
+ **Features:**
412
+ - Row Level Security (RLS) for multi-tenant isolation
413
+ - Automatic backups and scaling
414
+ - Real-time capabilities
415
+ - Production-ready infrastructure
416
 
417
+ **Setup:** Configure `SUPABASE_URL` and `SUPABASE_SERVICE_KEY` in `.env`
418
 
419
+ #### SQLite (Development Fallback)
 
 
 
 
 
420
 
421
+ **When to use:** Local development, testing, single-user scenarios
 
 
 
 
422
 
423
+ **Storage:**
424
+ - `data/admin_rules.db` - Admin rules (local file)
425
+ - `data/analytics.db` - Analytics events (local file)
426
+
427
+ **Features:**
428
+ - Zero configuration required
429
+ - Perfect for local development
430
+ - Automatic fallback when Supabase not configured
431
+
432
+ **Migration:** Use `python migrate_sqlite_to_supabase.py` to migrate existing SQLite data to Supabase. See `SUPABASE_SETUP.md` for detailed instructions.
433
 
434
  ---
435
 
 
463
 
464
  ---
465
 
466
+ ## Troubleshooting
467
+
468
+ ### Common Issues
469
+
470
+ #### Backend Not Starting
471
+ - **Issue**: FastAPI backend fails to start
472
+ - **Solution**:
473
+ - Check if port 8000 is already in use: `netstat -ano | findstr :8000` (Windows) or `lsof -i :8000` (Linux/Mac)
474
+ - Verify Python virtual environment is activated
475
+ - Check `.env` file exists and has required variables
476
+ - Review error logs for missing dependencies
477
+
478
+ #### MCP Server Connection Errors
479
+ - **Issue**: "Could not connect to MCP server" errors
480
+ - **Solution**:
481
+ - Ensure unified MCP server is running: `python backend/mcp_server/server.py`
482
+ - Check MCP server is on port 8900 (default)
483
+ - Verify `MCP_SERVER_ID` in `.env` matches server configuration
484
+ - Check firewall settings if running on different machines
485
+
486
+ #### RAG Search Not Returning Results
487
+ - **Issue**: RAG searches return no results despite ingested documents
488
+ - **Solution**:
489
+ - Check similarity threshold (default 0.3) - try lowering to 0.2 or 0.1
490
+ - Verify documents exist: `GET /rag/list?tenant_id={id}`
491
+ - Ensure tenant_id matches between ingestion and search
492
+ - Check PostgreSQL/pgvector connection and vector extension
493
+ - Review MCP server logs for search metrics
494
+
495
+ #### Supabase Configuration Issues
496
+ - **Issue**: Data still going to SQLite instead of Supabase
497
+ - **Solution**:
498
+ - Verify `SUPABASE_URL` and `SUPABASE_SERVICE_KEY` in `.env` (no quotes, no spaces)
499
+ - Use **service_role** key (not anon key) from Supabase Dashboard
500
+ - Run `python verify_supabase_setup.py` to check configuration
501
+ - Ensure tables exist: run SQL scripts in Supabase SQL Editor
502
+ - Check FastAPI startup logs for backend detection messages
503
+
504
+ #### LLM Connection Errors
505
+ - **Issue**: Agent responses fail with LLM errors
506
+ - **Solution**:
507
+ - For Ollama: Ensure Ollama is running (`ollama serve`)
508
+ - Check `OLLAMA_URL` and `OLLAMA_MODEL` in `.env`
509
+ - For Groq: Verify `GROQ_API_KEY` is set correctly
510
+ - Check `LLM_BACKEND` setting (ollama or groq)
511
+ - Test LLM connection: `curl http://localhost:11434/api/tags` (Ollama)
512
+
513
+ #### Document Ingestion Failures
514
+ - **Issue**: File uploads or document ingestion fails
515
+ - **Solution**:
516
+ - Check file size limits (default may be 10MB)
517
+ - Verify file format is supported (PDF, DOCX, TXT, MD)
518
+ - Ensure tenant_id is provided in request
519
+ - Check backend logs for specific error messages
520
+ - Verify PostgreSQL connection for RAG storage
521
+
522
+ #### Tenant Isolation Issues
523
+ - **Issue**: Documents or data leaking between tenants
524
+ - **Solution**:
525
+ - Run `python verify_tenant_isolation.py` to test isolation
526
+ - Check database queries include `WHERE tenant_id = ...` filters
527
+ - Verify tenant ID normalization is working correctly
528
+ - Review `python check_rag_database.py` output for tenant IDs
529
+
530
+ ### Getting Help
531
+
532
+ 1. **Check Logs**: Review FastAPI and MCP server logs for detailed error messages
533
+ 2. **Run Diagnostics**: Use helper scripts in the Testing & Diagnostics section
534
+ 3. **Verify Configuration**: Run `python verify_supabase_setup.py` and check `.env` file
535
+ 4. **Review Documentation**: See `backend/README.md` for backend-specific issues
536
+
537
+ ---
538
+
539
  ## Testing & Diagnostics
540
 
541
  IntegraChat ships with several helper scripts to validate the full stack end-to-end:
 
598
 
599
  ## Technical Stack
600
 
601
+ ### Backend
602
+ - **Framework**: FastAPI with async/await for high-performance MCP orchestration
 
 
 
 
 
603
  - **MCP Server**: Unified MCP server (port 8900) exposing all tools via namespaces
604
+ - **API**: RESTful API with Server-Sent Events (SSE) for streaming responses
605
+ - **LLM Integration**:
606
+ - Ollama (local, default) - `http://localhost:11434`
607
+ - Groq (cloud) - via API key
608
+ - Configurable backend with streaming support
609
+
610
+ ### Frontend
611
+ - **Gradio UI**: Full-featured interface with Plotly visualizations (`app.py`)
612
+ - **Next.js Console**: Modern React-based operator console (`frontend/`)
613
+ - **UI Libraries**:
614
+ - Plotly for interactive charts and visualizations
615
+ - Tailwind CSS for modern styling (Next.js)
616
+ - React 19 with TypeScript
617
+
618
+ ### Data Storage
619
+ - **RAG Vector Store**: PostgreSQL with pgvector extension (via Supabase or direct connection)
620
+ - **Analytics**: Supabase (production) or SQLite (development) with indexed queries
621
+ - **Rules Storage**: Supabase (production) or SQLite (development) with automatic fallback
622
+ - **Database**: PostgreSQL for RAG embeddings, Supabase/SQLite for analytics and rules
623
+
624
+ ### File Processing
625
+ - **Supported Formats**: TXT, PDF, DOC, DOCX, Markdown
626
+ - **Libraries**: PyPDF2, python-docx for server-side text extraction
627
+ - **Metadata**: Rich metadata support (source URL, timestamp, document type)
628
+
629
+ ### Communication
630
  - **Streaming**: Server-Sent Events (SSE) for real-time word-by-word response streaming
631
+ - **Protocol**: Model Context Protocol (MCP) for tool communication
632
+ - **HTTP**: RESTful endpoints with JSON payloads
633
 
634
  ## Recent Enhancements
635