Definire un servizio e la relativa API
Esponga i runner tramite un endpoint di servizio tipizzato.
Definire un servizio e la relativa API è una lezione MLOps Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento MLOps Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso MLOps Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
What a Service Is
In BentoML a Service is the unit you deploy. It wraps your model and exposes one or more API endpoints clients can call. 🛎️
Runners Run the Model
A runner is BentoML's optimized wrapper around a saved model. It handles inference in its own worker so the API stays responsive.
runner = bentoml.sklearn.get("iris_clf:latest").to_runner()Create the Service
You build a service by naming it and passing its runners. The Service object is what BentoML serves and scales.
svc = bentoml.Service("iris_classifier", runners=[runner])Add an Endpoint
You expose a function as an API with the @svc.api decorator. That function becomes a callable HTTP route.
@svc.api(input=..., output=...)
def classify(data):
...Declare Input and Output
You set typed IO descriptors so BentoML can validate and serialize. NumpyNdarray tells it to expect and return arrays.
from bentoml.io import NumpyNdarrayCall the Runner Inside
Within the API function you invoke the model through the runner. Using run sends the input to the model worker and returns the result.
result = runner.predict.run(data)
return resultPut It in service.py
BentoML looks for your code in a file by convention. You place the service in service.py at your project root.
Serve It Locally
One command starts a dev server. bentoml serve points at the file and the svc object, then watches for changes.
bentoml serve service:svc --reloadUse JSON for Dicts
Not every API speaks arrays. Swap the descriptor to JSON when your clients send and expect dictionaries instead.
from bentoml.io import JSONBuilt-In Swagger Docs
Once it runs, BentoML serves interactive Swagger docs at the root URL so you can test each endpoint in the browser. 🎉
Test with a curl Call
You can hit the live endpoint from your terminal. A quick curl POST sends sample input and shows the prediction it returns.
curl -X POST localhost:3000/classify -d "[[5.1,3.5,1.4,0.2]]"Quick Check
You want to call your model from inside an API function. What is the right wrapper to use?
Recap
You turned a runner into a Service, exposed an @svc.api endpoint with typed IO, dropped it in service.py, and served it with built-in docs. Live API done! 🙌
Domande Frequenti
La lezione «Definire un servizio e la relativa API» è gratuita?
Sì — il testo completo di «Definire un servizio e la relativa API» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso MLOps Academy, passa a CoddyKit PRO. Il corso MLOps Academy include 4 lezioni in totale.
Cosa imparerò in «Definire un servizio e la relativa API»?
Esponga i runner tramite un endpoint di servizio tipizzato. Eserciti MLOps Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare MLOps Academy?
Non è richiesta alcuna esperienza precedente. MLOps Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Definire un servizio e la relativa API»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione MLOps Academy?
Sì. Ogni lezione MLOps Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Salvare un modello nel Bento Store
- Definire un servizio e la relativa API
- Abilitare il micro-batching adattivo
- Creare un Bento e containerizzarlo