- Install FastAPI dependencies:
pip install fastapi uvicorn python-multipart- Or install all requirements:
pip install -r requirements.txtpython app.pyuvicorn app:app --host 0.0.0.0 --port 8000 --reload- GET
/ - Returns API information and available endpoints
- POST
/upload-pdf/ - Upload and process a PDF file
- Body: Form data with file upload
- POST
/add-url/ - Add and process a web URL
- Body:
{"url": "https://example.com"}
- POST
/ask/ - Ask a question based on processed documents
- Body:
{"question": "Your question here"}
- GET
/conversations/ - Retrieve all conversation history
- GET
/documents/ - Retrieve all processed documents
- GET
/health/ - Check if the service is running
# Upload a PDF
curl -X POST "http://localhost:8000/upload-pdf/" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@sample.pdf"
# Add a URL
curl -X POST "http://localhost:8000/add-url/" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com"}'
# Ask a question
curl -X POST "http://localhost:8000/ask/" \
-H "Content-Type: application/json" \
-d '{"question": "What is this document about?"}'
# Get conversations
curl -X GET "http://localhost:8000/conversations/"import requests
# Upload PDF
with open("sample.pdf", "rb") as f:
response = requests.post(
"http://localhost:8000/upload-pdf/",
files={"file": f}
)
# Add URL
response = requests.post(
"http://localhost:8000/add-url/",
json={"url": "https://example.com"}
)
# Ask question
response = requests.post(
"http://localhost:8000/ask/",
json={"question": "What is this document about?"}
)Once the server is running, visit:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Currently using a simple JSON file-based database (simple_db.json).
You can replace this with PostgreSQL, MongoDB, or any other database later.
Make sure your .env file contains:
OPENAI_API_KEY=your_openai_api_key
PINECONE_API_KEY=your_pinecone_api_key
- Create Dockerfile:
FROM python:3.9
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]- Build and run:
docker build -t rag-api .
docker run -p 8000:8000 rag-api