ellmer: A Flexible LLM Framework for R

1 Introduction to ellmer

ellmer is a powerful and flexible R package designed to simplify the use of large language models (LLMs). It offers support for a wide range of LLM providers and includes a rich set of features such as streaming outputs, tool/function calling, and structured data extraction. This makes it an excellent choice for integrating advanced AI capabilities into your R projects.

2 Installation

To get started, install the ellmer package from CRAN.

Code
# Install the ellmer package
install.packages("ellmer")

3 Getting Started

3.1 Load Necessary Packages

Load the ellmer and keyring packages into your R session. keyring is used for securely managing API keys.

Code
# Load the required libraries
library(ellmer)
library(keyring)

4 Using Google Gemini

This section demonstrates how to use ellmer with Google’s Gemini models.

4.1 Initialize the Chat Model

First, create an instance of the Gemini chat model, providing your API key and specifying the model you want to use.

Code
# Set up the Google Gemini chat model
chat_gemini_model <- chat_google_gemini(
  api_key = key_get("google_ai_api_key"),
  model = "gemini-1.5-flash"
)

chat_gemini_model

4.2 Generate Text

Once the model is initialized, you can start a chat and generate text.

Code
# Generate a response from the model
result <- chat_gemini_model$chat("Tell me three jokes about statisticians")
result

5 Interactive Modes

ellmer provides interactive modes for a more conversational experience.

5.1 Live Browser Mode

You can launch a web-based interface to chat with the model.

Code
# Open an interactive chat session in a web browser
live_browser(chat_gemini_model)

5.2 Console Mode

Alternatively, you can chat with the model directly in the R console.

Code
# Start an interactive chat session in the console
live_console(chat_gemini_model)

6 Advanced Usage

6.1 Using a System Prompt

You can provide a system prompt to guide the model’s behavior and tone.

Code
# Define a system prompt
system_prompt <- "You are an IT expert"
system_prompt
Code
# Initialize the model with the system prompt
chat_gemini_model_expert <- chat_google_gemini(
  system_prompt = system_prompt,
  api_key = key_get("google_ai_api_key"),
  model = "gemini-2.5-flash"
)

chat_gemini_model_expert

6.2 Vision Capabilities

ellmer also supports multi-modal models that can analyze images.

First, upload the image file to the Google API.

Code
# Upload an image file
file <- google_upload(
  path = "coffee.jpeg",
  api_key = key_get("google_ai_api_key")
)

Then, you can ask the model to describe or analyze the image.

Code
# Ask the model to summarize the content of the image
chat_gemini_model$chat(file, "Give me a three-paragraph summary of this")

7 Advanced Features

ellmer also supports more advanced features like structured output and tool calling, which allow for more complex and powerful applications.

8 Conclusion

ellmer is a comprehensive and user-friendly R package for working with large language models. Its wide range of features, support for multiple providers, and ease of use make it an invaluable tool for R users who want to incorporate the power of LLMs into their data analysis and applications.

9 Reference