A deterministic offline client for tests, demos and dry runs
Source:R/core-transport-mock.R
hr_mock_client.Rdhandler receives (messages, params) and returns either a string or a
gr_result. Every call is recorded in $calls(), so tests can assert on the
exact prompts a strategy produced, which is how you prove two reading
strategies are actually different.
Arguments
- handler
Function of
(messages, params)returning a string or a gr_result.- embed_handler
Function of
(texts, params)returning a numeric matrix with one row per input.- mode
NULL(the default) for replies as the handler gives them, or one of"bad_json","refuse","at_cap"and"overflow", or a vector of HTTP status codes such asc(429, 500, 200). See Details.
Details
Three things to know about the mock. It registers two model ids
("mock-model", "mock-embed") in the session's model registry the first
time it is called, so they appear in hr_models() afterwards. Its default
handler returns plain text, so readers that need JSON-schema output
(rerank, iterative) take their documented degraded path unless your
handler returns valid JSON for those prompts. And hr_embed() uses its
embed_handler unless hr_options(embedder =) names a registered embedder
(see the embedder argument of hr_embed()), reporting
embedding_source = "api", so an offline run gets semantic-shaped vectors
rather than the lexical fallback. A mock embed handler that fails or returns
the wrong number of rows is still caught and still degrades, like any other.
mode makes the mock misbehave the way a provider can, so you can test how
your code copes offline. It changes chat replies only; embeddings are left
as they are.
"bad_json"Every reply is a JSON object cut off part way, so a request with a JSON schema gets a reply that does not parse.
"refuse"Every reply is empty, which
hr_call()reports as a failed result withfinish_reason = "empty", as it does a refusal from a provider."at_cap"Every reply is the handler's, marked as cut off at the output cap (
finish_reason = "length") and billed at the full cap. A failed gr_result from the handler comes back as it is."overflow"The first request fails with status 400 and the body OpenAI sends for a prompt longer than the model's context window, which
hr_call()reports with classhr_context_overflow. Every request after it gets the handler's reply, so a retry with a smaller prompt (seeon_overflowinhr_options()) succeeds.$reset()makes the next request fail again.- A vector of HTTP status codes
Call
igets thei-th status, and every call after the last gets the last one. A status from 200 to 299 gives the handler's reply; any other gives a failed result with that status, marked retryable for the statuses the HTTP transport retries (408, 409, 425, 429, 500, 502, 503 and 504). The mock does not retry, so each status is one call.$reset()starts the sequence again.
See also
hr_client(), hr_call(), gr_result, gr_example() for a
document to run against
Examples
cl <- hr_mock_client(function(messages, params) "mock answer")
hr_call(cl, list(list(role = "user", content = "hi")))$text
#> [1] "mock answer"
# `$calls()` is how you prove two reading strategies differ: it records the
# exact prompts each one sent.
cl$reset()
ch <- gr_segment(gr_example(), list(method = "paragraph", max_tokens = 150))
#> Using cached ingestion for this document + settings.
#> Segmenting with 'paragraph' (cap 150 tokens, overlap 0).
invisible(gr_read(ch, "What was revenue?", cl, "skim"))
#> Reading with 'skim' (all|N+1|none) over 5 chunk(s).
table(vapply(cl$calls(), function(x) x$label, character(1)))
#>
#> skim.answer skim.extract
#> 1 5
# A provider that is rate limited, then fails, then answers.
flaky <- hr_mock_client(mode = c(429, 500, 200))
vapply(1:3, function(i) hr_call(flaky, "hi")$ok, logical(1))
#> [1] FALSE FALSE TRUE