rstudio/vetiver-r

Use of promises package inside vetiver api

Closed this issue · 3 comments

Is there any way of use promises::future_promise in an endpoint of vetiver api?

In my regular plumbers api I have something like that

library(brms)
library(plumber)
library(tidybayes)

library(future)
library(promises)

future::plan(multicore, workers = 6, .cleanup = FALSE)

brms_model <- readRDS("brms_model.rds")

## more get and post endpoint ----
# ------
#---------------------------------------
#* @post /predict_async
function(req, res) {


  promises::future_promise({
    data <- tryCatch(
      RcppSimdJson::fparse(req$postBody),
      error = function(e)
        NULL
    )
    if (is.null(data)) {
      res$status <- 400
      return(list(error = "No data submitted"))
    }

    res <- predict(brms_model, data) |>
      as.data.frame()
    return(res)
  })
}

I haven't tried this myself, but we do provide a lot of hooks into the deployment process for more advanced use cases so I would be interested in hearing about how it goes for you. I think I would try a process like:

  • Create vetiver model object
  • Version model on the appropriate board for you
  • Create a default plumber.R and then edit it

Maybe something like this?

# Generated by the vetiver package; edit with care

library(pins)
library(plumber)
library(rapidoc)
library(vetiver)
library(future)
library(promises)

b <- board_connect(auth = "envvar")
v <- vetiver_pin_read(b, "user.name/model-name", version = "version-number")

#* @plumber
function(pr) {
    promises::future_promise({ pr %>% vetiver_api(v) })
}

Have you tried anything like this before?

Once you have your plumber file, you can deploy it in whatever way it appropriate for your infrastructure, like rsconnect::deployAPI() or using Docker or similar.

Interestint. I'll try this weekend.

Thanks @juliasilge

Let us know if you have further questions!