This project is a FastAPI application integrated with Ollama, PGVectorDB, and Redis, designed to interact with PDF documents and provide AI-generated responses. It includes several endpoints for uploading PDFs and querying information from them, as well as health check mechanisms and rate limiting to ensure stability and performance.
- Docker and Docker Compose installed on your machine.
- Python 3.10.15 or higher installed on your machine.
- A GPU (optional) for enhanced performance.
-
Clone the repository:
git clone https://github.com/huseyindas/chat-with-pdf.git cd chat-with-pdf -
Copy the example environment file:
- Rename .env.example to .env:
cp .env.example .env
Edit .env as necessary to configure your environment settings.
-
Activate the virtual environment:
- If you have Pipenv installed, you can activate the virtual environment with:
pipenv shell
- If you do not have Pipenv, you can install the required packages using requirements.txt:
pip install -r requirements.txt
To start the FastAPI application with Docker Compose, you can use the provided start.sh script:
- Make the script executable:
chmod +x start.sh
- Run the application:
- To build the containers:
./start.sh build
- To run without rebuilding:
The script will check for GPU support and run the appropriate Docker Compose configuration (docker-compose.yml for CPU or docker-compose.gpu.yml for GPU)../start.sh
* Request Method: POST
* Endpoint: /v1/chat/{pdf_id}
* Description: Interact with a specific PDF.
* Input:
{
"message": "What is the main topic of this PDF?"
}
* Output:
{
"response": "The main topic of this PDF is <topic>."
}
* Request Method: POST
* Endpoint: /v1/pdf
* Description: Upload and register a PDF.
* Input: Multipart form data containing the PDF file.
* Example:
curl -X POST "http://localhost:8000/v1/pdf" \
-F "file=@/path/to/your/pdf/file.pdf"
* Output:
{
"pdf_id": "unique_pdf_identifier"
}
* Request Method: GET
* Endpoint: /v1/health
* Description: Check the health status of the application.
Upload PDF and chat endpoints are rate-limited to 3 requests per minute. Health check endpoint is limited to 1 request per second.
The application includes several middleware components:
CustomTimeoutHandlingMiddleware: Handles a 120-second timeout. CustomErrorHandlingMiddleware: Manages exceptions within the application. CustomHealthCheckMiddleware: Ensures Ollama, PGVector, and Redis are running before processing requests.
Access the API documentation at /docs (Swagger UI).
- AI and LangChain codes can be found in src/ai.
- Endpoint implementations are located in src/pdf and src/chat.
- Core operations, including middleware, rate limiting, logging, Redis, and database files, are in src/core.
-
To run the test suite, ensure that Ollama, PGVector, and Redis are running in Docker or on your system.
-
Check the environment file: Ensure that .env.pytest has the necessary configurations.
-
The run_tests.sh script exports the environment variables from .env.pytest and runs the test suite using pytest.
Run the tests:
sh run_tests.shThis FastAPI application provides a robust solution for interacting with PDF documents using AI technologies. Follow the setup instructions and utilize the API endpoints to leverage its capabilities effectively.