A real-time Django-based chatbot application with OpenAI integration and RAG (Retrieval-Augmented Generation) capabilities, supporting both text and voice chat functionality. Built with Django Channels for WebSocket communication and Celery for asynchronous task processing.
- 🤖 AI-Powered Conversations: Integration with OpenAI GPT models for intelligent responses
- 📚 RAG Implementation: Retrieval-Augmented Generation for context-aware responses using vector database
- 💬 Real-time Chat: WebSocket-based communication for instant messaging
- 🎤 Voice Chat: Speech-to-text and text-to-speech functionality
- 📄 Document Management: Upload and index documents for knowledge base
- 🔍 Semantic Search: Vector-based similarity search for relevant content retrieval
- ☁️ S3 Integration: Cloud storage for document management
- 📱 Responsive UI: Clean, mobile-friendly interface built with Tailwind CSS
- ⚡ Asynchronous Processing: Celery-based background task handling
- 🗄️ Database Management: PostgreSQL with Django ORM
- 📊 Admin Interface: Django admin for managing bots, profiles, conversations, and documents
- 🔧 Configurable Bots: Multiple LLM providers and model configurations with RAG parameters
- 📈 Chat History: Persistent conversation storage and retrieval
- Backend: Django 5.2.4, Django Channels, Django REST Framework
- Database: PostgreSQL
- Vector Database: External vector database service for RAG
- Storage: AWS S3 (via django-s3-storage)
- Message Broker: Redis
- Task Queue: Celery
- AI Integration: OpenAI API
- Frontend: HTML, JavaScript, Tailwind CSS
- WebSockets: Django Channels with Redis channel layer
Before running this application, ensure you have the following installed:
- Python 3.8+
- PostgreSQL
- Redis Server
- OpenAI API Key
- Vector Database Service (for RAG functionality)
- AWS S3 Bucket (for document storage)
For Windows users, you have several options:
- WSL (Windows Subsystem for Linux) - Recommended
- Install WSL2 and Ubuntu from Microsoft Store
- Run
sudo apt-get install redis-serverin WSL
- Redis for Windows (Community Edition)
- Download from: https://github.com/microsoftarchive/redis/releases
- Or use Memurai (Redis-compatible): https://www.memurai.com/
Unix/Linux/Mac:
git clone <repository-url>
cd simple_chatbotWindows (Command Prompt/PowerShell):
git clone <repository-url>
cd simple_chatbotUnix/Linux/Mac:
python -m venv venv
source venv/bin/activateWindows (Command Prompt):
python -m venv venv
venv\Scripts\activateWindows (PowerShell):
python -m venv venv
venv\Scripts\Activate.ps1Note: If you encounter execution policy error in PowerShell, run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserAll Platforms:
pip install -r requirements.txtCreate a .env file in the root directory using the provided sample.env:
Unix/Linux/Mac:
cp sample.env .envWindows (Command Prompt/PowerShell):
copy sample.env .envEdit the .env file with your configuration:
# Database Configuration
DATABASE_NAME='your_database_name'
DATABASE_USER='your_database_user'
DATABASE_PASSWORD='your_database_password'
DATABASE_HOST='localhost'
DATABASE_PORT='5432'
# Django Settings
SETTINGS_DEBUG='True'
DJANGO_SETTINGS_MODULE='simple_chatbot.settings'
DEFAULT_LOG_LEVEL='INFO'
# OpenAI Configuration
OPENAI_API_KEY='your_openai_api_key'
# RAG & Vector Database Configuration
VECTOR_DB_BASE_URL='http://your-vector-db-url:port'
DATABASE_INTERFACE_BEARER_TOKEN='your_vector_db_auth_token'
# AWS S3 Configuration (for document storage)
S3_BASE_URL='https://your-bucket.s3.region.amazonaws.com/'
AWS_REGION='your-aws-region'
AWS_ACCESS_KEY_ID='your-aws-access-key'
AWS_SECRET_ACCESS_KEY='your-aws-secret-key'Create a PostgreSQL database and run migrations:
All Platforms:
python manage.py makemigrations
python manage.py migrateAll Platforms:
python manage.py createsuperuserCreate initial profiles for the chatbot system:
All Platforms:
python manage.py shellThen in the Python shell:
from chatbot.models import Profile, Bot
# Create AI profile (required - ID must be 1)
ai_profile = Profile.objects.create(
id=1,
first_name="AI Assistant",
email="[email protected]",
profile_type="MODERATOR"
)
# Create default user profile (ID must be 2)
user_profile = Profile.objects.create(
id=2,
first_name="User",
email="[email protected]",
profile_type="USER"
)
# Create a sample bot with RAG configuration
bot = Bot.objects.create(
name="RAG Assistant",
route="/",
context="You are a helpful AI assistant with access to a knowledge base.",
llm_model="gpt-4o-mini",
top_k=3, # Number of documents to retrieve
filter_score=0.8, # Minimum relevance score for documents
bot_temperature=0.7,
max_token=2048
)
# Exit the shell
exit()Unix/Linux/Mac:
redis-serverWindows (if using WSL):
# In WSL terminal
sudo service redis-server start
# Or
redis-serverWindows (if using Redis for Windows):
# Navigate to Redis installation directory
redis-server.exeOpen a new terminal/command prompt and activate virtual environment:
Unix/Linux/Mac:
source venv/bin/activate
celery -A simple_chatbot worker --loglevel=info --pool=soloWindows (Command Prompt):
venv\Scripts\activate
celery -A simple_chatbot worker --loglevel=info --pool=soloWindows (PowerShell):
venv\Scripts\Activate.ps1
celery -A simple_chatbot worker --loglevel=info --pool=soloNote: The --pool=solo flag is important for Windows compatibility
Open another new terminal/command prompt and activate virtual environment:
Unix/Linux/Mac:
source venv/bin/activate
python manage.py runserver 0.0.0.0:9000Windows (Command Prompt):
venv\Scripts\activate
python manage.py runserver 0.0.0.0:9000Windows (PowerShell):
venv\Scripts\Activate.ps1
python manage.py runserver 0.0.0.0:9000The application will be available at http://localhost:9000
- Navigate to
http://localhost:9000/chat/ - Enter your context/instructions in the right panel
- Type your message and press Enter or click Send
- The AI will respond using both its training and relevant documents from the knowledge base
- Navigate to
http://localhost:9000/voice-chat/ - Enter your context/instructions in the right panel
- Click the "Speak" button and speak your message
- The AI will respond with both text and speech
- Access the Django admin at
http://localhost:9000/admin/ - Navigate to the Media section
- Upload documents (PDF or TXT files)
- Documents are automatically:
- Stored in S3
- Indexed in the vector database
- Available for RAG queries
Access the Django admin at http://localhost:9000/admin/ to:
- Bot Management: Configure bots with RAG parameters
top_k: Number of documents to retrievefilter_score: Minimum relevance score (0-1)context: System prompt for the bot
- Media Management: Upload and manage documents for the knowledge base
- Chat History: View all conversations
- Profile Management: Manage user profiles
- Document Upload: Documents are uploaded through the Media model
- Storage: Files are stored in S3 with metadata
- Indexing: Documents are automatically indexed in the vector database
- Query Processing: User questions trigger semantic search
- Context Retrieval: Relevant document chunks are retrieved
- Response Generation: ChatGPT generates responses using retrieved context
- Media Model: Manages document storage and metadata
- MediaVector Model: Tracks vector database IDs
- KeyValue Model: Stores document metadata (author, category, etc.)
- Vector Database Integration: Semantic search and retrieval
- S3 Storage: Reliable document storage
Configure RAG behavior through Bot model settings:
bot = Bot.objects.create(
name="Knowledge Bot",
top_k=5, # Retrieve top 5 relevant documents
filter_score=0.75, # Only use documents with >75% relevance
context="Answer based on the provided documents only.",
# ... other settings
)simple_chatbot/
├── chatbot/ # Main application
│ ├── admin.py # Django admin configuration
│ ├── apps.py # App configuration
│ ├── models/ # Database models
│ │ ├── __init__.py
│ │ ├── base_models.py # Core models (Bot, Profile, Chat, Media)
│ │ └── enums.py # Model choices and enums
│ ├── consumers/ # WebSocket consumers
│ │ ├── chat_consumers.py # Basic chat consumer
│ │ └── company_chat_consumers.py # RAG-enabled chat consumer
│ ├── celery_tasks/ # Background tasks
│ │ ├── chat_tasks.py # Basic chat response processing
│ │ └── company_chat_tasks.py # RAG-enhanced response processing
│ ├── utils/ # Utility functions
│ │ └── chat_utils.py # RAG utilities (vector DB operations)
│ ├── templates/ # HTML templates
│ │ ├── base.html
│ │ └── chat/
│ │ ├── chat.html # Text chat interface
│ │ └── voice_demo.html # Voice chat interface
│ ├── migrations/ # Database migrations
│ ├── routing.py # WebSocket URL routing
│ ├── urls.py # HTTP URL patterns
│ └── views.py # Django views
├── simple_chatbot/ # Project settings
│ ├── __init__.py
│ ├── asgi.py # ASGI configuration
│ ├── celery.py # Celery configuration
│ ├── settings.py # Django settings
│ ├── urls.py # Main URL configuration
│ └── wsgi.py # WSGI configuration
├── manage.py # Django management script
├── requirements.txt # Python dependencies
├── .gitignore # Git ignore rules
└── sample.env # Environment variables template
Configurable AI bot with LLM provider settings, context management, response parameters, and RAG configuration.
Key RAG Fields:
top_k: Number of documents to retrieve from vector databasefilter_score: Minimum relevance score for document inclusioncontext: System prompt with instructions for using retrieved documents
User profiles supporting different types (USER, MODERATOR, PROSPECT).
Chat message storage with sender/receiver relationships and session management.
Document management for RAG system:
- File storage in S3
- Automatic vector database indexing
- Metadata support through KeyValue pairs
Tracks vector database IDs for indexed documents.
Flexible metadata storage for documents (author, category, tags, etc.).
/chat/- Text chat interface/voice-chat/- Voice chat interface/admin/- Django admin interface
ws/chat/- Basic WebSocket chat connectionws/chat/company/- RAG-enabled WebSocket chat connection
- Create new models in
chatbot/models/ - Add WebSocket consumers in
chatbot/consumers/ - Implement background tasks in
chatbot/celery_tasks/ - Create templates in
chatbot/templates/ - Add URL patterns in
chatbot/urls.py
To add documents to the knowledge base programmatically:
from chatbot.models import Media, KeyValue
# Upload a document
media = Media.objects.create(
name="Product Documentation",
media_type="application/pdf",
file=your_file_object,
description="Product user manual"
)
# Add metadata
KeyValue.objects.create(
media=media,
key="category",
value="documentation"
)
# Document is automatically indexed in vector databaseAfter model changes:
All Platforms:
python manage.py makemigrations
python manage.py migrateRun Django tests:
All Platforms:
python manage.py test-
WebSocket Connection Failed
- Ensure Redis server is running
- Check CHANNEL_LAYERS configuration in settings.py
- On Windows, ensure Redis is properly installed (WSL or Windows version)
-
Celery Tasks Not Processing
- Verify Celery worker is running with
--pool=soloflag on Windows - Check Redis connection
- Ensure OPENAI_API_KEY is set
- Verify Celery worker is running with
-
Database Connection Error
- Verify PostgreSQL server is running
- Check database credentials in .env file
- Ensure database exists
-
OpenAI API Errors
- Verify API key is valid and has sufficient credits
- Check rate limits and usage quotas
-
Vector Database Issues
- Verify VECTOR_DB_BASE_URL is correct
- Check DATABASE_INTERFACE_BEARER_TOKEN is valid
- Ensure vector database service is running
-
S3 Upload Errors
- Verify AWS credentials in .env
- Check S3 bucket permissions
- Ensure bucket exists in specified region
-
Windows-Specific Issues
- PowerShell Execution Policy: Run
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser - Path Issues: Use forward slashes (/) in .env file paths even on Windows
- Celery: Always use
--pool=soloflag on Windows
- PowerShell Execution Policy: Run
- Django logs: Check console output
- Celery logs: Available in Celery worker terminal
- Redis logs: Check Redis server logs (WSL or Windows Redis logs)
- Vector DB logs: Check vector database service logs
For production deployment:
- Set
DEBUG=Falsein settings - Configure proper database settings
- Use a production ASGI server (e.g., Daphne, Uvicorn)
- Set up Redis with proper persistence
- Configure Celery with supervisor (Linux) or Task Scheduler (Windows)
- Use environment variables for sensitive settings
- Set up proper logging and monitoring
- Configure S3 bucket with appropriate security policies
- Ensure vector database has proper scaling and backup
- Indexing: Ensure documents are properly chunked before indexing
- Relevance Tuning: Adjust
filter_scorebased on your use case - Retrieval Count: Balance
top_kbetween context quality and response time - Caching: Consider caching frequent queries
- Document Quality: Ensure uploaded documents are clean and well-structured