Last modified: NA
Compiled: Mon Aug 21 22:37:22 2023

1 Load packages

library(chatAI4R)

2 Set the API key according to each Web API.

For example, to obtain an OpenAI API key, please register as a member on the OpenAI website (https://platform.openai.com/account/api-keys) and obtain your API key.

#Set your key for the OpenAI API
Sys.setenv(OPENAI_API_KEY = "Your API key")

#Set your key for the DreamStudio API
Sys.setenv(DreamStudio_API_KEY = "Your API key")

3 Simple usage

3.1 One-Shot Chatting

All runs using the chat4R function are One-Shot Chatting. Conversation history is not carried over to the next conversation.

#Set your API key
Sys.setenv(OPENAI_API_KEY = "Your API key")

#This function use the Web API; "https://api.openai.com/v1/chat/completions"
chat4R("What is the capital of France?")

#This function use the Web API; "https://api.openai.com/v1/completions"
completions4R("Hello")

3.2 Few-Shots/Chain-Shots Chatting

Executions using the conversation4R function will keep a history of conversations. The number of previous messages to keep in memory defaults to 2.

#First shot
conversation4R("Hello")

#Second shot
conversation4R("Hello")

4 Text to Image Generation (Legacy)

4.1 DALL·E 2 (Legacy)

Create an image generation prompt and generate an image using the DALL·E 2 model.

# Create a prompt
pr <- createImagePrompt(content = "A white cat. Brown cat. Fluffy. Round eyes. Round. Short hands. Sea. Photo. Facing forward.")

# Generate an image using DALL·E 2
img <- generateImage4R(pr)

# Display
Display(img)
Image examples generated by DALL·E 2 model

Figure 1: Image examples generated by DALL·E 2 model

4.2 Stable Diffusion (Legacy)

Create an image generation prompt and generate an image using the Stable Diffusion model.

# Create a prompt
pr <- createImagePrompt(content = "A white cat. Brown cat. Fluffy. Round eyes. Round. Short hands. Sea. Photo. Facing forward.")

# Generate an image using Stable Diffusion
img <- txt2img_StableDiffusion4R(pr)

# Display
Display(img)
Image examples generated by Stable Diffusion model

Figure 2: Image examples generated by Stable Diffusion model

Session information

## R version 4.2.3 (2023-03-15)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur ... 10.16
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] BiocStyle_2.24.0
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.11         rstudioapi_0.15.0   magrittr_2.0.3     
##  [4] knitr_1.43          BiocGenerics_0.42.0 EBImage_4.38.0     
##  [7] lattice_0.21-8      R6_2.5.1            jpeg_0.1-10        
## [10] rlang_1.1.1         fastmap_1.1.1       highr_0.10         
## [13] tools_4.2.3         grid_4.2.3          xfun_0.39          
## [16] png_0.1-8           cli_3.6.1           jquerylib_0.1.4    
## [19] htmltools_0.5.5     yaml_2.3.7          abind_1.4-5        
## [22] digest_0.6.33       bookdown_0.34       BiocManager_1.30.21
## [25] htmlwidgets_1.6.2   bitops_1.0-7        fftwtools_0.9-11   
## [28] sass_0.4.6          RCurl_1.98-1.12     cachem_1.0.8       
## [31] evaluate_0.21       rmarkdown_2.23      tiff_0.1-11        
## [34] compiler_4.2.3      bslib_0.5.0         magick_2.7.4       
## [37] locfit_1.5-9.8      jsonlite_1.8.7