ben-aaron188 | 3818e7c | 2022-09-08 17:49:01 +0200 | [diff] [blame] | 1 | #' Set up the authentication with your API key |
| 2 | #' |
| 3 | #' @description |
ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 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()` looks for your API key in a file that you provide the path to and ensures 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 | 85c32a0 | 2022-09-10 20:30:30 +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: |
ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 10 | #' gpt3_authenticate(path = './YOURPATH/access_key.txt') |
ben-aaron188 | 3818e7c | 2022-09-08 17:49:01 +0200 | [diff] [blame] | 11 | # ' |
| 12 | #' # After you are finished: |
ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 13 | #' gpt3_endsession() |
ben-aaron188 | 3818e7c | 2022-09-08 17:49:01 +0200 | [diff] [blame] | 14 | #' @export |
ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 15 | gpt3_authenticate = function(path){ |
ben-aaron188 | 42a91d2 | 2022-09-08 20:38:22 +0200 | [diff] [blame] | 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 | |
ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 21 | gpt3_endsession = function(){ |
ben-aaron188 | 3818e7c | 2022-09-08 17:49:01 +0200 | [diff] [blame] | 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){ |
ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 29 | warning("Use gpt3_authenticate() to set your API key") |
ben-aaron188 | 3818e7c | 2022-09-08 17:49:01 +0200 | [diff] [blame] | 30 | } else if(nchar(api_key) < 10){ |
| 31 | |
ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 32 | warning("Use gpt3_authenticate() to set your API key") |
ben-aaron188 | 3818e7c | 2022-09-08 17:49:01 +0200 | [diff] [blame] | 33 | |
| 34 | } |
| 35 | } |