This project is a Retrieval-Augmented Generation (RAG) system that allows users to upload PDF files, extract text from them, and interact with a chatbot that uses the retrieved information as context to provide relevant answers. The system is built using Python, Flask for the backend, and Streamlit for the frontend.
kind (Kubernetes in Docker)Clone the repository
git clone https://github.com/aaronkher/rag_chatbot.git
cd rag_chatbot
Create and activate a virtual environment
It is recommended to use a specific name for the virtual environment to keep the project organized.
python3 -m venv rag_chatbot_venv
source rag_chatbot_venv/bin/activate # On Windows use `rag_chatbot_venv\Scripts\activate`
Install dependencies
pip install -r requirements.txt
pip install sentence-transformers
python -m nltk.downloader punkt
Set up the OpenAI API Key
You need to set the OpenAI API key as an environment variable:
export OPENAI_API_KEY=your_openai_api_key_here
Replace your_openai_api_key_here with your actual OpenAI API key.
In the Streamlit frontend code (frontend/chat_page.py), ensure the BACKEND_API variable points to the correct backend service URL:
For Local Environment (without Docker) setup, uncomment the following line and comment out the Docker line:
# BACKEND_API = os.getenv('BACKEND_API', 'http://localhost:8000')
For Docker setup, use:
BACKEND_API = os.getenv('BACKEND_API', 'http://api:8000')
For Kubernetes setup, uncomment the following line and comment out the Docker line:
# BACKEND_API = os.getenv('BACKEND_API', 'http://my-api-service:8000')
Note: The BACKEND_API is set for Docker functionality by default. Make sure to update it accordingly if you are using Kubernetes or running the application locally without Docker
Run the API
python main/main_api.py
Run the Frontend
In a new terminal window:
streamlit run frontend/chat_page.py
Build the Docker Images
docker compose build
This command will build the Docker images for both the API and the frontend.
Run the Services
docker compose up
The API will be available at http://localhost:8000 and the Frontend will be available at http://localhost:8501.
🚨 Important: It takes some time for both the API (main_api.py) and the frontend (chat_page.py) services to start up completely. Please wait until you see both the API and frontend are running before attempting to use the application.
Stop the services
docker compose down
kindCreate a kind Cluster
kind create cluster --name rag-chatbot-cluster
Load Docker Images into kind
kind load docker-image my-api:latest --name rag-chatbot-cluster
kind load docker-image my-frontend:latest --name rag-chatbot-cluster
Deploy the Application to Kubernetes
Apply the deployment and service YAML files:
kubectl apply -f api-deployment.yaml
kubectl apply -f frontend-deployment.yaml
Port Forwarding to Access the Services
Run these commands in separate terminals:
kubectl port-forward service/my-api-service 30001:8000
kubectl port-forward service/my-frontend-service 30002:8501
The API will be accessible at http://localhost:30001 and the frontend at http://localhost:30002.
kind)Stop Port Forwarding:
Press Ctrl + C in the terminal where port forwarding is running.
Delete the Kubernetes Deployment and Services:
kubectl delete deployment my-api my-frontend
kubectl delete service my-api-service my-frontend-service
Delete the kind Cluster:
kind delete cluster --name rag-chatbot-cluster
Stop the Services:
docker compose down
Deactivate Virtual Environment:
deactivate
Stop Running Services:
Press Ctrl + C in the terminal where each service is running.
rag/: Contains the main backend logic.
chatgpt.py: Handles interactions with the ChatGPT model.document_vectorizer.py: Vectorizes documents for retrieval.ocr.py: Handles OCR tasks for PDF text extraction.main/: Contains the entry point for the Flask API.
main_api.py: The Flask API service.frontend/: Contains the Streamlit frontend application.
chat_page.py: The Streamlit frontend service.retriever/: Contains the logic for retrieving relevant document chunks.text_documents/: Stores the text extracted from the PDFs.Dockerfile.api: Dockerfile for the API service.Dockerfile.frontend: Dockerfile for the Frontend service.docker-compose.yml: Docker Compose configuration for running the project.api-deployment.yaml: Kubernetes Deployment configuration for the API service.api-service.yaml: Kubernetes Service configuration for exposing the API service.frontend-deployment.yaml: Kubernetes Deployment configuration for the frontend service.frontend-service.yaml: Kubernetes Service configuration for exposing the frontend service.kind-config.yaml: Configuration file for creating a Kubernetes cluster using kind.api_keys.env: Environment file containing sensitive information such as the OpenAI API key.Check out this video demonstration that showcases the project and explains how RAG works.
Make sure to create the api_keys.env file as described above and correctly set the OPENAI_API_KEY variable.
This project is a Retrieval-Augmented Generation (RAG) system that allows users to upload PDF files, extract text from them, and interact with a chatbot that uses the retrieved information as context to provide relevant answers. The system is built using Python, Flask for the backend, and Streamlit for the frontend.
kind (Kubernetes in Docker)Clone the repository
git clone https://github.com/aaronkher/rag_chatbot.git
cd rag_chatbot
Create and activate a virtual environment
It is recommended to use a specific name for the virtual environment to keep the project organized.
python3 -m venv rag_chatbot_venv
source rag_chatbot_venv/bin/activate # On Windows use `rag_chatbot_venv\Scripts\activate`
Install dependencies
pip install -r requirements.txt
pip install sentence-transformers
python -m nltk.downloader punkt
Set up the OpenAI API Key
You need to set the OpenAI API key as an environment variable:
export OPENAI_API_KEY=your_openai_api_key_here
Replace your_openai_api_key_here with your actual OpenAI API key.
In the Streamlit frontend code (frontend/chat_page.py), ensure the BACKEND_API variable points to the correct backend service URL:
For Local Environment (without Docker) setup, uncomment the following line and comment out the Docker line:
# BACKEND_API = os.getenv('BACKEND_API', 'http://localhost:8000')
For Docker setup, use:
BACKEND_API = os.getenv('BACKEND_API', 'http://api:8000')
For Kubernetes setup, uncomment the following line and comment out the Docker line:
# BACKEND_API = os.getenv('BACKEND_API', 'http://my-api-service:8000')
Note: The BACKEND_API is set for Docker functionality by default. Make sure to update it accordingly if you are using Kubernetes or running the application locally without Docker
Run the API
python main/main_api.py
Run the Frontend
In a new terminal window:
streamlit run frontend/chat_page.py
Build the Docker Images
docker compose build
This command will build the Docker images for both the API and the frontend.
Run the Services
docker compose up
The API will be available at http://localhost:8000 and the Frontend will be available at http://localhost:8501.
🚨 Important: It takes some time for both the API (main_api.py) and the frontend (chat_page.py) services to start up completely. Please wait until you see both the API and frontend are running before attempting to use the application.
Stop the services
docker compose down
kindCreate a kind Cluster
kind create cluster --name rag-chatbot-cluster
Load Docker Images into kind
kind load docker-image my-api:latest --name rag-chatbot-cluster
kind load docker-image my-frontend:latest --name rag-chatbot-cluster
Deploy the Application to Kubernetes
Apply the deployment and service YAML files:
kubectl apply -f api-deployment.yaml
kubectl apply -f frontend-deployment.yaml
Port Forwarding to Access the Services
Run these commands in separate terminals:
kubectl port-forward service/my-api-service 30001:8000
kubectl port-forward service/my-frontend-service 30002:8501
The API will be accessible at http://localhost:30001 and the frontend at http://localhost:30002.
kind)Stop Port Forwarding:
Press Ctrl + C in the terminal where port forwarding is running.
Delete the Kubernetes Deployment and Services:
kubectl delete deployment my-api my-frontend
kubectl delete service my-api-service my-frontend-service
Delete the kind Cluster:
kind delete cluster --name rag-chatbot-cluster
Stop the Services:
docker compose down
Deactivate Virtual Environment:
deactivate
Stop Running Services:
Press Ctrl + C in the terminal where each service is running.
rag/: Contains the main backend logic.
chatgpt.py: Handles interactions with the ChatGPT model.document_vectorizer.py: Vectorizes documents for retrieval.ocr.py: Handles OCR tasks for PDF text extraction.main/: Contains the entry point for the Flask API.
main_api.py: The Flask API service.frontend/: Contains the Streamlit frontend application.
chat_page.py: The Streamlit frontend service.retriever/: Contains the logic for retrieving relevant document chunks.text_documents/: Stores the text extracted from the PDFs.Dockerfile.api: Dockerfile for the API service.Dockerfile.frontend: Dockerfile for the Frontend service.docker-compose.yml: Docker Compose configuration for running the project.api-deployment.yaml: Kubernetes Deployment configuration for the API service.api-service.yaml: Kubernetes Service configuration for exposing the API service.frontend-deployment.yaml: Kubernetes Deployment configuration for the frontend service.frontend-service.yaml: Kubernetes Service configuration for exposing the frontend service.kind-config.yaml: Configuration file for creating a Kubernetes cluster using kind.api_keys.env: Environment file containing sensitive information such as the OpenAI API key.Check out this video demonstration that showcases the project and explains how RAG works.
Make sure to create the api_keys.env file as described above and correctly set the OPENAI_API_KEY variable.