Temporarily sets a default LLM for all dsprrr operations within a code block.
Similar to DSPy's dspy.context(lm=lm) context manager. When the block exits
(normally or due to an error), the previous LM is restored.
Details
The scoped LM has higher priority than options(dsprrr.default_chat) and
auto-detection, but lower priority than an explicit .llm parameter.
Resolution order (highest to lowest):
Explicit
.llmparameterScoped LM from
with_lm()/local_lm()Module's stored
$chatoptions(dsprrr.default_chat)Auto-detection from environment variables
Examples
if (FALSE) { # \dontrun{
# Use Claude for a specific block
claude <- ellmer::chat_claude()
result <- with_lm(claude, {
dsp("question -> answer", question = "What is 2+2?")
dsp("text -> summary", text = "Long article...")
})
# Nested contexts work correctly
gpt4 <- ellmer::chat_openai(model = "gpt-4o")
with_lm(gpt4, {
run(mod1, text = "outer uses gpt4")
with_lm(claude, {
run(mod2, text = "inner uses claude")
})
run(mod3, text = "back to gpt4")
})
} # }
