Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jcrodriguez1989/chatgpt

Interface to ChatGPT from R
https://github.com/jcrodriguez1989/chatgpt

assistant chatgpt gpt-3 gpt-4 hacktoberfest llm nlp openai r rstats rstats-package rstatses rstudio rstudio-addin

Last synced: 1 day ago
JSON representation

Interface to ChatGPT from R

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
library("chatgpt")
Sys.setenv("OPENAI_VERBOSE" = FALSE)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```

# ChatGPT coding assistant for RStudio

[![CRAN status](https://www.r-pkg.org/badges/version/chatgpt)](https://CRAN.R-project.org/package=chatgpt)
[![R-CMD-check](https://github.com/jcrodriguez1989/chatgpt/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/jcrodriguez1989/chatgpt/actions/workflows/R-CMD-check.yaml)
[![CRAN downloads](https://cranlogs.r-pkg.org/badges/chatgpt)](https://cran.rstudio.com/web/packages/chatgpt/index.html)


Meme by Programming Jokes I IT Humor & Memes

## Installation

Install the current released version of `{chatgpt}` from
[CRAN](https://cran.r-project.org/package=chatgpt):

```{r eval = FALSE}
install.packages("chatgpt")
```

Or install the development version from [GitHub](https://github.com/jcrodriguez1989/chatgpt) with:

```{r eval = FALSE}
# install.packages("remotes")
remotes::install_github("jcrodriguez1989/chatgpt")
```

## Requirements

You need to setup your ChatGPT API key in R.

First you will need to obtain your ChatGPT API key. You can create an API key by accessing [OpenAI API page](https://beta.openai.com/account/api-keys) -don't miss their article about [Best Practices for API Key Safety
](https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety)-.

Then you have to assign your API key for usage in R, this can be done just for the actual session, by doing:

``` r
Sys.setenv(OPENAI_API_KEY = "XX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
```

Or you can do it persistent (session-wide), by assigning it in your `.Renviron` file. For it, execute `usethis::edit_r_environ()`, and in that file write a line at the end your API key as

``` r
OPENAI_API_KEY=XX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
```

## Features

The {chatgpt} R package provides a set of features to assist in R coding.
Current existing addins:

```{r, echo=FALSE, results='asis'}
addins_info <- readLines("inst/rstudio/addins.dcf")
addins_info <- addins_info[grepl("^(Name: |Description: )", addins_info)]
addins_info <- gsub("^(Name: |Description: )", "", addins_info)
indexes <- seq_len(length(addins_info))
indexes <- indexes[indexes %% 2 == 1]
cat(paste(
sapply(indexes, function(i) paste0(" - **", addins_info[[i]], ":** ", addins_info[[i + 1]])),
collapse = "\n"
))
```

**Note:** When no code is selected, it will use the whole file's code.

## Common Errors

#### You exceeded your current quota, please check your plan and billing details

```r
Error in gpt_get_completions:
list(message = "You exceeded your current quota, please check your plan and billing details.", type = "insufficient_quota", param = NULL, code = NULL)
```

To fix this, you need to **provide a billing method** in OpenAI. More information can be found in this [article](https://help.openai.com/en/articles/6891831-error-code-429-you-exceeded-your-current-quota-please-check-your-plan-and-billing-details).

## Code Examples

```{r, echo=FALSE, results='asis'}
pkg_name <- "chatgpt"
exported_functions <- setdiff(sort(getNamespaceExports(pkg_name)), "reset_chat_session")
examples_usage <- sapply(exported_functions, function(exported_function) {
function_example <- example(exported_function, pkg_name, character.only = TRUE, give.lines = TRUE)
function_example <- function_example[grepl("^##D ", function_example)]
function_example <- gsub("^##D ", "", function_example)
example_result <- capture.output(eval(parse(text = function_example)))
example_result <- gsub("`|'", "", paste0(example_result, collapse = "\n"))
paste0(
paste0("#### `", exported_function, "`\n\n``` r\n"),
paste0("> ", function_example, "\n"),
example_result,
"\n```",
collapse = "\n"
)
})
cat(paste0("\n", examples_usage, collapse = "\n\n"))
```

## Additional Parameters

### Disable Console Messages

If you want {chatgpt} not to show messages in console, please set the environment variable `OPENAI_VERBOSE=FALSE`.

### Addin Changes in Place

If you want {chatgpt} addins to take place in the editor -i.e., replace the selected code with the result of the addin execution- then you sould set the environment variable `OPENAI_ADDIN_REPLACE=TRUE`.

### Change the language of ChatGPT responses

To change the language that ChatGPT responds in, the `OPENAI_RETURN_LANGUAGE` environment variable must be changed.
E.g.,

```{r}
Sys.setenv("OPENAI_RETURN_LANGUAGE" = "Español")
cat(chatgpt::explain_code("for (i in 1:10) {\n print(i ** 2)\n}"))
```

### Use ChatGPT behind a proxy

In order to run ChatGPT queries behind a proxy, set the `OPENAI_PROXY` environment variable with a valid `IP:PORT` proxy.
E.g., `Sys.setenv("OPENAI_PROXY" = "81.94.255.13:8080")`.

### Switch OPENAI's API URL

To replace the default OPENAI's API URL (`"https://api.openai.com/v1"`), you can set the `OPENAI_API_URL` environment variable with the URL you need to use.
E.g., `Sys.setenv("OPENAI_API_URL" = "https://api.chatanywhere.com.cn")`.

### ChatGPT Model Tweaks

ChatGPT model parameters can be tweaked by using environment variables.

The following environment variables can be set to tweak the behavior, as documented in https://beta.openai.com/docs/api-reference/completions/create .

* `OPENAI_MODEL`; defaults to `"gpt-4o-mini"`
* `OPENAI_MAX_TOKENS`; defaults to `256`
* `OPENAI_TEMPERATURE`; defaults to `1`
* `OPENAI_TOP_P`; defaults to `1`
* `OPENAI_FREQUENCY_PENALTY`; defaults to `0`
* `OPENAI_PRESENCE_PENALTY`; defaults to `0`
* `OPENAI_LOGPROBS`; defaults to `FALSE`