Spaces:
Sleeping
Sleeping
File size: 2,885 Bytes
1d570ce |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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. |