rstudio / vetiver-r

Version, share, deploy, and monitor models
https://rstudio.github.io/vetiver-r/
Other
180 stars 27 forks source link

Use of promises package inside vetiver api #252

Closed joscani closed 3 weeks ago

joscani commented 11 months ago

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)
  })
}
juliasilge commented 11 months ago

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:

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.

joscani commented 10 months ago

Interestint. I'll try this weekend.

Thanks @juliasilge

juliasilge commented 3 weeks ago

Let us know if you have further questions!