GPT, Gemini, etc., all in one R package!

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.

genai_logo

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:

sample_img1 sample_img2 sample_img3


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.

  1. Ensure that you possess an eligible device equipped with R.

  2. Access to the internet is essential to generate text or engage in chat through GenAI.

  3. 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:

  1. Install the package from The Comprehensive R Archive Network (CRAN).

    install.packages("GenAI")

  2. 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:

  • Get Supported Generative AI Models: Use the available.models function to explore the range of supported models. Learn more.
  • Google Generative AI Object Creation: Create Google Generative AI objects using the genai.google function. Learn more.
  • Moonshot AI Object Creation: Initiate Moonshot AI objects with the genai.moonshot function. Learn more.
  • OpenAI Object Creation: Dive into OpenAI’s capabilities by creating OpenAI objects using the genai.openai function. Learn more.

Text Generation

GenAI offers various methods for text generation, allowing you to generate text with different inputs:

  • Text Generation with Text as Input: Utilize the txt function to generate text based on textual input. Learn more.
  • Text Generation with Text and Image as Input: Explore advanced text generation by incorporating both text and image inputs with the txt.image function. Learn more.

Chat Generation

Enhance communication experiences with GenAI’s chat generation functionalities:

  • Chat Generation with Text as Input: Generate chat responses based on textual input using the chat function. Learn more.
  • Chat Edit with New Text as Input: Edit chat responses with new text inputs using the chat.edit function. Learn more.
  • Chat History Operations: Manage chat history with functions like 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:

  • Image Generation with Text as Input: Utilize the 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!