Tutorial 6: Save and reuse a module
Source:vignettes/tutorial-deploy-to-production.Rmd
tutorial-deploy-to-production.RmdCompiling a module produces demos, instructions, and tuned settings
that you do not want to recompute every time R restarts. This tutorial
shows how to save a compiled module to disk with
save_program(), share versioned copies on a pins board, restore it cleanly in a
new session, and inspect the traces it records as it runs.
library(dsprrr)
library(ellmer)
chat <- chat_openai(model = "gpt-6-luna")Recorded outputs on the package website were captured with
gpt-4.1; gpt-6-luna may answer slightly
differently.
Compile a module
Start from the sentiment classifier in Tutorial 5, compiled offline
with LabeledFewShot():
sig <- signature(
"review -> sentiment: enum('positive', 'negative', 'neutral')",
instructions = "Classify the sentiment of this product review."
)
trainset <- tibble::tribble(
~review, ~sentiment,
"Love it!", "positive",
"Hate it!", "negative",
"It's okay.", "neutral",
"Amazing!", "positive",
"Terrible!", "negative",
"Meh.", "neutral",
"Best ever!", "positive",
"Worst purchase!", "negative",
"Fine I guess.", "neutral"
)
classifier <- module(sig) |> compile(LabeledFewShot(k = 3L), trainset)Verify that the compiled classifier runs:
run(classifier, review = "This product is fantastic!", .llm = chat)
#> $sentiment
#> [1] "positive"Save to a file
The simplest persistence path needs no extra packages:
save_program() writes a validated, versioned program
artifact (signatures, demos, config, and optimization state) to an
.rds file, and load_program() rebuilds it in
any later session. program_artifact_id() returns its
SHA-256 content identity:
artifact_path <- tempfile(fileext = ".rds")
save_program(classifier, artifact_path)
loaded <- load_program(artifact_path)
identical(loaded$demos, classifier$demos)
#> [1] TRUE
identical(program_artifact_id(loaded), program_artifact_id(classifier))
#> [1] TRUEThe chat is intentionally not stored in the artifact—credentials
never touch disk—so pass .llm (or configure a default chat)
when you run the restored module:
run(loaded, review = "Worst product ever!", .llm = chat)
#> $sentiment
#> [1] "negative"Share versions with pins
When you want version history or shared team storage (such as S3,
Google Cloud Storage, or Posit Connect),
pin_module_config() writes the same program artifact to a
pins board. The board below is a temporary folder that R
deletes when the session ends:
board <- pins::board_temp()
pin_module_config(board, "sentiment-classifier", classifier)
#> Creating new version '20261126T011318Z-bb3fa'
#> Writing to pin 'sentiment-classifier'
#> ✔ Pinned program artifact: "sentiment-classifier"
#> ℹ Root module: <PredictModule>
#> ℹ Graph nodes: 1
#> ℹ Compiled: TRUEFor real use, pick a board that outlives the session:
board <- pins::board_local() # a folder in your user data directory
board <- pins::board_folder("pins") # or a folder in your projectShared boards such as pins::board_connect(),
pins::board_s3(), and pins::board_gcs() work
the same way. Each call to pin_module_config() with the
same name saves a new version:
pins::pin_versions(board, "sentiment-classifier") lists
them, and
pins::pin_read(board, "sentiment-classifier", version = "<id>")
reads an older one.
In a new session, connect to the same board, read the pin, and rebuild the module:
config <- pins::pin_read(board, "sentiment-classifier")
restored <- restore_module_config(config)
#> ✔ Restored program artifact
#> ℹ Root module: <PredictModule>
#> ℹ Artifact version: 6
identical(restored$demos, classifier$demos)
#> [1] TRUErestore_module_config() and load_program()
read the current artifact format (version 6), so keep the code that
compiled the module: if a future dsprrr release updates the format,
compile and save again. A module that holds R functions or tools also
needs a registry when you save and restore it; see
?program_artifact. For automated targets
pipelines, Quarto reports, and approval workflows, see Run dsprrr in pipelines.
Save and inspect traces
A module records a trace for each call it makes, with token counts, latency, cost, and model. Run the module a few times, then pin its traces next to the configuration:
run(classifier, review = "Great product!", .llm = chat)
#> $sentiment
#> [1] "positive"
run(classifier, review = "Not worth the money.", .llm = chat)
#> $sentiment
#> [1] "negative"
run(classifier, review = "Does the job.", .llm = chat)
#> $sentiment
#> [1] "neutral"
if (has_pins) {
pin_trace(board, "sentiment-traces", classifier)
}
#> Creating new version '20261126T011320Z-be0aa'
#> Writing to pin 'sentiment-traces'
#> ✔ Pinned 4 traces: "sentiment-traces"
#> ℹ Total tokens: 941export_traces() returns the same data as a tibble with
one row per call, including the call made right after compiling:
export_traces(classifier)
#> # A tibble: 4 × 11
#> timestamp latency_ms input_tokens cached_input_tokens output_tokens
#> <dbl> <dbl> <int> <int> <int>
#> 1 1795655598. 880. 101 0 7
#> 2 1795655599. 383. 200 0 7
#> 3 1795655600. 320. 271 0 7
#> 4 1795655600. 328. 341 0 7
#> # ℹ 6 more variables: total_tokens <int>, cost <dbl>, model <chr>,
#> # prompt_length <int>, program_artifact_id <chr>, trace_context <list>The tibble leaves out prompts and responses by default, so you can
share token and latency logs without exposing user text. Pass
include_prompts = TRUE or
include_outputs = TRUE (to export_traces() or
pin_trace()) when you need them. For a quick rollup of
calls, tokens, and spend:
summarize_traces(classifier)
#>
#> ── Module Trace Summary ──
#>
#> ── Usage Metrics
#> • 4 requests
#> • 941 total tokens
#> • Input/Output ratio: 0.03
#> • Total cost: $0.0001053
#>
#> ── Performance
#> • Average latency: 477.8ms
#> • Total time: 1.91s
#> • Tokens/request: 235.2
#>
#> ── Models Used
#> gpt-6-luna: 4 requestsWhere to go next
Across these six tutorials you have walked the full dsprrr loop:
declare a signature(), wrap it in module(),
run it on vectors and data frames, score it with
evaluate(), improve it with compile() or
optimize_grid(), and save the result for reuse. From
here:
- Need reasoning, tools, or multi-step workflows? Read Choose a module type and Chain modules into pipelines.
- Want stronger optimizers than grid search? Read Compile and optimize and Choose an optimizer.
-
Running dsprrr inside
targetsor Quarto? Read Run dsprrr in pipelines.