Skip to contents

A memoised version of call_llm to avoid repeated identical requests.

Usage

cache_llm_call(config, messages, verbose = FALSE)

Arguments

config

An llm_config object from llm_config.

messages

A list of message objects or character vector for embeddings.

verbose

Logical. If TRUE, prints the full API response (passed to call_llm).

Value

The (memoised) response object from call_llm.

Details

  • Requires the memoise package. Add memoise to your package's DESCRIPTION.

  • Clearing the cache can be done via memoise::forget(cache_llm_call) or by restarting your R session.

  • The cache lives in the R process that makes the call. Under a future multisession plan (as used by call_llm_par), each worker process keeps its own cache, which disappears with the worker; identical requests in different workers are not deduplicated.

Examples

if (FALSE) { # \dontrun{
  # Using cache_llm_call:
  response1 <- cache_llm_call(my_config, list(list(role="user", content="Hello!")))
  # Subsequent identical calls won't hit the API unless we clear the cache.
  response2 <- cache_llm_call(my_config, list(list(role="user", content="Hello!")))
} # }