02. FastAPI Backend Setup

Chapter 2 of 15 · 15 min

Create the project directory and install dependencies:

mkdir chatbot && cd chatbot
python3 -m venv venv
source venv/bin/activate   # on Windows: venv\Scripts\activate
pip install fastapi uvicorn httpx python-multipart

Create app/main.py:

from fastapi import FastAPI

app = FastAPI()

@app.get("/health")
def health():
    return {"status": "ok"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

Run it:

uvicorn app.main:app --reload

The --reload flag restarts the server when you edit files, which saves time during development. Test with curl http://localhost:8000/health.

A common failure: if port 8000 is already in use, uvicorn exits with OSError: [Errno 98] Address already in use. Fix it with fuser -k 8000/tcp on Linux or find the process and kill it.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Create a new route GET /models that returns a hardcoded list ["llama3", "mistral"]. Verify it appears in the OpenAPI docs at http://localhost:8000/docs.