call_llm {LLMR} | R Documentation |
Call LLM API
Description
Send text or multimodal messages to a supported Large-Language-Model (LLM) service and retrieve either a chat/completion response or a set of embeddings.
Usage
call_llm(config, messages, verbose = FALSE, json = FALSE)
Arguments
config |
An |
messages |
Character vector, named vector, or list of message objects as described above. |
verbose |
Logical; if |
json |
Logical; if |
Details
Generative vs. embedding mode
-
Generative calls (the default) go to the provider's chat/completions endpoint. Any extra model-specific parameters supplied through
...
inllm_config()
(for exampletemperature
,top_p
,max_tokens
) are forwarded verbatim to the request body. For reasoning, some provider have shared model names and ask for a config argument that indicates reasoning should be enabled, others have dedicated model names for reasoning-enabled models, and may or may not allow for another argument that indicates the effort level. -
Embedding calls are triggered when
config$embedding
isTRUE
or the model name contains the string "embedding". These calls are routed to the provider's embedding endpoint and return raw embedding data. At present, extra parameters are not passed through to embedding endpoints.
Messages argument
messages
can be
a plain character vector (each element becomes a user message),
a named character vector whose names are interpreted as roles, or
a list of explicit message objects (
list(role = ..., content = ...)
).
For multimodal requests you may either use the classic list-of-parts or
(simpler) pass a named character vector where any element whose name is
"file"
is treated as a local file path and uploaded with the request (see
Example 3 below).
Value
-
Generative mode: a character string (assistant reply). When
json = TRUE
, the string has attributesraw_json
(JSON text) andfull_response
(parsed list). call_llm supports reasning models as well, but whether the output of these models include the text of the reasning or not depends on the provider. -
Embedding mode: a list with element
data
, compatible withparse_embeddings()
.
See Also
llm_config
to create the configuration object.
call_llm_robust
for a version with automatic retries for rate limits.
call_llm_par
helper that loops over texts and stitches
embedding results into a matrix.
Examples
## Not run:
## 1. Generative call ------------------------------------------------------
cfg <- llm_config("openai", "gpt-4o-mini", Sys.getenv("OPENAI_API_KEY"))
call_llm(cfg, "Hello!")
## 2. Embedding call -------------------------------------------------------
embed_cfg <- llm_config(
provider = "gemini",
model = "gemini-embedding-001",
api_key = Sys.getenv("GEMINI_KEY"),
embedding = TRUE
)
emb <- call_llm(embed_cfg, "This is a test sentence.")
parse_embeddings(emb)
## 3. Multimodal call (named-vector shortcut) -----------------------------
cfg <- llm_config(
provider = "openai",
model = "gpt-4.1-mini",
api_key = Sys.getenv("OPENAI_API_KEY")
)
msg <- c(
system = "you answer in rhymes",
user = "interpret this. Is there a joke here?",
file = "path/to/local_image.png")
call_llm(cfg, msg)
## 4. Reasoning example
cfg_reason <- llm_config("openai",
"o4-mini",
Sys.getenv("OPENAI_API_KEY"),
reasoning_effort = 'low')
call_llm(cfg_reason,
c(system='Only give an integer number. Nothing else',
user='Count "s" letters in Mississippi'))
## End(Not run)