blob: d0fbb805a5172861556259b38491419169c9d875 [file] [log] [blame]
ben-aaron1883818e7c2022-09-08 17:49:01 +02001#' Set up the authentication with your API key
2#'
3#' @description
ben-aaron18885c32a02022-09-10 20:30:30 +02004#' 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-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-aaron18885c32a02022-09-10 20:30:30 +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:
ben-aaron18885c32a02022-09-10 20:30:30 +020010#' gpt3_authenticate(path = './YOURPATH/access_key.txt')
ben-aaron1883818e7c2022-09-08 17:49:01 +020011# '
12#' # After you are finished:
ben-aaron18885c32a02022-09-10 20:30:30 +020013#' gpt3_endsession()
ben-aaron1883818e7c2022-09-08 17:49:01 +020014#' @export
ben-aaron18885c32a02022-09-10 20:30:30 +020015gpt3_authenticate = function(path){
ben-aaron18842a91d22022-09-08 20:38:22 +020016 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
ben-aaron18885c32a02022-09-10 20:30:30 +020021gpt3_endsession = function(){
ben-aaron1883818e7c2022-09-08 17:49:01 +020022 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){
ben-aaron18885c32a02022-09-10 20:30:30 +020029 warning("Use gpt3_authenticate() to set your API key")
ben-aaron1883818e7c2022-09-08 17:49:01 +020030 } else if(nchar(api_key) < 10){
31
ben-aaron18885c32a02022-09-10 20:30:30 +020032 warning("Use gpt3_authenticate() to set your API key")
ben-aaron1883818e7c2022-09-08 17:49:01 +020033
34 }
35}