<<<<<<< HEAD
=======
A comprehensive AI-powered customer support chatbot built with LangChain, Flask, and FAISS. This system uses Retrieval-Augmented Generation (RAG) to provide accurate, context-aware responses to customer queries.
- Features
- Architecture
- Tech Stack
- Installation
- Configuration
- Usage
- Project Structure
- Testing Examples
- Analytics Dashboard
- API Endpoints
- Troubleshooting
- Contributing
- Multi-intent recognition: Billing, Technical, Account, Complaints, General
- Entity extraction: Account numbers, dates, emails, product names
- Confidence scoring: Provides confidence levels for each classification
- Multi-intent handling: Detects and handles multiple intents in single message
- FAISS vector store: Fast similarity search for document retrieval
- Semantic search: Understanding context beyond keyword matching
- Document chunking: Efficient text splitting for better retrieval
- Source attribution: Shows which documents were used for answers
- Multiple knowledge domains: Billing, Technical, Account, General FAQs
- Multi-turn conversations: Maintains context across messages
- Session management: Separate conversation history per user
- Memory integration: Uses LangChain's ConversationBufferMemory
- Clarifying questions: Asks for clarification when needed
- Graceful fallbacks: Offers human agent connection when uncertain
- Real-time metrics: Query count, response times, satisfaction scores
- Intent distribution: Tracks which intents are most common
- User feedback: Star rating system for responses
- Performance monitoring: Response time analytics
- Continuous learning: Data collection for model improvement
βββββββββββββββ
β User UI β
β (Browser) β
ββββββββ¬βββββββ
β
β
ββββββββββββββββββββββββββββββββββββββββ
β Flask Application β
β ββββββββββββββββββββββββββββββββββ β
β β Intent Classification β β
β β - Pattern Matching β β
β β - Entity Extraction β β
β β - Confidence Scoring β β
β ββββββββββββββββββββββββββββββββββ β
β β
β ββββββββββββββββββββββββββββββββββ β
β β RAG Pipeline (LangChain) β β
β β ββββββββββββββββββββββββββββ β β
β β β Query Processing β β β
β β ββββββββββββββββββββββββββββ β β
β β ββββββββββββββββββββββββββββ β β
β β β Vector Search (FAISS) β β β
β β β - Semantic Retrieval β β β
β β β - Top-K Documents β β β
β β ββββββββββββββββββββββββββββ β β
β β ββββββββββββββββββββββββββββ β β
β β β LLM Generation β β β
β β β - OpenAI / Gemini β β β
β β β β β β
β β ββββββββββββββββββββββββββββ β β
β ββββββββββββββββββββββββββββββββββ β
β β
β ββββββββββββββββββββββββββββββββββ β
β β Conversation Memory β β
β β - Session Management β β
β β - Context Tracking β β
β ββββββββββββββββββββββββββββββββββ β
β β
β ββββββββββββββββββββββββββββββββββ β
β β Analytics Engine β β
β β - Metrics Collection β β
β β - Performance Tracking β β
β ββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββ
β β
β β
ββββββββββββββββββ ββββββββββββββββ
β FAISS Index β β Analytics β
β (Vector DB) β β JSON Store β
ββββββββββββββββββ ββββββββββββββββ
-
Backend: Flask (Python web framework)
-
LLM Framework: LangChain (Orchestration)
-
Vector Database: FAISS (Facebook AI Similarity Search)
-
Embeddings: HuggingFace Sentence Transformers
-
LLM Options:
- Google Gemini Pro (Free tier available)
-
Frontend: HTML5, CSS3, Vanilla JavaScript
-
Session Management: Flask Sessions
- Python 3.8+
- pip package manager
- Git
git clone <your-repository-url>
cd customer-support-chatbot
# On Windows
python -m venv venv
venv\Scripts\activate
# On macOS/Linux
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
mkdir -p knowledge_base templates
Create .txt
files in the knowledge_base/
directory:
billing.txt
technical.txt
account.txt
general.txt
(Sample documents are provided in the repository)
python ingest_documents.py
This will:
- Load all documents from
knowledge_base/
- Split them into chunks
- Create embeddings
- Build and save FAISS vector store
Expected output:
Loading documents...
Loaded: billing.txt
Loaded: technical.txt
Loaded: account.txt
Loaded: general.txt
Loaded 4 documents
Splitting documents into chunks...
Created 152 chunks
Creating embeddings (this may take a few minutes)...
Creating FAISS vector store...
Saving vector store...
β Vector store created and saved successfully!
Copy .env.example
to .env
:
cp .env.example .env
Edit .env
with your configuration:
MODEL_TYPE=openai
OPENAI_API_KEY=sk-your-api-key-here
OPENAI_MODEL=gpt-3.5-turbo
SECRET_KEY=your-random-secret-key
MODEL_TYPE=gemini
GOOGLE_API_KEY=your-google-api-key-here
GEMINI_MODEL=gemini-pro
SECRET_KEY=your-random-secret-key
Get free API key: https://makersuite.google.com/app/apikey
MODEL_TYPE=ollama
OLLAMA_MODEL=llama2
OLLAMA_BASE_URL=http://localhost:11434
SECRET_KEY=your-random-secret-key
First install Ollama: https://ollama.ai/
Then run: ollama pull llama2
python app.py
The server will start at http://localhost:5000
Open your browser and navigate to:
http://localhost:5000
customer-support-chatbot/
βββ app.py # Main Flask application
βββ ingest_documents.py # Knowledge base ingestion script
βββ requirements.txt # Python dependencies
βββ .env.example # Environment variables template
βββ .env # Your environment variables (create this)
βββ README.md # This file
βββ analytics.json # Analytics data storage
βββ templates/
β βββ index.html # Frontend UI
βββ knowledge_base/ # Knowledge base documents
β βββ billing.txt
β βββ technical.txt
β βββ account.txt
β βββ general.txt
βββ faiss_index/ # FAISS vector store (generated)
βββ index.faiss
βββ index.pkl
User Input:
I was charged twice for my subscription on account 123456789
Expected Response:
- Intent:
billing
(high confidence) - Entities: Account number
123456789
- Answer: Information about duplicate charges and refund process
- Sources: Billing FAQ documents
User Input:
The app keeps crashing when I try to login and I'm getting an error message
Expected Response:
- Intent:
technical
(high confidence) - Multiple topics detected: crashes, login, errors
- Answer: Troubleshooting steps for login and crash issues
- Sources: Technical support documents
User Input:
How do I reset my password and enable two-factor authentication?
Expected Response:
- Intent:
account
(high confidence) - Multiple intents: password reset, 2FA setup
- Answer: Step-by-step instructions for both tasks
- Sources: Account management documents
User Input:
I'm very frustrated because my payment failed and now the app won't open
Expected Response:
- Primary intent:
complaints
- Secondary intents:
billing
,technical
- Answer: Empathetic response addressing both issues
- Sources: Multiple document categories
User Input:
What integrations do you support?
Expected Response:
- Intent:
general
- Answer: List of supported integrations
- Sources: General information documents
Click the "π Analytics" button in the chat interface or navigate to:
http://localhost:5000/analytics
- Total Queries: Number of questions asked
- Average Response Time: Mean time to generate responses
- Satisfaction Score: Average user ratings (1-5 scale)
- Intent Distribution: Breakdown of query categories
Analytics are stored in analytics.json
:
{
"total_queries": 45,
"intent_accuracy": [],
"response_times": [1.2, 0.8, 1.5, ...],
"satisfaction_scores": [5, 4, 5, 3, ...],
"intent_distribution": {
"billing": 12,
"technical": 18,
"account": 10,
"complaints": 3,
"general": 2
}
}
Send a message to the chatbot
Request:
{
"message": "How do I reset my password?"
}
Response:
{
"response": "To reset your password: 1. Go to login page...",
"intent": {
"primary_intent": "account",
"confidence": 0.95,
"all_intents": ["account"]
},
"entities": {},
"response_time": 1.23,
"sources": [
{"content": "Password reset process..."}
]
}
Submit user feedback
Request:
{
"rating": 5
}
Response:
{
"status": "success"
}
Get analytics summary
Response:
{
"total_queries": 45,
"avg_response_time": 1.15,
"avg_satisfaction": 4.2,
"intent_distribution": {
"billing": 12,
"technical": 18,
"account": 10,
"complaints": 3,
"general": 2
}
}
Clear conversation history
Response:
{
"status": "success"
}
Solution: Run python ingest_documents.py
to create the index
Solution: Check your .env
file has the correct API key for your chosen model
Solution: Ensure virtual environment is activated and run pip install -r requirements.txt
Solutions:
- Use faster model (e.g., gpt-3.5-turbo instead of gpt-4)
- Reduce chunk size in retrieval
- Use local Ollama model
- Check internet connection
Solution: Change port in app.py
:
app.run(debug=True, port=5001) # Use different port
Solutions:
- Ensure Ollama is running:
ollama serve
- Check base URL in
.env
- Verify model is downloaded:
ollama list
- Create a new
.txt
file inknowledge_base/
- Add your content
- Run
python ingest_documents.py
again - Restart the Flask app
Edit the INTENT_PATTERNS
dictionary in app.py
:
INTENT_PATTERNS = {
'billing': [
r'\b(bill|invoice|payment)\b',
# Add more patterns
],
'custom_intent': [
r'\b(your|patterns|here)\b',
]
}
Edit the CSS in templates/index.html
:
/* Change color scheme */
background: linear-gradient(135deg, #YOUR_COLOR1 0%, #YOUR_COLOR2 100%);
In app.py
, modify:
vectorstore.as_retriever(search_kwargs={"k": 5}) # Return top 5 documents
# Install Heroku CLI
heroku login
heroku create your-app-name
git push heroku main
heroku config:set OPENAI_API_KEY=your-key
- Connect GitHub repository
- Add environment variables in dashboard
- Deploy automatically
Use Docker or traditional deployment methods. Ensure environment variables are set securely.
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request.
For issues and questions:
- Create an issue on GitHub
- Email: your-email@example.com
- LangChain for the amazing framework
- OpenAI, Google, and Ollama for LLM access
- HuggingFace for embeddings
- Facebook AI for FAISS
Built with β€οΈ using LangChain, Flask, and FAISS
61f035c (Initial commit)