A PDF document assistant with a FastAPI-based backend and a Streamlit-based frontend. The backend integrates with Google Gemini AI and Pinecone vector database, allowing users to upload PDF files and ask questions about their content using natural language via the frontend interface. Event bot(https://eventbot-pinecone-db.streamlit.app/)
- π Features
- π Prerequisites
- π οΈ Installation & Setup
- π API Keys Setup
- βοΈ Environment Configuration
- π Running Locally
- π‘ API Endpoints
- π Deploy to Render.com
- π§ Development
- π§ͺ Running Tests
- π οΈ Troubleshooting
- π Monitoring
- π Security
- π License
- π€ Contributing
- π Support
- PDF Document Processing: Upload and process PDF files into searchable vectors.
- AI-Powered Q&A: Ask questions about uploaded PDFs using Google Gemini AI.
- Vector Search: Efficient document retrieval using Pinecone vector database.
- Resume Support: Special handling for resume/CV documents (filename-based user ID extraction).
- Streamlit Frontend: User-friendly interface for uploading PDFs and interacting with the chatbot.
- RESTful API: Clean REST endpoints for the backend, consumed by the frontend.
- Health Monitoring: Built-in health checks and logging for the backend.
- Python 3.8 or higher
- Google AI Studio account (for Gemini API key)
- Pinecone account (for Pinecone API key and index)
- Git
For a quick start, follow these steps:
-
Clone the Repository:
git clone https://github.com/vijender883/Chatbot_Pinecone_flask_backend cd Chatbot_Pinecone_flask_backend -
Create and Activate Virtual Environment:
- macOS/Linux:
python3 -m venv venv source venv/bin/activate - Windows (Command Prompt):
python -m venv venv venv\Scripts\activate
For PowerShell or other shells, please refer to the detailed guide.
- macOS/Linux:
-
Install Dependencies:
pip install -r requirements.txt
For comprehensive instructions, including API key setup, environment configuration, running the application, deployment, and troubleshooting, please see our Detailed Installation and Setup Guide.
To run the FastAPI backend server:
make run-backend
# Alternatively: uvicorn app:app --reload (or similar command based on your app structure)The backend will typically start on http://localhost:8000 (FastAPI's default) or http://localhost:5000 if configured.
To run the Streamlit frontend application:
- Ensure the backend is running.
- Set the
ENDPOINTenvironment variable if your backend is not onhttp://localhost:5000. For local development, you can addENDPOINT=http://localhost:5000to your.envfile.
make run-frontend
# Alternatively: streamlit run src/frontend/streamlit_app.pyThe frontend will typically be available at http://localhost:8501.
The API routes are primarily defined in src/backend/routes/chat.py. The root / endpoint is in app.py.
| Endpoint | Method | Description | Request Body (Format) | Success Response (JSON Example) |
|---|---|---|---|---|
/ |
GET | Basic API information and available endpoints. | N/A | {"message": "PDF Assistant Chatbot API", "version": "1.0.0", "endpoints": {"/health": "GET - Health check", ...}} (from chat.py if routed, or app.py's version) |
/health |
GET | Detailed health check of backend services. | N/A | {"status": "success", "health": {"gemini_api": true, ...}, "healthy": true} |
/uploadpdf |
POST | Uploads a PDF file for processing and vectorization. | FormData: file (PDF file) |
{"success": true, "message": "PDF 'name.pdf' uploaded...", "filename": "name.pdf"} |
/answer |
POST | Asks a question about the processed PDF content. | JSON: {"query": "Your question?"} |
{"answer": "AI generated answer."} |
Note: The root endpoint / defined in app.py provides a simple welcome message. The one in chat.py (if chat_bp is mounted at root) offers more detail. The table reflects the more detailed one for completeness.
For deployment instructions, see the Detailed Installation and Setup Guide.
Chatbot_Pinecone_flask_backend/
βββ .env # Local environment variables (gitignored)
βββ .env.template # Template for .env file
βββ .git/ # Git version control directory
βββ .gitignore # Specifies intentionally untracked files for Git
βββ README.md # This guide
βββ Makefile # Defines common tasks like running, testing, linting
βββ app.py # Main FastAPI application entry point for the backend (often named main.py or app.py)
βββ requirements.txt # Python package dependencies for both backend and frontend
βββ requirements-dev.txt # Development-specific dependencies (testing, linting)
βββ start.sh # Shell script for starting the backend application (e.g., via Uvicorn with Gunicorn workers)
βββ src/ # Main source code directory
β βββ backend/ # Source code for the FastAPI backend
β β βββ __init__.py
β β βββ agents/
β β β βββ base.py
β β β βββ rag_agent.py
β β βββ config.py
β β βββ routes/
β β β βββ __init__.py
β β β βββ chat.py
β β βββ services/
β β β βββ orchestrator.py
β β βββ utils/
β β βββ helper.py
β βββ frontend/ # Source code for the Streamlit frontend
β βββ streamlit_app.py # Main Streamlit application file
βββ tests/ # Automated tests (primarily for the backend)
βββ conftest.py
βββ test_agents/
βββ test_routes/
Backend:
app.py: Initializes the FastAPI app, includes routers, and defines the root (/) endpoint for the backend. (Filename might bemain.py)src/backend/routes/chat.py: Contains the FastAPI APIRouter for core API endpoints:/health,/uploadpdf, and/answer.src/backend/agents/rag_agent.py: Implements the core RAG (Retrieval Augmented Generation) logic, including PDF processing, vector embedding, and question answering using Gemini and Pinecone.src/backend/services/orchestrator.py: Acts as a layer between API routes and theRAGAgent.src/backend/config.py: Manages application configuration, loading settings from environment variables.
Frontend:
src/frontend/streamlit_app.py: A Streamlit application providing the user interface. It interacts with the backend API to upload PDFs and get answers to questions.
The application uses environment variables for configuration. These are typically defined in a .env file in the project root for local development. See .env.template for a list of required variables.
Backend Variables:
GEMINI_API_KEY: Your Google Gemini API key.PINECONE_API_KEY: Your Pinecone API key.PINECONE_INDEX_NAME: The name of your Pinecone index.PINECONE_CLOUD: The cloud provider for your Pinecone index (e.g.,aws).PINECONE_REGION: The region of your Pinecone index (e.g.,us-east-1).APP_ENV: Set todevelopmentorproduction. This variable typically controls debug mode and other environment-specific settings. (FormerlyFLASK_ENV)PORT: Port for the backend server (defaults to8000for Uvicorn/FastAPI, but can be5000if configured).
Frontend Variables:
ENDPOINT: The URL of the backend API. For local development, this would typically behttp://localhost:5000.
(This section outlines general steps. Specific test setup might vary.)
For information on installing test dependencies, see the Detailed Installation and Setup Guide.
- Run Tests: Navigate to the project root directory and execute:
Pytest will automatically discover and run tests (typically files named
pytest
test_*.pyor*_test.pyin thetests/directory).
Refer to the tests/ directory and any specific test documentation or configuration files for more detailed instructions on running tests.
For troubleshooting common installation and setup issues, refer to the Detailed Installation and Setup Guide.
For more verbose error output locally:
- Set
APP_ENV=developmentin your.envfile. This often enables FastAPI's debug mode. - Optionally, set
LOG_LEVEL=DEBUGin.envfor more detailed application logs. - Run the app (e.g.,
uvicorn app:app --reload).
The /health endpoint (see API Endpoints) provides detailed status of backend components. Regularly polling this endpoint can help ensure system availability.
- Local Development: Logs are output to the console where
python app.pyis running. AdjustLOG_LEVELin.envfor desired verbosity. - Render Deployment: Access and monitor logs via the Render dashboard for your service. This is crucial for diagnosing issues in the production environment.
Key information to look for in logs:
- Successful/failed PDF uploads and processing durations.
- Question answering request details.
- Errors from external services (Gemini, Pinecone).
- Any unexpected application exceptions or tracebacks.
- API Keys: Handled via environment variables (
.envlocally, Render's environment settings). Never hardcode keys. Ensure.envis in.gitignore. - File Uploads:
werkzeug.utils.secure_filenameis used to sanitize filenames.- File type and size are validated as per
ALLOWED_EXTENSIONSandMAX_FILE_SIZEin the app configuration.
- Input Validation: Basic validation for presence of query in
/answerand file in/uploadpdf. Sensitive inputs should always be validated and sanitized. - CORS: FastAPI handles CORS through
CORSMiddleware. Ensure it's configured securely, especially in production, by specifying allowed origins, methods, and headers. For example:from fastapi.middleware.cors import CORSMiddleware app.add_middleware( CORSMiddleware, allow_origins=["https://your.frontend.domain.com"], # Or ["*"] for development allow_credentials=True, allow_methods=["*"], allow_headers=["*"], )
- Error Handling: FastAPI has built-in support for returning structured JSON error responses (e.g., using
HTTPException) and allows for custom exception handlers. This helps avoid exposing raw stack traces. - Dependency Management: Keep
requirements.txtup-to-date. Regularly audit dependencies for vulnerabilities using tools likepip-auditor GitHub's Dependabot. - HTTPS: Render automatically provides HTTPS for deployed services.
This project is licensed under the MIT License. It's good practice to include a LICENSE file in the repository root with the full text of the MIT License.
Contributions are welcome! Please adhere to the following process:
- Fork the Repository: Create your own fork on GitHub.
- Create a Branch:
git checkout -b feature/your-new-featureorbugfix/issue-description. - Develop: Make your changes.
- Test: Add and run tests for your changes using
pytest. - Commit: Write clear, concise commit messages.
- Push: Push your branch to your fork:
git push origin your-branch-name. - Pull Request: Open a PR against the
mainbranch of the original repository. Clearly describe your changes and link any relevant issues.
If you encounter issues or have questions:
- Check GitHub Issues: See if your question or problem has already been addressed.
- Review Troubleshooting Section: The Detailed Installation and Setup Guide might have a solution.
- Create a New Issue: If your issue is new, provide detailed information:
- Steps to reproduce.
- Expected vs. actual behavior.
- Error messages and relevant logs.
- Your environment (OS, Python version).
- For Render-specific deployment issues, consult the Render documentation.
Happy coding! π