Deployment and Project Integration · Lesson 2
Building APIs with FastAPI
FastAPI is a Python web framework for building APIs quickly. It uses type hints and Pydantic models to validate requests and generate interactive docs at /docs automatically.
It supports async endpoints, which suits LLM calls that wait on the network, and streaming responses for token-by-token output.
Run it with Uvicorn. Add input validation, authentication, rate limiting and clear error responses before exposing it.
Key points
- Pydantic models validate input and output
- Automatic OpenAPI docs at /docs
- Async endpoints suit network-bound LLM calls
- StreamingResponse for live tokens
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Ask(BaseModel):
question: str
@app.post("/ask")
async def ask(body: Ask):
answer = await rag_answer(body.question)
return {"answer": answer}
# run: uvicorn main:app --reload