Skip to main content

1. What is FastAPI?

FastAPI is a Python web framework used to build APIs. It is particularly useful for ML/LLM applications because it provides:
  • High performance
  • Automatic API documentation
  • Request/response validation
  • Easy integration with Python ML libraries
  • Asynchronous programming support
A typical LLM inference architecture looks like:

2. Installing FastAPI

Create a virtual environment and install the required packages:
For Hugging Face models:

3. Creating a Basic FastAPI App

Create main.py:
Run the server:
The API will be available at:
FastAPI also automatically provides interactive documentation at:
and:

4. Creating API Endpoints

For example, create a simple greeting endpoint:
Request:
Response:

5. POST Request with Input Data

LLM APIs normally receive prompts using POST. FastAPI uses Pydantic models to validate request data.
Example request:
Response:

6. Loading a Hugging Face Model

Hugging Face’s transformers library provides pretrained models and tokenizers. For demonstration, we can use a small text-generation model.
Here:
  • AutoTokenizer converts text into tokens.
  • AutoModelForCausalLM loads a causal language model.
  • distilgpt2 is a relatively small model suitable for demonstration.
For production, you would generally choose a model based on your hardware, latency, context length, licensing, and quality requirements.

7. Generating Text with the Hugging Face Model

The basic inference process is:
The process is:

8. Building the LLM Inference API

Now combine FastAPI and Hugging Face. main.py:
Run it:

9. Testing the API

Using curl:
Example response:
You can also test it interactively through:

10. Improving the Generation Parameters

Hugging Face’s generate() supports several useful parameters.
Important parameters: For example:

11. Running the Model on GPU

For larger models, GPU inference is usually necessary.
Then move the inputs to the same device:
Then:

12. A Better Production-Oriented Version

You don’t want to repeatedly load the model for every request. Instead, load it once when the application starts.
Then your endpoint can use the already-loaded model:
This avoids loading the model on every API request.

13. Using a Chat-Style API

For an LLM application, you may want an API resembling a chat endpoint. Request:
Pydantic models can represent this:
Endpoint:

14. Project Structure

A small LLM API can be organized like this:

schemas.py

model.py

main.py

Run:

15. Production Architecture

For a real LLM deployment, the architecture is usually closer to:
For larger production LLMs, you may use a dedicated inference engine rather than directly calling model.generate() inside a FastAPI process. Examples include Hugging Face’s Text Generation Inference (TGI) and other optimized serving frameworks.

16. Key Concepts to Remember

FastAPI

Hugging Face inference

LLM API

Most important practical point: load the model once, not once per request; keep model/tokenizer initialization outside the request handler, use the appropriate device (cuda when available), and for large production models consider a specialized inference server rather than running raw Transformers generation directly inside FastAPI.