ben-aaron188 | 3818e7c | 2022-09-08 17:49:01 +0200 | [diff] [blame] | 1 | #' Make a test request to the GPT-3 API |
| 2 | #' |
| 3 | #' @description |
| 4 | #' `gpt3.test_request()` sends a basic [completion request](https://beta.openai.com/docs/api-reference/completions) to the Open AI GPT-3 API. |
| 5 | #' @param verbose (boolean) if TRUE prints the actual prompt and GPT-3 completion of the test request (default: FALSE). |
| 6 | #' @return A message of success or failure of the connection. |
| 7 | #' @examples |
| 8 | #' gpt3.test_request() |
| 9 | #' @export |
| 10 | gpt3.make_request = function(prompt_ |
| 11 | , model_ = 'text-davinci-002' |
| 12 | , output_type_ = 'string_only' |
| 13 | , suffix_ = NULL |
| 14 | , max_tokens_ = 256 |
| 15 | , temperature_ = 0.9 |
| 16 | , top_p_ = 1 |
| 17 | , n_ = 1 |
| 18 | , stream_ = F |
| 19 | , logprobs_ = NULL |
| 20 | , echo_ = F |
| 21 | , stop_ = NULL |
| 22 | , presence_penalty_ = 0 |
| 23 | , frequency_penalty_ = 0 |
| 24 | , best_of_ = 1 |
| 25 | , logit_bias_ = NULL |
| 26 | ) |
| 27 | { |
| 28 | |
| 29 | parameter_list = list(prompt = prompt_ |
| 30 | , model = model_ |
| 31 | , suffix = suffix_ |
| 32 | , max_tokens = max_tokens_ |
| 33 | , temperature = temperature_ |
| 34 | , top_p = top_p_ |
| 35 | , n = n_ |
| 36 | , stream = stream_ |
| 37 | , logprobs = logprobs_ |
| 38 | , echo = echo_ |
| 39 | , stop = stop_ |
| 40 | , presence_penalty = presence_penalty_ |
| 41 | , frequency_penalty = frequency_penalty_ |
| 42 | , best_of = best_of_ |
| 43 | , logit_bias = logit_bias_ |
| 44 | ) |
| 45 | |
| 46 | request_base = httr::POST(url = url.completions |
| 47 | , body = parameter_list |
| 48 | , httr::add_headers(Authorization = paste("Bearer", api_key)) |
| 49 | , encode = "json") |
| 50 | |
| 51 | |
| 52 | if(output_type_ == 'string_only'){ |
| 53 | output = httr::content(request_base)$choices[[1]]$text |
| 54 | } else { |
| 55 | output = httr::content(request_base) |
| 56 | } |
| 57 | |
| 58 | return(output) |
| 59 | |
| 60 | } |