ben-aaron188 | 3818e7c | 2022-09-08 17:49:01 +0200 | [diff] [blame] | 1 | #' Set up the authentication with your API key |
| 2 | #' |
| 3 | #' @description |
| 4 | #' Access to GPT-3's functions requires an API key that you obtain from [https://openai.com/api/](https://openai.com/api/). `gpt3.authenticate()` accepts your API key and ensures that you can connect to the models. `gpt3.endsession()` overwrites your API key _for this session_ (it is recommended that you run this when you are done). `check_apikey_form()` is a simple check if any information has been provided at all. |
ben-aaron188 | 42a91d2 | 2022-09-08 20:38:22 +0200 | [diff] [blame] | 5 | #' @param path The file path to the API key |
ben-aaron188 | 3818e7c | 2022-09-08 17:49:01 +0200 | [diff] [blame] | 6 | #' @return A confirmation message |
ben-aaron188 | 42a91d2 | 2022-09-08 20:38:22 +0200 | [diff] [blame] | 7 | #' @details The easiest way to store you API key is in a `.txt` file with _only_ the API key in it (without quotation marks or other common string indicators). `gpt3.authenticate()` reads the single file you point it to and retrieves the content as authentication key for all requests. |
ben-aaron188 | 3818e7c | 2022-09-08 17:49:01 +0200 | [diff] [blame] | 8 | #' @examples |
| 9 | #' # Starting a session: |
| 10 | #' gpt3.authenticate(apikey = 'REPLACE_THIS_WITH_YOUR_KEY') |
| 11 | # ' |
| 12 | #' # After you are finished: |
| 13 | #' gpt3.endsession() |
| 14 | #' @export |
ben-aaron188 | 42a91d2 | 2022-09-08 20:38:22 +0200 | [diff] [blame] | 15 | gpt3.authenticate = function(path){ |
| 16 | apikey_ = readLines(path) |
| 17 | api_key <<- apikey_ |
ben-aaron188 | 3818e7c | 2022-09-08 17:49:01 +0200 | [diff] [blame] | 18 | print(paste0("Will use --> ", api_key, " for authentication.")) |
| 19 | } |
| 20 | |
| 21 | gpt3.endsession = function(){ |
| 22 | api_key = "---" |
| 23 | print('-- session ended: you need to re-authenticate again next time.') |
| 24 | } |
| 25 | |
| 26 | check_apikey_form = function(){ |
| 27 | |
| 28 | if(exists(x = 'api_key') == F){ |
| 29 | warning("Use gpt3.authenticate() to set your API key") |
| 30 | } else if(nchar(api_key) < 10){ |
| 31 | |
| 32 | warning("Use gpt3.authenticate() to set your API key") |
| 33 | |
| 34 | } |
| 35 | } |