chattr: An RStudio/Positron Interface for LLMs

Interact with Large Language Models in RStudio • chattr

1 Introduction to chattr

chattr is an R package that provides a seamless interface to Large Language Models (LLMs) directly within the RStudio and Positron IDEs. It allows you to interact with LLMs from your R scripts or through an interactive Shiny Gadget, making it a powerful tool for developers and data scientists.

2 Installation

First, you need to install the chattr package from GitHub.

Code
# Install the chattr package using pak
pak::pak("mlverse/chattr")

3 Getting Started

3.1 Load the Package

Once installed, load the chattr library into your R session.

Code
# Load the chattr library
library(chattr)

3.2 Set Up the Chat Provider

chattr works with various LLM providers. Here, we set up a chat provider using GitHub’s Copilot model through the ellmer package.

Code
# Set up the chat provider using GitHub Copilot (gpt-4o model)
# Note: GitHub Copilot does not require a separate OpenAI API key.
my_chat <- ellmer::chat_github(model = "gpt-4o")
chattr_use(my_chat)

4 Interactive Usage

4.1 Using the chattr App

chattr includes a Shiny-based gadget for an interactive chat experience.

Code
# Launch the interactive chattr app
# chattr_app()

4.2 Interacting Directly in Code

You can also send prompts to the LLM directly from your R code.

Code
# Send a prompt to the configured chat provider
chattr("what is your name?")

5 History Management

chattr keeps a record of your interactions, which you can manage with the following functions.

5.1 View History

Code
# Retrieve and print the chat history
chattr_history <- ch_history()
print(chattr_history)

5.2 Save History

You can save your chat history to a file for later use.

Code
# Save the current chat history to an RDS file
saveRDS(ch_history(), "chat_history.rds")

5.3 Clear History

To start a new session, you can clear the current chat history.

Code
# Clear the chat history by passing an empty list
print(ch_history(list()))

5.4 Reload History

You can reload a previously saved chat history.

Code
# Load a saved chat history from an RDS file
chattr_history <- ch_history(readRDS("chat_history.rds"))
print(chattr_history)

6 Conclusion

chattr is a versatile R package that brings the power of large language models directly into your development environment. Its ease of use, interactive features, and history management capabilities make it an essential tool for anyone looking to leverage LLMs in their R projects.

7 Reference