GenAI - R Package "GenAI" Overview

CRAN Version download count

💡 Update: version 0.2.0 💡
Utilizing R6 class, enhancing user friendiliness. Support one more generative AI - Moonshot AI. Moreover, you can now generate image using this package!


Basic Information

GenAI: Generative Artificial Intelligence

Utilizing 'Generative Artificial Intelligence' models like 'GPT-4' and 'Gemini Pro' as coding and writing assistants for 'R' users. Through these models, 'GenAI' offers a variety of functions, encompassing text generation, code optimization, natural language processing, chat, and image interpretation. The goal is to aid 'R' users in streamlining laborious coding and language processing tasks.

Depends: magrittr

Imports: base64enc, httr, jsonlite, tools, R6, listenv, magick, ggplotify

Author: Li Yuan

Maintainer: Li Yuan <>

BugReports: https://github.com/GitData-GA/GenAI/issues

License: CC BY 4.0

URL: https://genai.gd.edu.kg/

Needs Compilation: no

CRAN checks: GenAI results


Quickstart

  1. 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.

  2. Installation (2 options)

    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")

  3. Setup and get your first AI generated response using GenAI

    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()
    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()
    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()
  4. See the documentation for more functions.