Vetiver
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
= rsconnect.api.RSConnectServer(url=url, api_key=api_key)
connect_server
vetiver.deploy_rsconnect(=connect_server,
connect_server=board,
board="my.username/pin_name"
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
= vetiver_endpoint(
endpoint f"https://connect.example.com/content/{APP_ID}/predict"
)
= {"Authorization": f"Key {api_key}"}
h = predict(endpoint=endpoint, data=new_data, headers=h) response
<- vetiver_endpoint(
endpoint "https://connect.example.com/content/$APP_ID/predict")
<- Sys.getenv("CONNECT_API_KEY")
apiKey
predict(
endpoint,
new_data,::add_headers(Authorization = paste("Key", apiKey))) httr
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
= RSConnectServer(url=rsc_url, api_key=api_key)
connect_server = pins.board_connect(
board =rsc_url, api_key=api_key, allow_pickle_read=True
server_url
)
= LinearRegression().fit(mtcars, mtcars["mpg"])
cars_lm = vetiver.VetiverModel(
v ="my.username/cars_mpg", prototype_data=mtcars
cars_lm, model_name
)
vetiver.vetiver_pin_write(board, v)
vetiver.deploy_rsconnect(=connect_server,
connect_server=board,
board="my.username/cars_mpg"
pin_name )
library(vetiver)
library(pins)
<- lm(mpg ~ ., data = mtcars)
cars_lm <- vetiver_model(cars_lm, "cars_mpg")
v <- board_connect()
b vetiver_pin_write(b, v)
vetiver_deploy_rsconnect(
b, "my.username/cars_mpg",
predict_args = list(debug = TRUE),
account = "my.username"
)