blob: c6a3a69c79eb5ecc22996f370cdf85312b0211cd [file] [log] [blame]
ben-aaron1883818e7c2022-09-08 17:49:01 +02001#' 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-aaron18842a91d22022-09-08 20:38:22 +02005#' @param path The file path to the API key
ben-aaron1883818e7c2022-09-08 17:49:01 +02006#' @return A confirmation message
ben-aaron18842a91d22022-09-08 20:38:22 +02007#' @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-aaron1883818e7c2022-09-08 17:49:01 +02008#' @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-aaron18842a91d22022-09-08 20:38:22 +020015gpt3.authenticate = function(path){
16 apikey_ = readLines(path)
17 api_key <<- apikey_
ben-aaron1883818e7c2022-09-08 17:49:01 +020018 print(paste0("Will use --> ", api_key, " for authentication."))
19}
20
21gpt3.endsession = function(){
22 api_key = "---"
23 print('-- session ended: you need to re-authenticate again next time.')
24}
25
26check_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}