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 llm_config object created by llm_config().

messages

Character vector, named vector, or list of message objects as described above.

verbose

Logical; if TRUE, prints the full API response.

json

Logical; if TRUE, returns the raw JSON with attributes.

Details

Generative vs. embedding mode

Messages argument

messages can be

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

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)

[Package LLMR version 0.5.0 Index]