| Title: | Persistent Cache for 'ellmer' and 'shinychat' Large Language Model Interactions |
|---|---|
| Description: | Provides a persistent SQLite-based cache for user prompts and large language model (LLM) responses in applications built with the 'ellmer' and 'shinychat' packages. Reduces API (application programming interface) costs and latency by storing and retrieving identical or similar chat interactions across sessions and users. |
| Authors: | Shekhar Chalwadi [aut, cre] |
| Maintainer: | Shekhar Chalwadi <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-11-19 03:50:28 UTC |
| Source: | https://github.com/cran/chatcache |
Checks the cache before calling an ellmer Chat object. On a miss, the model is queried and the result is stored.
cached_chat(chat, prompt, ..., use_history = TRUE)cached_chat(chat, prompt, ..., use_history = TRUE)
chat |
An ellmer Chat object. |
prompt |
Character. User prompt. |
... |
Additional arguments passed to |
use_history |
Logical. Whether to include conversation history in the
cache key. Default is |
Character string containing the response.
## Not run: library(ellmer) chat <- chat_openai(system_prompt = "Be concise.") answer <- cached_chat(chat, "What is the capital of France?") ## End(Not run)## Not run: library(ellmer) chat <- chat_openai(system_prompt = "Be concise.") answer <- cached_chat(chat, "What is the capital of France?") ## End(Not run)
Convenience wrapper that retrieves or generates a response and appends it to a shinychat UI.
cached_shinychat_respond(chat, user_input, chat_id = "chat")cached_shinychat_respond(chat, user_input, chat_id = "chat")
chat |
An ellmer Chat object. |
user_input |
Character. The user message (typically |
chat_id |
Character. The shinychat UI id. Default is |
Invisible character response.
Deletes all stored interactions.
chatcache_clear(confirm = TRUE)chatcache_clear(confirm = TRUE)
confirm |
Logical. If |
Invisible logical indicating whether the cache was cleared.
Looks up a previously stored response by cache key and increments the hit count.
chatcache_get(cache_key, con = NULL)chatcache_get(cache_key, con = NULL)
cache_key |
Character. The key returned by |
con |
Optional DBI connection. Uses the global connection if |
A list with elements response, metadata, and hit_count, or NULL
if the key is not found.
Creates or connects to the SQLite database used to store prompts and responses.
chatcache_init(cache_dir = NULL)chatcache_init(cache_dir = NULL)
cache_dir |
Character string. Directory in which to store the cache
database. Defaults to the user cache directory via |
An invisible DBI connection object.
## Not run: chatcache_init() chatcache_init(cache_dir = tempdir()) ## End(Not run)## Not run: chatcache_init() chatcache_init(cache_dir = tempdir()) ## End(Not run)
Returns a data frame of recent cached prompt-response pairs.
chatcache_list(limit = 100, con = NULL)chatcache_list(limit = 100, con = NULL)
limit |
Integer. Maximum number of rows to return. Default is 100. |
con |
Optional DBI connection. |
A data frame.
Inserts or replaces a prompt-response pair in the cache.
chatcache_set( cache_key, prompt, response, system_prompt = NULL, model = NULL, provider = NULL, metadata = NULL, con = NULL )chatcache_set( cache_key, prompt, response, system_prompt = NULL, model = NULL, provider = NULL, metadata = NULL, con = NULL )
cache_key |
Character. Cache key. |
prompt |
Character. Original user prompt. |
response |
Character. LLM response text. |
system_prompt |
Character or |
model |
Character or |
provider |
Character or |
metadata |
List or |
con |
Optional DBI connection. |
Invisible TRUE.
Creates a stable SHA-256 hash from the prompt and relevant context.
make_cache_key(prompt, system_prompt = NULL, model = NULL, history = NULL)make_cache_key(prompt, system_prompt = NULL, model = NULL, history = NULL)
prompt |
Character. The user prompt. |
system_prompt |
Character or |
model |
Character or |
history |
Character vector or |
A character string (SHA-256 hash).