pythonic-rag / backend /README.md
vin00d's picture
assignment 3 submission and fastapi and react code with cursor help.
1d570ce
# Document Chat Backend
This is the FastAPI backend for the Document Chat application that enables document uploading, processing, and chat-based Q&A functionality.
## Project Structure
```
backend/
β”œβ”€β”€ app/
β”‚ β”œβ”€β”€ __init__.py
β”‚ β”œβ”€β”€ main.py # FastAPI application and route definitions
β”‚ β”œβ”€β”€ models.py # Pydantic models for request/response validation
β”‚ β”œβ”€β”€ services/
β”‚ β”‚ β”œβ”€β”€ __init__.py
β”‚ β”‚ β”œβ”€β”€ document_processor.py # Document processing and chunking
β”‚ β”‚ β”œβ”€β”€ vector_store.py # Vector storage and similarity search
β”‚ β”‚ └── chat_service.py # OpenAI chat completion handling
β”‚ └── dependencies.py # FastAPI dependencies and shared resources
└── requirements.txt # Python package dependencies
```
## Setup
1. Create a virtual environment:
```bash
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
```
2. Install dependencies:
```bash
pip install -r requirements.txt
```
3. Create a `.env` file in the root directory with your OpenAI API key:
```
OPENAI_API_KEY=your_api_key_here
```
4. Run the development server:
```bash
uvicorn app.main:app --reload
```
The API will be available at `http://localhost:8000`
## API Endpoints
- `POST /upload`
- Upload PDF or TXT files for processing
- Returns a session ID for subsequent chat interactions
- `POST /chat`
- Send messages to chat with the uploaded document
- Requires a session ID from a previous upload
## Development
- The `services` directory contains the core business logic:
- `document_processor.py`: Handles file uploads and text chunking
- `vector_store.py`: Manages document embeddings and similarity search
- `chat_service.py`: Interfaces with OpenAI for chat completions
- `models.py` defines the data structures using Pydantic
- `main.py` contains the FastAPI application and route handlers
- `dependencies.py` manages shared resources and dependencies
## Requirements
See `requirements.txt` for a full list of Python dependencies. Key packages include:
- FastAPI
- OpenAI
- PyPDF2
- python-multipart
- python-dotenv
- uvicorn
```
To create the directory structure, you can run these commands:
```bash
mkdir -p backend/app/services
touch backend/app/__init__.py
touch backend/app/main.py
touch backend/app/models.py
touch backend/app/dependencies.py
touch backend/app/services/__init__.py
touch backend/app/services/document_processor.py
touch backend/app/services/vector_store.py
touch backend/app/services/chat_service.py
touch backend/requirements.txt
```
This will create all the necessary files and directories in the structure you specified. The README.md provides a clear overview of the project structure, setup instructions, and key components for developers who will work with the backend.