hr_chain() turns steps into an hr_arm() whose run passes the item to
the first step, what that returns to the second, and so on, and returns
what the last step returns. Each step is called as step(x, ctx), where
ctx is the arm's context with ctx$step (the step's name) and
ctx$step_params (its resolved settings) added; ctx$params stays the
arm's params.
Value
An hr_arm() with the attribute hr_steps, the named list of its
steps in order.
Details
Steps come from hr_step(), gr_step() or gr_step_retrieve(). A named
argument names its step, and the name is written into the step; an
unnamed one keeps the step's own name. Names must be unique, must not
contain a dot (a setting is named "step.setting") and must not be
name or version.
Given a chain first, hr_chain() derives a new chain from it: x = NULL
drops step x, x = step replaces step x where it stands, and a step
under a new name is added at the end. So hr_chain(checked, judge = NULL, name = "nojudge") is checked without its judge.
The order is checked when the chain is built, from what each step
declares it takes and returns. A step that takes "chunks" when no step
before it returns chunks warns with class hr_chain_gap: a gr_step()
read step would cut the document at the default settings without saying
so. A step that takes "doc" with no step before it returning one does
not warn: a chain usually starts at the segment step, which reads the
document at the default ingest settings, and a warning there would fire
on almost every chain. Add an ingest step first to choose those
settings. After a step that returns "answer", only steps that take
"answer" or "state", or declare nothing, may follow; any other is an
error of class hr_bad_chain. A held command step (see
hr_step_command()) must come last, or be followed only by other held
steps; any other step after it is an error of class hr_held_not_last.
The arm records each step's settings as given, as "step.setting", in
params and as the settings hr_benchmark() turns into param.*
columns. Its configuration, which hr_sweep() and hr_benchmark()
compare, is a hash of the steps' names, versions, code, the values that
code reads and the resolved settings, so a setting given at its default
gives the same configuration as the setting left out. hr_settings()
lists every setting of the chain by path, hr_set() changes some of
them, and hr_sweep() sweeps them by path, as
hr_sweep(chain, data.frame(retrieve.top_k = c(2, 8))).
See also
hr_step(), gr_step(), hr_settings(), hr_set(), hr_benchmark(),
vignette("compose")
Other harness:
gr_arm(),
gr_condensation_check(),
gr_qa_reference(),
gr_reverse_text(),
gr_scorer_answer(),
hr_arm(),
hr_benchmark(),
hr_contrast(),
hr_drift_check(),
hr_effort_check(),
hr_effort_escalate(),
hr_effort_event(),
hr_effort_log(),
hr_mdd(),
hr_modules(),
hr_reference(),
hr_register(),
hr_report(),
hr_scorer(),
hr_set(),
hr_settings(),
hr_skill(),
hr_step(),
hr_step_command(),
hr_sweep(),
hr_sweep_check()
Examples
cl <- hr_mock_client(function(m, p) "Revenue was 45.2 million dollars.")
ref <- gr_qa_reference(
questions = c(revenue = "What was revenue in 2024?"),
documents = gr_example(), answers = list("45.2 million"), split = "test")
tidy <- hr_step("tidy", function(a, ctx) {
a$answer <- trimws(a$answer)
a
}, input = "answer", output = "answer")
checked <- hr_chain(segment = gr_step("segment"),
retrieve = gr_step_retrieve(top_k = 4),
read = gr_step("read", reader = "stuff"),
tidy = tidy, name = "checked")
plain <- hr_chain(checked, tidy = NULL, name = "plain")
names(attr(plain, "hr_steps"))
#> [1] "segment" "retrieve" "read"
old <- hr_options(verbose = FALSE)
bench <- hr_benchmark(list(checked, plain), ref, runs = 5, client = cl,
scorer = gr_scorer_answer())
table(bench$arm, bench$correct)
#>
#> TRUE
#> checked 5
#> plain 5
hr_options(old)