-
Notifications
You must be signed in to change notification settings - Fork 37
/
README.Rmd
158 lines (115 loc) · 5.72 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{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
<!-- badges: start -->
[![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)
<!-- badges: end -->
<center>
<img width="300" height="400" src="man/figures/chatgpt_meme.jpeg">
<p>Meme by Programming Jokes I IT Humor & Memes</p>
</center>
## 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-3.5-turbo"`
* `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`