dsprrr integrates with tidymodels in three places: scoring module
predictions with yardstick, building parameter grids for
optimize_grid() with dials, and wrapping
zero-shot prediction modules in the llm_predict()
parsnip model specification.
Score predictions with yardstick
Run the module over a data frame with run_dataset(),
which keeps every column and adds a result list-column.
Turn the predicted field into a factor with the truth’s levels, and any
yardstick metric works:
library(yardstick)
reviews <- tibble::tibble(
text = c("Arrived broken.", "Works great.", "It's a chair."),
sentiment = factor(c("negative", "positive", "neutral"))
)
classify <- module(
signature("text -> sentiment: enum('negative', 'neutral', 'positive')")
)
scored <- run_dataset(
classify,
reviews,
.llm = ellmer::chat_openai(model = "gpt-6-luna")
)
scored$.pred_class <- factor(
vapply(
scored$result,
\(r) if (is.list(r)) r$sentiment else NA_character_,
character(1)
),
levels = levels(scored$sentiment)
)
accuracy(scored, truth = sentiment, estimate = .pred_class)
conf_mat(scored, truth = sentiment, estimate = .pred_class)A row whose call failed holds NA in result,
hence the is.list() check. Setting levels
matters: yardstick refuses a prediction factor whose levels
differ from the truth’s, which happens whenever some class is never
predicted.
Build grids with dials
temperature(), top_p(), and
reasoning_effort() are dials parameter objects
(temperature() and top_p() default to
[0, 1]):
temperature()
#> Temperature (quantitative)
#> Range: [0, 1]
reasoning_effort()
#> Reasoning Effort (qualitative)
#> 3 possible values include:
#> 'low', 'medium', and 'high'
dials::grid_regular(temperature(), levels = 3)
#> # A tibble: 3 × 1
#> temperature
#> <dbl>
#> 1 0
#> 2 0.5
#> 3 1module_parameters() suggests a dials
parameter set for a module, swapping
temperature/top_p for
reasoning_effort when model looks like a
reasoning model (pass include to pick only the knobs you
want):
classify <- module(
signature("text -> sentiment: enum('negative', 'neutral', 'positive')")
)
knobs <- c("temperature", "top_p", "reasoning_effort")
module_parameters(classify, model = "claude-sonnet-4-5", include = knobs)
#> Collection of 2 parameters for tuning
#>
#> identifier type object
#> temperature temperature dparam[+]
#> top_p top_p dparam[+]
#>
module_parameters(classify, model = "gpt-6-luna", include = knobs)
#> Collection of 1 parameters for tuning
#> identifier type object
#> reasoning_effort reasoning_effort dparam[+]
#> optimize_grid() accepts a dials grid
directly, or a dials::parameters() set to build one from.
It evaluates each row and keeps the best settings on the module:
optimize_grid(
classify,
data = reviews,
metric = metric_exact_match(field = "sentiment"),
grid = dials::grid_regular(temperature(), levels = 3),
# gpt-6-luna accepts temperature only with reasoning turned off
.llm = ellmer::chat_openai(
model = "gpt-6-luna",
params = ellmer::params(reasoning_effort = "none")
)
)
module_trials(classify)To tune how much a reasoning model thinks instead, sweep a
reasoning_effort() grid; see Models, providers and
streaming.
The parsnip model
When parsnip is loaded, dsprrr registers a model type,
llm_predict(), with the "dsprrr" engine for
classification and regression:
library(parsnip)
reviews <- tibble::tibble(
text = c("Arrived broken.", "Works great.", "It's a chair."),
sentiment = factor(c("negative", "positive", "neutral"))
)
spec <- llm_predict(
mode = "classification",
signature = "text -> sentiment: enum('negative', 'neutral', 'positive')",
temperature = 0
) |>
set_engine("dsprrr")
fitted <- fit_xy(spec, x = reviews["text"], y = reviews$sentiment)
fitted
#> parsnip model object
#> ── PredictModule ──
#>
#> ── Signature
#>
#> ── Signature ──
#>
#> ── Inputs
#> • text: "string" - Input: text
#>
#> ── Output
#> Type: "object(sentiment: enum(negative, neutral, positive))"
#>
#> ── Instructions
#> Given the fields `text`, produce the fields `sentiment`.Fitting builds a zero-shot module from signature (or,
without one, derives it from the predictor names and outcome levels),
stores temperature and top_p in the module’s
config, and makes no model calls. Use fit_xy() (or
workflows::add_variables()) rather than a formula for
free-text columns: R’s formula interface converts character predictors
into factors with fixed training levels, which rejects unseen text at
prediction time.
predict() runs the module on new_data using
the default Chat (set with set_default_chat() or
local_lm()) and returns .pred_class
(preserving the training factor’s levels) or .pred:
set_default_chat(
ellmer::chat_openai(
model = "gpt-6-luna",
params = ellmer::params(reasoning_effort = "none")
)
)
predict(fitted, new_data = tibble::tibble(text = c("Best purchase ever!", "Total junk.")))
#> # A tibble: 2 x 1
#> .pred_class
#> <fct>
#> 1 positive
#> 2 negativeFor evaluation with graded or repeated samples, see Evaluate with vitals.