rstudio / vetiver-r

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

Error: lexical error: invalid char in json text #285

Open b-rodrigues opened 1 month ago

b-rodrigues commented 1 month ago

Hello

trying to predict from an endpoint results in the following error:

Error: lexical error: invalid char in json text.
                                       <!DOCTYPE html PUBLIC "-//W3C//
                     (right here) ------^

The model is a {glmnet} model trained using a {tidymodels} workflow and with {targets}.

I can use the web interface to get my predictions:

image

but trying from an R session using predict() on a data.frame results in the error above.

This is what I’m doing: the fitted workflow is saved in a variable which I passed to vetiver_model() and called model_to_deploy:

> model_to_deploy

── glmnet_fit ─ <bundled_workflow> model for deployment 
A glmnet classification modeling workflow using 14 features

Now I serve the model locally using {plumber}:

library(plumber)

vetiver_api(pr(),model_to_deploy) %>% pr_run(port = 8088) 

Running plumber API at http://127.0.0.1:8088
Running rapidoc Docs at http://127.0.0.1:8088/__docs__/

from another R session, I load some test data and remove the target:

pred_set <- testing_set[1,-15]

and define the endpoint:

ep <- vetiver_endpoint("http://127.0.0.1:8088/predict")

finally, I try to predict:

predict(ep, pred_set, encode = "json")
Error: lexical error: invalid char in json text.
                                       <!DOCTYPE html PUBLIC "-//W3C//
                     (right here) ------^

What am I missing?

EDIT:

Actually I just tried running the example from the readme and get the same error. I’m testing this on my work windows laptop, could this somehow be related?

b-rodrigues commented 1 month ago

EDIT: it is likely due to some configuration on my work laptop, I’ll have to try on another machine. But would there be a way to circumvent this issue?

juliasilge commented 1 month ago

This kind of error usually is because you need to authenticate your predict() API call. If you are using Posit Connect, it will typically look like this:

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))
)

Do you think you need pass in authentication details?

b-rodrigues commented 1 month ago

Thanks, but I don't think that's it, this is running locally. I think there might be some configuration on my work laptop that somehow blocks POST calls, I haven't had the chance to try on another machine yet

juliasilge commented 1 month ago

Ah gotcha, sorry that I missed that earlier. Yes, that sounds like something about your local setup. You may want to make a simpler example (maybe not even using R) to show IT folks that you can't test local API calls.

In terms of circumventing the problem, I wonder if you can authenticate somehow as the local user?

jrosell commented 1 month ago

Thanks, but I don't think that's it, this is running locally. I think there might be some configuration on my work laptop that somehow blocks POST calls, I haven't had the chance to try on another machine yet

@b-rodrigues I had an idea. Can you try to make the calls from inside a running docker container?

b-rodrigues commented 1 month ago

Thanks, but I don't think that's it, this is running locally. I think there might be some configuration on my work laptop that somehow blocks POST calls, I haven't had the chance to try on another machine yet

@b-rodrigues I had an idea. Can you try to make the calls from inside a running docker container?

That’s a great idea, and indeed, doing the call from an R session running inside a container to the api running from the same container succeeds!

Ah gotcha, sorry that I missed that earlier. Yes, that sounds like something about your local setup. You may want to make a simpler example (maybe not even using R) to show IT folks that you can't test local API calls.

Yes, there’s definitely something going on, I’ll try to set something up and show it to them, see what can be done.

In terms of circumventing the problem, I wonder if you can authenticate somehow as the local user?

You mean, using my windows user credentials?