Vetiver

Enhanced Advanced

Vetiver is a framework for MLOps tasks in Python and R. The goal of vetiver is to provide fluent tooling to version, share, deploy, and monitor a trained model. Functions handle both recording and checking the model’s input data prototype, and predicting from a remote API endpoint.

Vetiver integrates with pins in Python and pins in R to support model versioning. When you write a vetiver model to a board, the binary model object is stored together with necessary metadata, including its version, the packages needed to make a prediction, and the model’s input data prototype for checking new data at prediction time.

Deploy

To deploy a vetiver API to Connect, we recommend that users first pin their model to a board, possibly board_connect() in Python or in R. From the pin info, the vetiver package can deploy the API.

import vetiver
import rsconnect

connect_server = rsconnect.api.RSConnectServer(url=url, api_key=api_key)

vetiver.deploy_rsconnect(
    connect_server=connect_server,
    board=board,
    pin_name="my.username/pin_name"
)
library(vetiver)

vetiver_deploy_rsconnect(
    board = board, 
    name = "my.username/pin_name",
    predict_args = list(debug = TRUE),
    account = "my.username"
)

The username for the pin and the account deploying the model API do not have to be the same.

Predict from your model endpoint

A model deployed via vetiver can be treated as a special vetiver_endpoint() object. If the API is only accessible to specified people, use an API key for authorization. You can use predict() to make predictions from your remote endpoint with your new data.

from vetiver.server import predict, vetiver_endpoint

endpoint = vetiver_endpoint(
    f"https://connect.example.com/content/{APP_ID}/predict"
)

h = {"Authorization": f"Key {api_key}"}
response = predict(endpoint=endpoint, data=new_data, headers=h)
endpoint <- vetiver_endpoint(
  "https://connect.example.com/content/$APP_ID/predict")
apiKey <- Sys.getenv("CONNECT_API_KEY")

predict(
  endpoint,
  new_data,
  httr::add_headers(Authorization = paste("Key", apiKey)))

Example

To run this example, you must have your Connect URL and API key loaded in your environment.

import pins
import vetiver
from vetiver.data import mtcars
from rsconnect.api import RSConnectServer
from sklearn.linear_model import LinearRegression

connect_server = RSConnectServer(url=rsc_url, api_key=api_key)
board = pins.board_connect(
    server_url=rsc_url, api_key=api_key, allow_pickle_read=True
)

cars_lm = LinearRegression().fit(mtcars, mtcars["mpg"])
v = vetiver.VetiverModel(
    cars_lm, model_name="my.username/cars_mpg", prototype_data=mtcars
)

vetiver.vetiver_pin_write(board, v)
vetiver.deploy_rsconnect(
    connect_server=connect_server,
    board=board,
    pin_name="my.username/cars_mpg"
)
library(vetiver)
library(pins)

cars_lm <- lm(mpg ~ ., data = mtcars)
v <- vetiver_model(cars_lm, "cars_mpg")
b <- board_connect()
vetiver_pin_write(b, v)

vetiver_deploy_rsconnect( 
     b, 
     "my.username/cars_mpg", 
     predict_args = list(debug = TRUE), 
     account = "my.username" 
)