A FastAPI-based chatbot that allows users to upload PDFs and ask questions about their content using Google's Generative AI.
- Python 3.12 or higher
- pip (Python package installer)
- A Google API key for Generative AI
- Clone the repository:
git clone <repository-url>
cd pdf_chatbot
- Create a virtual environment:
python -m venv venv
- Activate the virtual environment:
On Windows:
.\venv\Scripts\activate
On Unix or MacOS:
source venv/bin/activate
- Install dependencies:
pip install -r requirements.txt
- Set up environment variables:
- Copy the
.env.example
file to.env
:
- Copy the
cp .env.example .env
- Edit the
.env
file and set your configuration values:
# Required
APP_GOOGLE_API_KEY=your_google_api_key_here
# Optional (defaults shown)
APP_LOG_LEVEL=INFO
APP_ML_BUCKET_NAME=genai
APP_ML_MODEL_MODEL_NAME=gemini-1.5-flash
APP_ML_MODEL_TEMPERATURE=0.0
APP_ML_MODEL_MAX_RETRIES=2
APP_ML_TRIMMER_MAX_TOKENS=65000
APP_ML_CHUNK_SIZE=1000
APP_ML_CHUNK_OVERLAP=200
- Create required directories:
mkdir -p uploads static templates faiss_index
- Development mode with auto-reload:
uvicorn main:app --reload --port 8000
- Production mode:
uvicorn main:app --host 0.0.0.0 --port 8000
Or using Python directly:
python main.py
The application will be available at http://localhost:8000
- Build the Docker image:
docker build -t pdf-chatbot .
- Run the container:
docker run -p 8000:8000 --env-file .env pdf-chatbot
- Open your browser and navigate to
http://localhost:8000
- Upload a PDF document using the upload form
- Start chatting with the bot about the content of your PDF
pdf_chatbot/
├── main.py # FastAPI application
├── config.py # Configuration management
├── faiss_db.py # Vector database handler
├── requirements.txt # Python dependencies
├── .env # Environment variables
├── .env.example # Example environment file
├── static/ # Static files
│ ├── css/
│ └── js/
├── templates/ # Jinja2 templates
├── uploads/ # PDF uploads directory
└── faiss_index/ # Vector database storage
AGPL-3.0 License
Copyright (C)
This program is free software: you can redistribute it and/or modify it under the terms of the Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Affero General Public License for more details.
You should have received a copy of the Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.
Additional Terms: If you modify this program and use it over a network, you must make the source code available to the users of the program through the same means as this program. This applies to any modifications you make to the program, and any derivative works you create, regardless of whether they are used commercially or non-commercially.