# 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.