Soumsa commited on
Commit
b8a2d0d
Β·
verified Β·
1 Parent(s): c28e126

You are an expert full-stack AI engineer. Build a production-ready AI Customer Support Bot with the following specifications:

Browse files

## ARCHITECTURE OVERVIEW
Create a modern 3-tier architecture:
1. Frontend: Next.js 14 + TypeScript + Tailwind CSS + shadcn/ui
2. Backend: FastAPI (Python 3.11+) + LangGraph for orchestration
3. Data Layer: PostgreSQL + Redis + Pinecone (vector store)

## PROJECT STRUCTURE

### Backend (FastAPI)
backend/
β”œβ”€β”€ app/
β”‚ β”œβ”€β”€ api/v1/
β”‚ β”‚ β”œβ”€β”€ chat.py # WebSocket + REST endpoints
β”‚ β”‚ β”œβ”€β”€ sessions.py # Session CRUD
β”‚ β”‚ └── analytics.py # Dashboard metrics
β”‚ β”œβ”€β”€ agents/
β”‚ β”‚ β”œβ”€β”€ orchestrator.py # LangGraph state machine
β”‚ β”‚ β”œβ”€β”€ faq_agent.py # RAG-based FAQ retrieval
β”‚ β”‚ β”œβ”€β”€ action_agent.py # Tool-calling agent
β”‚ β”‚ └── escalation.py # Human handoff logic
β”‚ β”œβ”€β”€ services/
β”‚ β”‚ β”œβ”€β”€ llm_service.py # OpenAI/Anthropic abstraction
β”‚ β”‚ β”œβ”€β”€ rag_service.py # Pinecone vector search
β”‚ β”‚ └── memory.py # Redis session management
β”‚ β”œβ”€β”€ models/
β”‚ β”‚ β”œβ”€β”€ database.py # SQLAlchemy models
β”‚ β”‚ └── schemas.py # Pydantic validation
β”‚ └── core/
β”‚ β”œβ”€β”€ config.py # Environment variables
β”‚ └── prompts.py # LLM prompt templates

### Frontend (Next.js)
frontend/
β”œβ”€β”€ app/
β”‚ β”œβ”€β”€ page.tsx # Main chat interface
β”‚ └── api/chat/route.ts # API proxy
β”œβ”€β”€ components/
β”‚ β”œβ”€β”€ chat/
β”‚ β”‚ β”œβ”€β”€ ChatWindow.tsx
β”‚ β”‚ β”œβ”€β”€ MessageList.tsx
β”‚ β”‚ β”œβ”€β”€ ActionApprovalModal.tsx
β”‚ β”‚ └── EscalationBanner.tsx
β”‚ └── ui/ # shadcn components
β”œβ”€β”€ hooks/
β”‚ β”œβ”€β”€ useChat.ts # Chat state management
β”‚ └── useWebSocket.ts # Real-time connection
└── lib/
└── api.ts # Backend API client

