---
title: "Usage of functions in the chatAI4R package"
author: 
- name: Satoshi Kume
  email: satoshi.kume.1984@gmail.com
date: "`r Sys.Date()`"
graphics: no
package: chatAI4R
output:
    BiocStyle::html_document:
        toc_float: true
vignette: >
    %\VignetteIndexEntry{Usage of functions in the chatAI4R package}
    %\VignetteEncoding{UTF-8}
    %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

```{r style, echo = FALSE, results = 'asis', message=FALSE}
BiocStyle::markdown()
```

**Last modified:** `r file.info("HowToUse_BiocStyle.Rmd")$mtime`<br />
**Compiled**: `r date()`

# Load packages

```{r echo=TRUE, eval=FALSE}

library(chatAI4R)

```

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

```{r echo=TRUE, eval=FALSE}
#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")
```

# Simple usage

## One-Shot Chatting

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

```{r echo=TRUE, eval=FALSE}
#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")
```

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

```{r echo=TRUE, eval=FALSE}
#First shot
conversation4R("Hello")

#Second shot
conversation4R("Hello")
```

# Text to Image Generation (Legacy)

## DALL·E 2 (Legacy)

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

```{r echo=TRUE, eval=FALSE}
# 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)
```

```{r Fig001, fig.cap = "Image examples generated by DALL·E 2 model", echo = FALSE}
options(EBImage.display = "raster")
img <- system.file("images", "Fig01.png", package="chatAI4R")
EBImage::display(EBImage::readImage(files = img))
```

## Stable Diffusion (Legacy)

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

```{r echo=TRUE, eval=FALSE}
# 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)
```

```{r Fig002, fig.cap = "Image examples generated by Stable Diffusion model", echo = FALSE}
options(EBImage.display = "raster")
img <- system.file("images", "Fig02.png", package="chatAI4R")
EBImage::display(EBImage::readImage(files = img))
```

# Session information {.unnumbered}
```{r sessionInfo, echo=FALSE}
sessionInfo()
```
