| llm_fn | R Documentation |
Apply an LLM prompt over vectors/data frames
llm_fn(
x,
prompt,
.config,
.system_prompt = NULL,
...,
.tags = NULL,
.fields = NULL,
.return = c("text", "columns", "object"),
.na_action = c("send", "skip", "error"),
.rows_per_prompt = 1L,
.rowpack_payload = c("user", "system"),
.rowpack_recovery = c("halve_recursive", "halve_once", "singletons", "retry_same",
"none")
)
x |
A character vector or a data.frame/tibble. |
prompt |
A glue template string. With a data-frame you may reference
columns ( |
.config |
An llm_config object. |
.system_prompt |
Optional system message (character scalar). |
... |
Passed unchanged to |
.tags |
Optional character vector of XML-like tag names to request and parse.
When supplied, delegates to |
.fields |
Optional field selector for tag extraction (see |
.return |
One of |
.na_action |
What to do with elements whose template references an |
.rows_per_prompt |
Integer scalar, or |
.rowpack_payload |
One of |
.rowpack_recovery |
How to handle rows that a batched call leaves unresolved (dropped, malformed, or truncated). One of:
Recovery is bounded by an internal call budget so it always terminates. |
For generative mode:
.return = "text": character vector
.return = "columns": tibble with diagnostics
.return = "object": list of llmr_response (or NA on failure;
unavailable when .rows_per_prompt > 1)
For embedding mode, always a numeric matrix.
"Batch" appears in three distinct senses in LLMR, and they are easy to keep
apart once you note which path each belongs to. (1) The asynchronous provider
Batch API (llm_batch_submit() and friends) defers a whole job for later
delivery at a reduced price; it is the only deferred path here. (2) In the
embedding path, get_batched_embeddings()'s batch_size sets how many texts
go in one synchronous embedding request, bounded by the provider's per-call
limit. (3) In the generative path, the .rows_per_prompt argument here packs
several data rows into one generative prompt and parses them back into rows.
Senses (2) and (3) are synchronous: batch_size and .rows_per_prompt
control how work is grouped into requests, not the per-token rate, so the
synchronous helpers do not themselves apply a provider discount. Pricing is a
separate axis: several providers bill batched embeddings at a reduced rate
through a dedicated async/batch tier (the embedding analogue of (1)), so
embeddings are not categorically full-price; that discount is a property of
the endpoint, not of batch_size. Row packing (3) can still lower total
tokens by amortizing shared prompt overhead across rows, again without
changing the rate.
With .rows_per_prompt > 1, several input elements travel in one generative
request: LLMR wraps each element's prompt in a numbered tag,
<row_1>...</row_1>, <row_2>...</row_2>, and so on, appends that block to
the message (see .rowpack_payload), and instructs the model to answer each
item inside a matching numbered tag. The reply is split back into the
original elements by those numbers. Batching trades a smaller number of
(larger) requests for some dependence on the model following the protocol; it
is most useful with capable models at temperature = 0, and it is a net loss
when the model ignores the wrapping. Results are deterministic given the
model's outputs: partitioning and parsing add no randomness. Rows the model
drops, reorders, duplicates, or truncates are detected and re-issued
according to .rowpack_recovery. Because a batch shares one underlying call,
token counts are reported once per batch (on its first resolved row, NA
elsewhere), as is the wall-clock duration, so that summing those columns is
correct. When a batch reply is entirely unusable and its rows succeed only
through recovery calls, the failed call's spend has no successful row to
land on, so sums can slightly undercount in heavy-recovery runs.
llm_mutate(), llm_fn_structured(), llm_fn_tags(),
llm_parse_rowpack_tags(), setup_llm_parallel(), call_llm_broadcast(),
get_batched_embeddings()
## Not run:
words <- c("excellent", "awful")
cfg <- llm_config("groq", "openai/gpt-oss-20b", temperature = 0)
llm_fn(words, "Classify '{x}' as Positive/Negative.", cfg, .return = "text")
df <- tibble::tibble(text = words, source = c("review", "review"))
llm_fn(df, "Classify '{text}' from {source}.", cfg, .return = "columns")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.