## DATABASE SCHEMA (PostgreSQL)
```sql
-- Core tables
CREATE TABLE sessions (
session_id UUID PRIMARY KEY,
user_id VARCHAR(255),
status VARCHAR(50) DEFAULT 'active',
escalation_reason TEXT,
created_at TIMESTAMP DEFAULT NOW(),
metadata JSONB
);

CREATE TABLE messages (
message_id UUID PRIMARY KEY,
session_id UUID REFERENCES sessions(session_id),
role VARCHAR(20) NOT NULL,
content TEXT NOT NULL,
confidence_score FLOAT,
matched_faq_id VARCHAR(255),
timestamp TIMESTAMP DEFAULT NOW(),
tool_calls JSONB
);

CREATE TABLE faqs (
faq_id VARCHAR(255) PRIMARY KEY,
question TEXT NOT NULL,
answer TEXT NOT NULL,
category VARCHAR(100),
keywords TEXT[],
usage_count INT DEFAULT 0,
is_active BOOLEAN DEFAULT TRUE
);

CREATE TABLE escalation_queue (
queue_id UUID PRIMARY KEY,
session_id UUID REFERENCES sessions(session_id),
priority VARCHAR(20) DEFAULT 'normal',
reason TEXT,
status VARCHAR(50) DEFAULT 'pending',
created_at TIMESTAMP DEFAULT NOW()
);

CREATE TABLE feedback (
feedback_id UUID PRIMARY KEY,
message_id UUID REFERENCES messages(message_id),
rating INT CHECK (rating >= 1 AND rating <= 5),
feedback_text TEXT,
created_at TIMESTAMP DEFAULT NOW()
);
KEY IMPLEMENTATION REQUIREMENTS
1. LangGraph Multi-Agent Orchestrator
Create a state machine with these agents:

Intent Classifier: Route queries to FAQ/Action/Escalation
FAQ Agent: Use Pinecone for semantic search (threshold: 0.75)
Action Agent: Tool-calling with approval mechanism (interrupt before execution)
Escalation Agent: Trigger on: low confidence (3+ messages), explicit request, sentiment analysis (anger/frustration), repeated failures

State definition:
pythonclass AgentState(TypedDict):
messages: Annotated[list, add_messages]
session_id: str
user_info: dict
current_intent: str
confidence_score: float
escalation_triggered: bool
pending_actions: list
2. RAG-Based FAQ System

Use OpenAI text-embedding-3-small for embeddings
Store in Pinecone with metadata filtering
Hybrid search: semantic similarity + keyword matching
Return top 5 results, filter by confidence threshold
Generate answers using Claude 3.5 Sonnet with FAQ context

3. Real-Time Chat (WebSocket)

Use Socket.IO for bidirectional communication
Events: 'message', 'typing_start', 'action_approval_needed', 'escalation'
Maintain session state in Redis (TTL: 30 minutes)
Graceful fallback to REST API if WebSocket fails

4. Action Approval Flow
For sensitive operations (data updates, bookings):

Agent proposes action β†’ sends to frontend
Display modal with action details and impacts
User approves/rejects β†’ resume LangGraph execution
Use LangGraph's interrupt_before mechanism

5. Escalation Logic
Trigger escalation when:

Confidence score < 0.6 for 3 consecutive messages
User says: "speak to agent", "human", "representative"
Sentiment analysis shows anger (intensity > 0.7)
Agent retries same query 3+ times without resolution

Actions on escalation:

Create escalation_queue record with priority
Generate conversation summary for human agent
Display banner with estimated wait time
Disable bot input, enable "Cancel Escalation" option

6. Session Memory Management

Store in Redis with session_id as key
Include: last 10 messages, user_info, conversation_summary, action_history
Compress old sessions to PostgreSQL after 30 minutes
Use LangChain's ConversationSummaryMemory for long conversations

7. Frontend Requirements

Modern, clean UI with message bubbles (user: right/blue, bot: left/gray)
Typing indicator with animated dots
Action approval modal with clear description and approve/reject buttons
Escalation banner showing queue position and wait time
Auto-scroll to latest message
Input validation (max 500 chars, rate limit: 10 msg/min)
Responsive design (mobile-first)

8. Analytics Dashboard (Admin)
Display metrics:

Total conversations, resolution rate, escalation rate
Average confidence score, response time
Top FAQ categories, unresolved queries
User satisfaction (thumbs up/down on messages)
Escalation reasons breakdown (pie chart)

9. LLM Configuration
Primary LLM: Claude 3.5 Sonnet (complex queries, actions)
Secondary LLM: GPT-4o-mini (intent classification, fast responses)
Embeddings: OpenAI text-embedding-3-small
Temperature: 0 for actions, 0.7 for conversational responses
System prompt template:
You are a helpful customer support assistant for [COMPANY].

Guidelines:
- Be concise, friendly, and professional
- Use provided FAQ context when available
- Always explain actions before executing
- If unsure, admit limitations and offer human escalation
- Maintain conversation context across messages

Current user: {user_info}
FAQ context: {faq_context}
Conversation history: {conversation_history}
DELIVERABLES

GitHub Repository with:

Complete source code
Docker Compose setup
Environment variable template (.env.example)
README with setup instructions
API documentation (auto-generated with FastAPI)


Database Migration Scripts:

Alembic migrations for PostgreSQL
Seed script for sample FAQs


Demo Features:

Sample FAQ dataset (50+ questions)
Mock escalation queue
Test user with conversation history


Documentation:

Architecture diagram
API endpoint documentation
Prompt engineering guide
Deployment instructions (Docker/Railway/Vercel)



TESTING REQUIREMENTS

Unit tests for agents (pytest)
Integration tests for API endpoints
E2E tests for chat flow (Playwright)
Load testing for WebSocket (100 concurrent users)

DEPLOYMENT STRATEGY

Backend: Docker container on Railway/Render
Frontend: Vercel with edge functions
Database: Supabase (PostgreSQL) or Railway
Vector Store: Pinecone (free tier)
Monitoring: Sentry for errors, PostHog for analytics

CRITICAL IMPLEMENTATION NOTES

Never use localStorage in artifacts - use React state
Implement exponential backoff for LLM rate limits
Sanitize all user inputs to prevent prompt injection
Log all escalations for quality assurance
Add CORS middleware for frontend-backend communication
Use connection pooling for database (SQLAlchemy with pool_size=10)
Implement graceful degradation if vector store is unavailable
Cache frequent FAQ queries in Redis (TTL: 1 hour)

SUCCESS METRICS

70%+ queries resolved by AI without escalation
< 3 second average response time
90%+ uptime
4+ star average user satisfaction
< 5% error rate

Build this system step-by-step, starting with:

Database schema and models
Basic FastAPI app with health check
Simple LangGraph orchestrator
FAQ agent with mock data
Next.js chat interface
WebSocket integration
Action approval flow
Escalation logic
Analytics dashboard
Testing and deployment

Use best practices: type hints, async/await, error handling, logging, and comprehensive docstrings.

---

## **πŸš€ QUICK START COMMAND**

Use this shortened version if you want to start immediately:
Build an AI customer support chatbot:

Backend: FastAPI + LangGraph + Pinecone (RAG) + PostgreSQL + Redis
Frontend: Next.js + TypeScript + Tailwind + WebSocket
Features: FAQ retrieval (RAG), multi-agent orchestration, action approval (human-in-loop), escalation logic, session memory, analytics
Database: sessions, messages, faqs, escalation_queue, feedback tables
Agents: IntentClassifier β†’ FAQ/Action/Escalation agents with LangGraph state machine
Escalation triggers: confidence<0.6, explicit request, sentiment analysis, retry count>3
Use Claude 3.5 Sonnet for main LLM, GPT-4o-mini for classification
Real-time chat with Socket.IO, action approval modals, typing indicators
Deliver: Full repo with Docker setup, README, 50+ sample FAQs, API docs
Start with database schema, then FastAPI setup, then LangGraph orchestrator, then Next.js UI.


---

## **πŸ“¦ TECH STACK SUMMARY**

| Layer | Technology | Purpose |
|-------|-----------|---------|
| **Frontend** | Next.js 14 + TypeScript | Modern React framework |
| | Tailwind CSS + shadcn/ui | Styling & components |
| | Socket.IO client | Real-time communication |
| **Backend** | FastAPI (Python 3.11+) | Async REST API |
| | LangGraph | Agent orchestration |
| | LangChain | LLM abstraction |
| **AI/ML**

Files changed (2) hide show
  1. README.md +8 -5
  2. index.html +231 -18
README.md CHANGED
@@ -1,10 +1,13 @@
1
  ---
2
- title: Supportbot Supreme Ai
3
- emoji: πŸ‘€
4
- colorFrom: gray
5
- colorTo: yellow
6
  sdk: static
7
  pinned: false
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
1
  ---
2
+ title: SupportBot Supreme AI πŸ€–
3
+ colorFrom: purple
4
+ colorTo: red
5
+ emoji: 🐳
6
  sdk: static
7
  pinned: false
8
+ tags:
9
+ - deepsite-v3
10
  ---
11
 
12
+ # Welcome to your new DeepSite project!
13
+ This project was created with [DeepSite](https://deepsite.hf.co).
index.html CHANGED
@@ -1,19 +1,232 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>SupportBot Supreme AI</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <script src="https://unpkg.com/feather-icons"></script>
9
+ <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
10
+ <style>
11
+ .chat-container {
12
+ height: calc(100vh - 160px);
13
+ }
14
+ .typing {
15
+ display: inline-block;
16
+ }
17
+ .typing-dot {
18
+ display: inline-block;
19
+ width: 8px;
20
+ height: 8px;
21
+ border-radius: 50%;
22
+ margin-right: 3px;
23
+ background: #3b82f6;
24
+ animation: typingAnimation 1.4s infinite ease-in-out;
25
+ }
26
+ .typing-dot:nth-child(1) {
27
+ animation-delay: 0s;
28
+ }
29
+ .typing-dot:nth-child(2) {
30
+ animation-delay: 0.2s;
31
+ }
32
+ .typing-dot:nth-child(3) {
33
+ animation-delay: 0.4s;
34
+ margin-right: 0;
35
+ }
36
+ @keyframes typingAnimation {
37
+ 0%, 60%, 100% { transform: translateY(0); }
38
+ 30% { transform: translateY(-5px); }
39
+ }
40
+ .message-user {
41
+ border-radius: 18px 18px 0 18px;
42
+ }
43
+ .message-bot {
44
+ border-radius: 18px 18px 18px 0;
45
+ }
46
+ .scrollbar-hide::-webkit-scrollbar {
47
+ display: none;
48
+ }
49
+ </style>
50
+ </head>
51
+ <body class="bg-gray-50">
52
+ <div class="max-w-4xl mx-auto p-4 h-screen flex flex-col">
53
+ <!-- Header -->
54
+ <header class="bg-white shadow-sm rounded-lg p-4 mb-4 flex items-center justify-between">
55
+ <div class="flex items-center space-x-3">
56
+ <div class="w-10 h-10 bg-blue-500 rounded-full flex items-center justify-center">
57
+ <i data-feather="message-circle" class="text-white"></i>
58
+ </div>
59
+ <h1 class="text-xl font-bold text-gray-800">SupportBot Supreme</h1>
60
+ </div>
61
+ <div class="flex items-center space-x-2">
62
+ <span class="px-3 py-1 bg-blue-100 text-blue-800 text-sm rounded-full">AI Assistant</span>
63
+ <button class="p-2 rounded-full hover:bg-gray-100">
64
+ <i data-feather="settings" class="text-gray-500"></i>
65
+ </button>
66
+ </div>
67
+ </header>
68
+
69
+ <!-- Chat Container -->
70
+ <div class="flex-1 bg-white rounded-lg shadow-sm overflow-hidden flex flex-col">
71
+ <!-- Messages Area -->
72
+ <div id="chat-messages" class="chat-container p-4 overflow-y-auto scrollbar-hide space-y-3">
73
+ <!-- Welcome Message -->
74
+ <div class="flex justify-start">
75
+ <div class="bg-blue-50 text-blue-800 p-4 message-bot max-w-[80%]">
76
+ <p class="font-medium">Hello! πŸ‘‹ I'm your AI support assistant. How can I help you today?</p>
77
+ <p class="text-sm mt-2 opacity-80">I can answer questions about your account, orders, and more.</p>
78
+ </div>
79
+ </div>
80
+ </div>
81
+
82
+ <!-- Input Area -->
83
+ <div class="border-t p-4 bg-gray-50">
84
+ <div class="flex items-end space-x-2">
85
+ <div class="flex-1 relative">
86
+ <textarea
87
+ id="message-input"
88
+ class="w-full p-3 border rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 resize-none"
89
+ placeholder="Type your message..."
90
+ rows="1"
91
+ style="min-height: 44px; max-height: 120px;"></textarea>
92
+ <button class="absolute right-3 bottom-3 text-gray-400 hover:text-blue-500">
93
+ <i data-feather="paperclip"></i>
94
+ </button>
95
+ </div>
96
+ <button id="send-button" class="bg-blue-500 hover:bg-blue-600 text-white p-3 rounded-lg transition">
97
+ <i data-feather="send"></i>
98
+ </button>
99
+ </div>
100
+ <p class="text-xs text-gray-500 mt-2">SupportBot may make mistakes. Consider checking important information.</p>
101
+ </div>
102
+ </div>
103
+
104
+ <!-- Footer -->
105
+ <footer class="text-center text-xs text-gray-500 mt-4">
106
+ <p>Β© 2023 SupportBot Supreme AI. All rights reserved.</p>
107
+ </footer>
108
+ </div>
109
+
110
+ <!-- Typing Indicator Template -->
111
+ <template id="typing-template">
112
+ <div class="flex justify-start" id="typing-indicator">
113
+ <div class="bg-blue-50 text-blue-800 p-4 message-bot">
114
+ <div class="typing">
115
+ <span class="typing-dot"></span>
116
+ <span class="typing-dot"></span>
117
+ <span class="typing-dot"></span>
118
+ </div>
119
+ </div>
120
+ </div>
121
+ </template>
122
+
123
+ <script>
124
+ feather.replace();
125
+
126
+ // Chat simulation
127
+ const messageInput = document.getElementById('message-input');
128
+ const sendButton = document.getElementById('send-button');
129
+ const chatMessages = document.getElementById('chat-messages');
130
+ const typingTemplate = document.getElementById('typing-template');
131
+
132
+ // Auto-resize textarea
133
+ messageInput.addEventListener('input', function() {
134
+ this.style.height = 'auto';
135
+ this.style.height = (this.scrollHeight) + 'px';
136
+ });
137
+
138
+ // Sample FAQ responses
139
+ const faqResponses = {
140
+ "password": "To reset your password, click 'Forgot Password' on the login page and follow the instructions sent to your email.",
141
+ "hours": "Our support team is available Monday to Friday, 9AM to 6PM (EST).",
142
+ "order": "You can track your order in your account dashboard under 'My Orders'.",
143
+ "return": "We accept returns within 30 days of purchase. Please visit our Returns Center for more details.",
144
+ "contact": "You can reach our support team at support@example.com or call us at (555) 123-4567."
145
+ };
146
+
147
+ // Keywords for FAQ matching
148
+ const faqKeywords = {
149
+ "password": ["password", "reset", "forgot", "login"],
150
+ "hours": ["hours", "time", "available", "open", "close"],
151
+ "order": ["order", "track", "delivery", "ship", "status"],
152
+ "return": ["return", "refund", "exchange", "policy"],
153
+ "contact": ["contact", "speak", "human", "agent", "phone", "call", "email"]
154
+ };
155
+
156
+ // Send message function
157
+ function sendMessage() {
158
+ const message = messageInput.value.trim();
159
+ if (message === '') return;
160
+
161
+ // Add user message to chat
162
+ addMessage(message, 'user');
163
+ messageInput.value = '';
164
+ messageInput.style.height = 'auto';
165
+
166
+ // Show typing indicator
167
+ const typingIndicator = typingTemplate.content.cloneNode(true);
168
+ chatMessages.appendChild(typingIndicator);
169
+ chatMessages.scrollTop = chatMessages.scrollHeight;
170
+
171
+ // Simulate AI response after delay
172
+ setTimeout(() => {
173
+ // Remove typing indicator
174
+ document.getElementById('typing-indicator')?.remove();
175
+
176
+ // Check for escalation keywords
177
+ if (message.toLowerCase().includes('human') ||
178
+ message.toLowerCase().includes('agent') ||
179
+ message.toLowerCase().includes('representative')) {
180
+ addMessage("I'll connect you with a human support agent. Please wait while I transfer you...", 'bot');
181
+ setTimeout(() => {
182
+ addMessage("All our agents are currently busy. Your estimated wait time is 5-7 minutes. Would you like to leave a callback request instead?", 'bot');
183
+ }, 2000);
184
+ return;
185
+ }
186
+
187
+ // Find matching FAQ
188
+ let response = "I'm sorry, I couldn't find information about that. Could you please provide more details?";
189
+ for (const [key, keywords] of Object.entries(faqKeywords)) {
190
+ if (keywords.some(keyword => message.toLowerCase().includes(keyword))) {
191
+ response = faqResponses[key];
192
+ break;
193
+ }
194
+ }
195
+
196
+ // Add bot response
197
+ addMessage(response, 'bot');
198
+ }, 1000 + Math.random() * 2000);
199
+ }
200
+
201
+ // Add message to chat
202
+ function addMessage(text, sender) {
203
+ const messageDiv = document.createElement('div');
204
+ messageDiv.className = `flex justify-${sender === 'user' ? 'end' : 'start'}`;
205
+
206
+ const messageContent = document.createElement('div');
207
+ messageContent.className = sender === 'user'
208
+ ? 'bg-blue-500 text-white p-4 message-user max-w-[80%]'
209
+ : 'bg-blue-50 text-blue-800 p-4 message-bot max-w-[80%]';
210
+ messageContent.textContent = text;
211
+
212
+ messageDiv.appendChild(messageContent);
213
+ chatMessages.appendChild(messageDiv);
214
+ chatMessages.scrollTop = chatMessages.scrollHeight;
215
+ }
216
+
217
+ // Event listeners
218
+ sendButton.addEventListener('click', sendMessage);
219
+ messageInput.addEventListener('keypress', (e) => {
220
+ if (e.key === 'Enter' && !e.shiftKey) {
221
+ e.preventDefault();
222
+ sendMessage();
223
+ }
224
+ });
225
+
226
+ // Sample messages
227
+ setTimeout(() => {
228
+ addMessage("Try asking me about password reset, business hours, order tracking, returns policy, or contact information!", 'bot');
229
+ }, 1500);
230
+ </script>
231
+ </body>
232
  </html>