Large Language Models (LLMs) in R with tidychatmodels

[This article was first published on business-science.io, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Hey guys, welcome back to my R-tips newsletter. Lately it’s felt like Python, which has dozens of tools for Large Language Models (LLMs), is moving faster into LLMs than the R community. Thankfully, I stumbled upon a new R package that integrates Large Language Models (LLMs) from OpenAI, Mistral AI, and more all inside of R. It’s called tidychatmodels, and I’m sharing how to get started in under 5 minutes. Let’s go!

Table of Contents

Here’s what you’re learning today:

  • What is tidychatmodels? You’ll discover what tidychatmodels is and how it brings LLMs to RStudio
  • Benefits of using Tidy Chat Models How tidychatmodels uses the familiar tidyverse-style syntax for working with LLMs
  • How to use LLMs inside of R I have prepared a full tutorial for making an R Code Chatbot.
  • Make a quick Shiny App with tidychatmodels I’ll explain how to make your first shiny app to upload and explore a dataset stored as a CSV file.

Tidychatmodels

Get the Code (In the R-Tip 081 Folder)


SPECIAL ANNOUNCEMENT: ChatGPT for Data Scientists Workshop on July 17th

Inside the workshop I’ll share how I built a Machine Learning Powered Production Shiny App with ChatGPT (extends this data analysis to an insane production app):

ChatGPT for Data Scientists

What: ChatGPT for Data Scientists

When: Wednesday July 17th, 2pm EST

How It Will Help You: Whether you are new to data science or are an expert, ChatGPT is changing the game. There’s a ton of hype. But how can ChatGPT actually help you become a better data scientist and help you stand out in your career? I’ll show you inside my free chatgpt for data scientists workshop.

Price: Does Free sound good?

How To Join: 👉 Register Here


R-Tips Weekly

This article is part of R-Tips Weekly, a weekly video tutorial that shows you step-by-step how to do common R coding tasks. Pretty cool, right?

Here are the links to get set up. 👇

This Tutorial is Available in Video (6-minutes)

I have a 6-minute video that walks you through setting up tidychatmodels in R and running your first LLM. 👇

What is tidychatmodels?

tidychatmodels is a tidyverse-style interface to LLMs (Large Language Models) developed by Albert Rapp. According to it’s documentation:

[tidychatmodels] provides a simple interface to chat with your favorite AI chatbot from R. It is inspired by the modular nature of {tidymodels} where you can easily swap out any ML model for another one but keep the other parts of the workflow the same.

Supported LLM Model Vendors

Currently model vendors are supported:

Benefits of using Tidychatmodels

One of the main benefits is the unified interface. This makes it easy to swap in and out different LLMs by simply changing the API key and LLM provider.

Swap out LLMs

It’s literally that easy to swap out an LLM provider for another LLM model.

Tutorial: How to use LLMs inside of R with tidychatmodels

It takes about 1 minute to get tidychatmodels set up so you can start using LLMs inside of R. All the tutorial code shown is available in the R-Tips Newsletter folder for R-Tip 081.

Get the Code

Step 1 – Set Up Tidy Chat Models:

The first step is to set up tidychatmodels. You’ll need to install the software and set up your LLM provider’s API key. Follow these 4 steps:

  1. Install: tidychatmodels is not on CRAN as of this article. But you can install from GitHub.
  2. Load: Load tidyverse and tidychatmodels
  3. API Key: Set up your OpenAI API Key in your R environment.
  4. Get Your API Key: Use Sys.getenv().

Chattr Setup

Step 2 – Create a Chat Model

Once you have Tidy Chat Models set up, you’re ready to make your first chat model. Let’s create a chat model that is specifically designed to write R code. This is how:

  • Use OpenAI model vendor
  • Select “gpt-4o” for the LLM specification
  • Add parameters like temperature and max tokens
  • Set the role for the chatbot via prompt engineering

Chat Model

Step 3 – Run the LLM

The next step is to get a user request, and to process the request using the OpenAI gpt-4o model.

  • The user request: Our user is requesting to create a shiny app that uploads a dataset provided as a CSV and makes a visualization with the interactive plotly library.
  • The LLM is then run using the perform_chat() function

Run the LLM

Step 4 – Extract the results

The last step is to extract the results.

  • To extract the results, use extract_chat(silent = TRUE), which returns a data frame (tibble)
  • To print the code in human readable text, use pluck() and cat().
  • This returns the Shiny App code from our AI R Coder Copilot. (We’ll see if it runs next)

Extract the Results

Does the Shiny App work from our R Coder AI Copilot

Shiny App

The shiny app works! I uploaded the CSV file stored in the 081 Folder, and I can quickly view a scatter plot of numeric columns.

Reminder: The code is available free inside R-tips

All of the code you saw today is available in R-Tips Newsletter folder for R-Tip 081

Get The Code

Conclusions:

This is exciting! tidychatmodels is making it easier and more productive for me to use LLM’s in my R workflow. I look forward to seeing how tidychatmodels progresses as LLM’s become a bigger part of my data science process.

There you have it. How to get LLMs in R. But, the next problem is that you’ll need to solve business problems with data science and R.

If you would like to grow your Business Data Science skills, then please read on…

Need to advance your business data science skills?

I’ve helped 6,107+ students learn data science for business from an elite business consultant’s perspective.

I’ve worked with Fortune 500 companies like S&P Global, Apple, MRM McCann, and more.

And I built a training program that gets my students life-changing data science careers (don’t believe me? see my testimonials here):

6-Figure Data Science Job at CVS Health ($125K)
Senior VP Of Analytics At JP Morgan ($200K)
50%+ Raises & Promotions ($150K)
Lead Data Scientist at Northwestern Mutual ($175K)
2X-ed Salary (From $60K to $120K)
2 Competing ML Job Offers ($150K)
Promotion to Lead Data Scientist ($175K)
Data Scientist Job at Verizon ($125K+)
Data Scientist Job at CitiBank ($100K + Bonus)

Whenever you are ready, here’s the system they are taking:

Here’s the system that has gotten aspiring data scientists, career transitioners, and life long learners data science jobs and promotions…

What They're Doing - 5 Course R-Track

Join My 5-Course R-Track Program Now!
(And Become The Data Scientist You Were Meant To Be…)

P.S. – Samantha landed her NEW Data Science R Developer job at CVS Health (Fortune 500). This could be you.

Success Samantha Got The Job

To leave a comment for the author, please follow the link and comment on their blog: business-science.io.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)