Lesson 3 of 6

Serving a model with plumber

In Lesson 2, Dev's meal-kit cancellation model finally had a safe home: versioned on a board, any past version one line away. But it is still stuck inside R. When a customer's subscription comes up for renewal, it is the billing system, a completely separate service, that needs to ask "is this customer about to cancel?" and it cannot load an R object.

This lesson builds the bridge: a small web service that puts the model behind a URL, so any system can send it a customer and get a prediction back.

By the end of this lesson you will be able to:

  • Explain why a stored model still cannot serve predictions to other systems, and what a REST API adds
  • Trace a prediction endpoint's contract: a JSON request in over HTTP, a JSON prediction out
  • Write a minimal plumber file, run it, and let vetiver generate one for you

Prerequisites: Lesson 2 (a versioned model and the vetiver bundle), and you can [fit a model and read predict output](Your-First-End-to-End-Model-in-R.html) and write a function.

The gap

The model works, but nothing can reach it

Dev's model predicts beautifully, as long as you are sitting inside his R session. The billing system is not. It might be written in Java, it runs on a different machine, and it has no idea what an .rds file is. Handing it the model object solves nothing.

What every other system already knows how to do is make a web request: send some data to a URL and read the reply. So we meet them there. We wrap the model in a REST API, a small program that listens at an address like http://models.internal/predict, and follows one simple contract:

  • A system sends a request to the URL, carrying the customer's details as JSON (a plain-text format every language can read and write).
  • The API runs the model on those details and gets a probability.
  • The API sends back a response, the prediction as JSON, over HTTP (the same protocol your browser uses to load a page).

That request-and-reply loop is the whole idea. A specific address you can POST a customer to, and a prediction that comes straight back.