Last update: 2024/02/15
In the fast-evolving landscape of artificial intelligence (AI), a groundbreaking tool has emerged, poised to redefine the way R users engage with their coding and language processing tasks. Introducing GenAI — the ultimate R package that seamlessly integrates cutting-edge Generative AI models like ‘GPT-4’ and ‘Gemini Pro’. But GenAI is not just another package; it’s a gateway to a new realm of possibilities.
CRAN: https://cran.r-project.org/package=GenAI
Official website: https://genai.gd.edu.kg/r/
With the recent update to version 0.2.0, GenAI has undergone a transformation. Leveraging the power of R6 class, it now offers enhanced user-friendliness, making complex tasks feel like a breeze. But that’s not all — GenAI now proudly supports yet another generative AI model — Moonshot AI. Plus, brace yourselves for a game-changer: image generation is now within your reach through this very package!
Some images generated through this package with OpenAI’s DALL-E 2 model:
A Closer Look at GenAI
GenAI isn’t just a tool; it’s a paradigm shift. At its core, it harnesses the prowess of ‘Generative Artificial Intelligence’ models to assist R users in a myriad of tasks. From text generation to code optimization, from natural language processing to chat functionalities, and even image interpretation, GenAI is a one-stop solution for all your AI needs within the R environment.
Key Details
Getting Started with GenAI
Prerequisites
Prior to utilizing the GenAI package, several prerequisites must be met.
Ensure that you possess an eligible device equipped with R.
Access to the internet is essential to generate text or engage in chat through GenAI.
Obtain an API key from the selected Generative AI service provider. GenAI currently supports Generative AI models from Google, Moonshot AI, and OpenAI.
Installation
You have two options for installing GenAI:
Install the package from The Comprehensive R Archive Network (CRAN).
install.packages("GenAI")
Install the package from GenAI official website.
remotes::install_url("https://genai.gd.edu.kg/release/R/GenAI_latest.tar.gz",
dependencies = TRUE,
method = "libcurl")
Your First AI Interaction
Now that you have GenAI installed, let’s dive into the magic of AI-generated responses. Depending on your chosen AI model, try the QuickStart examples below to explore:
QuickStart for Google Models (Open in Colab)
# Import the library
library("GenAI")
all.models = available.models() %>% print()
# Please change YOUR_GOOGLE_API to your own API key of Google Generative AI
Sys.setenv(GOOGLE_API = "YOUR_GOOGLE_API")
# Create a Google Generative AI object
google = genai.google(api = Sys.getenv("GOOGLE_API"),
model = all.models$google$model[1],
version = all.models$google$version[1],
proxy = FALSE)
# Text Generation
google %>%
txt("Please write a story about Mars in 50 words.") %>%
cat()
# Chat Generation
google %>%
chat("Please write a story about Mars in 50 words.") %>%
cat()
google %>%
chat("Please write a story about Jupiter in 50 words.") %>%
cat()
google %>%
chat("Please write a story about Earth in 50 words.") %>%
cat()
# Print Chat History
google %>%
chat.history.print()
QuickStart for OpenAI Models (Open in Colab)
# Import the library
library("GenAI")
all.models = available.models() %>% print()
# Please change YOUR_OPENAI_API to your own API key of OpenAI
Sys.setenv(OPENAI_API = "YOUR_OPENAI_API")
# (Optional) Please change YOUR_OPENAI_ORG to your own organization ID for OpenAI
Sys.setenv(OPENAI_ORG = "YOUR_OPENAI_ORG")
# Create an OpenAI object
openai = genai.openai(api = Sys.getenv("OPENAI_API"),
model = all.models$openai$model[1],
version = all.models$openai$version[1],
proxy = FALSE,
organization.id = Sys.getenv("OPENAI_ORG"))
# Text Generation
openai %>%
txt("Please write a story about Mars in 50 words.") %>%
cat()
# Chat Generation
openai %>%
chat("Please write a story about Mars in 50 words.") %>%
cat()
openai %>%
chat("Please write a story about Jupiter in 50 words.") %>%
cat()
openai %>%
chat("Please write a story about Earth in 50 words.") %>%
cat()
# Print Chat History
openai %>%
chat.history.print()
QuickStart for Moonshot AI models (Open in Colab)
# Import the library
library("GenAI")
all.models = available.models() %>% print()
# Please change YOUR_MOONSHOT_API to your own API key of Moonshot AI
Sys.setenv(MOONSHOT_API = "YOUR_MOONSHOT_API")
# Create a Moonshot AI object
moonshot = genai.moonshot(api = Sys.getenv("MOONSHOT_API"),
model = all.models$moonshot$model[1],
version = all.models$moonshot$version[1],
proxy = FALSE)
# Text Generation
moonshot %>%
txt("Please write a story about Mars in 50 words.") %>%
cat()
# Chat Generation
moonshot %>%
chat("Please write a story about Mars in 50 words.") %>%
cat()
moonshot %>%
chat("Please write a story about Jupiter in 50 words.") %>%
cat()
moonshot %>%
chat("Please write a story about Earth in 50 words.") %>%
cat()
# Print Chat History
moonshot %>%
chat.history.print()
Unlock the Full Potential
This is just the tip of the iceberg! Dive into the GenAI documentation to unleash a plethora of functions waiting to be explored.
GenAI R package documentation: https://genai.gd.edu.kg/r/documentation/
Setup
The setup process involves accessing supported Generative AI models and initializing objects for Google AI, Moonshot AI, and OpenAI. Here are the steps:
available.models
function to explore the range of supported models. Learn more.
genai.google
function. Learn more.
genai.moonshot
function. Learn more.
genai.openai
function. Learn more.
Text Generation
GenAI offers various methods for text generation, allowing you to generate text with different inputs:
txt
function to generate text based on textual input. Learn more.
txt.image
function. Learn more.
Chat Generation
Enhance communication experiences with GenAI’s chat generation functionalities:
chat
function. Learn more.
chat.edit
function. Learn more.
chat.history.convert
, chat.history.export
, chat.history.import
, chat.history.print
, chat.history.reset
, and chat.history.save
. Learn more.
Image Generation
Generate visually appealing content using text inputs with GenAI’s image generation functionality:
img
function to generate images based on textual input. Learn more.
GenAI isn’t just a tool; it’s a catalyst for innovation. Embrace the future of R programming with GenAI today!