pip install bentoml★Python 3.9+. Installs the framework + thebentomlCLI. Add framework extras as needed (e.g.bentoml[triton]).import bentoml @bentoml.service class Summarizer: @bentoml.api def summarize(self, text: str) -> str: return do_summary(text)★A whole service in one class. Save asservice.py; the class name is the service.bentoml serve service:Summarizer★Starts a dev server on:3000with Swagger UI at/. Syntax is<file>:<ClassName>.curl -X POST localhost:3000/summarize -H "Content-Type: application/json" \ -d '{"text":"long text..."}'Each@apimethod becomes aPOST /<method>endpoint that takes JSON matching its params.
@bentoml.service(name="summarizer") class S: def __init__(self): self.model = load() # runs once per worker at startup★__init__loads models/state once when the service starts — not per request.@bentoml.api def predict(self, x: int) -> int: ...★Only@bentoml.apimethods are exposed; helpers stay private. One service can have many endpoints.@bentoml.api(route="/v1/generate")Override the URL path. Default route is the method name.with bentoml.importing(): import torch # heavy/optional import, only at runtimebentoml.importing()defers imports that shouldn't run at build time (e.g. GPU libs).
@bentoml.api def f(self, name: str, n: int = 1) -> dict: ...★Type hints define & validate the request/response schema automatically (via Pydantic under the hood).from pydantic import BaseModel class In(BaseModel): prompt: str; temp: float = 0.7 @bentoml.api def gen(self, params: In) -> str: ...★Use a Pydantic model for structured/validated input with defaults & constraints.from PIL import Image import numpy as np def classify(self, img: Image.Image) -> np.ndarray: ...Rich types just work: PIL images, numpy arrays, pandas DataFrames, andpathlib.Pathfor file uploads/downloads.from bentoml import File def transcribe(self, audio: File) -> str: ...Accept raw file uploads with theFile/Pathtypes; return files the same way.