ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 1 | % Generated by roxygen2: do not edit by hand |
ben-aaron188 | 718e3a6 | 2022-10-24 14:28:51 +0200 | [diff] [blame^] | 2 | % Please edit documentation in R/gpt3_single_completion.R |
| 3 | \name{gpt3_single_completion} |
| 4 | \alias{gpt3_single_completion} |
ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 5 | \title{Makes a single completion request to the GPT-3 API} |
| 6 | \usage{ |
ben-aaron188 | 718e3a6 | 2022-10-24 14:28:51 +0200 | [diff] [blame^] | 7 | gpt3_single_completion( |
ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 8 | prompt_input, |
| 9 | model = "text-davinci-002", |
| 10 | output_type = "complete", |
| 11 | suffix = NULL, |
| 12 | max_tokens = 100, |
| 13 | temperature = 0.9, |
| 14 | top_p = 1, |
| 15 | n = 1, |
| 16 | logprobs = NULL, |
| 17 | stop = NULL, |
| 18 | presence_penalty = 0, |
| 19 | frequency_penalty = 0, |
| 20 | best_of = 1 |
| 21 | ) |
| 22 | } |
| 23 | \arguments{ |
| 24 | \item{prompt_input}{character that contains the prompt to the GPT-3 request} |
| 25 | |
| 26 | \item{model}{a character vector that indicates the \href{https://beta.openai.com/docs/models/gpt-3}{model} to use; one of "text-davinci-002" (default), "text-curie-001", "text-babbage-001" or "text-ada-001"} |
| 27 | |
| 28 | \item{output_type}{character determining the output provided: "complete" (default), "text" or "meta"} |
| 29 | |
| 30 | \item{suffix}{character (default: NULL) (from the official API documentation: \emph{The suffix that comes after a completion of inserted text})} |
| 31 | |
| 32 | \item{max_tokens}{numeric (default: 100) indicating the maximum number of tokens that the completion request should return (from the official API documentation: \emph{The maximum number of tokens to generate in the completion. The token count of your prompt plus max_tokens cannot exceed the model's context length. Most models have a context length of 2048 tokens (except for the newest models, which support 4096)})} |
| 33 | |
| 34 | \item{temperature}{numeric (default: 0.9) specifying the sampling strategy of the possible completions (from the official API documentation: \emph{What sampling temperature to use. Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer. We generally recommend altering this or top_p but not both.})} |
| 35 | |
| 36 | \item{top_p}{numeric (default: 1) specifying sampling strategy as an alternative to the temperature sampling (from the official API documentation: \emph{An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10\% probability mass are considered. We generally recommend altering this or temperature but not both.})} |
| 37 | |
| 38 | \item{n}{numeric (default: 1) specifying the number of completions per request (from the official API documentation: \emph{How many completions to generate for each prompt. \strong{Note: Because this parameter generates many completions, it can quickly consume your token quota.} Use carefully and ensure that you have reasonable settings for max_tokens and stop.})} |
| 39 | |
| 40 | \item{logprobs}{numeric (default: NULL) (from the official API documentation: \emph{Include the log probabilities on the logprobs most likely tokens, as well the chosen tokens. For example, if logprobs is 5, the API will return a list of the 5 most likely tokens. The API will always return the logprob of the sampled token, so there may be up to logprobs+1 elements in the response. The maximum value for logprobs is 5. If you need more than this, please contact support@openai.com and describe your use case.})} |
| 41 | |
| 42 | \item{stop}{character or character vector (default: NULL) that specifies after which character value when the completion should end (from the official API documentation: \emph{Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.})} |
| 43 | |
| 44 | \item{presence_penalty}{numeric (default: 0) between -2.00 and +2.00 to determine the penalisation of repetitiveness if a token already exists (from the official API documentation: \emph{Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.}). See also: \url{https://beta.openai.com/docs/api-reference/parameter-details}} |
| 45 | |
| 46 | \item{frequency_penalty}{numeric (default: 0) between -2.00 and +2.00 to determine the penalisation of repetitiveness based on the frequency of a token in the text already (from the official API documentation: \emph{Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.}). See also: \url{https://beta.openai.com/docs/api-reference/parameter-details}} |
| 47 | |
| 48 | \item{best_of}{numeric (default: 1) that determines the space of possibilities from which to select the completion with the highest probability (from the official API documentation: \emph{Generates \code{best_of} completions server-side and returns the "best" (the one with the highest log probability per token)}). See details.} |
| 49 | } |
| 50 | \value{ |
| 51 | A list with two data tables (if \code{output_type} is the default "complete"): [\link{1}] contains the data table with the columns \code{n} (= the mo. of \code{n} responses requested), \code{prompt} (= the prompt that was sent), and \code{gpt3} (= the completion as returned from the GPT-3 model). [\link{2}] contains the meta information of the request, including the request id, the parameters of the request and the token usage of the prompt (\code{tok_usage_prompt}), the completion (\code{tok_usage_completion}) and the total usage (\code{tok_usage_total}). |
| 52 | |
| 53 | If \code{output_type} is "text", only the data table in slot [\link{1}] is returned. |
| 54 | |
| 55 | If \code{output_type} is "meta", only the data table in slot [\link{2}] is returned. |
| 56 | } |
| 57 | \description{ |
ben-aaron188 | 718e3a6 | 2022-10-24 14:28:51 +0200 | [diff] [blame^] | 58 | \code{gpt3_single_completion()} sends a single \href{https://beta.openai.com/docs/api-reference/completions}{completion request} to the Open AI GPT-3 API. |
ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 59 | } |
| 60 | \details{ |
| 61 | For a general guide on the completion requests, see \url{https://beta.openai.com/docs/guides/completion}. This function provides you with an R wrapper to send requests with the full range of request parameters as detailed on \url{https://beta.openai.com/docs/api-reference/completions} and reproduced below. |
| 62 | |
| 63 | For the \code{best_of} parameter: When used with n, best_of controls the number of candidate completions and n specifies how many to return – best_of must be greater than n. Note that this is handled by the wrapper automatically if(best_of <= n){ best_of = n}. |
| 64 | |
| 65 | Parameters not included/supported: |
| 66 | \itemize{ |
| 67 | \item \code{logit_bias}: \url{https://beta.openai.com/docs/api-reference/completions/create#completions/create-logit_bias} |
| 68 | \item \code{echo}: \url{https://beta.openai.com/docs/api-reference/completions/create#completions/create-echo} |
| 69 | \item \code{stream}: \url{https://beta.openai.com/docs/api-reference/completions/create#completions/create-stream} |
| 70 | } |
| 71 | } |
| 72 | \examples{ |
| 73 | # First authenticate with your API key via `gpt3_authenticate('pathtokey')` |
| 74 | |
| 75 | # Once authenticated: |
| 76 | |
| 77 | ## Simple request with defaults: |
ben-aaron188 | 718e3a6 | 2022-10-24 14:28:51 +0200 | [diff] [blame^] | 78 | gpt3_single_completion(prompt_input = 'How old are you?') |
ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 79 | |
| 80 | ## Instruct GPT-3 to write ten research ideas of max. 150 tokens with some controls: |
ben-aaron188 | 718e3a6 | 2022-10-24 14:28:51 +0200 | [diff] [blame^] | 81 | gpt3_single_completion(prompt_input = 'Write a research idea about using text data to understand human behaviour:' |
ben-aaron188 | 5bcd911 | 2022-09-10 21:33:50 +0200 | [diff] [blame] | 82 | , temperature = 0.8 |
| 83 | , n = 10 |
| 84 | , max_tokens = 150) |
ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 85 | |
| 86 | ## For fully reproducible results, we need `temperature = 0`, e.g.: |
ben-aaron188 | 718e3a6 | 2022-10-24 14:28:51 +0200 | [diff] [blame^] | 87 | gpt3_single_completion(prompt_input = 'Finish this sentence:/n There is no easier way to learn R than' |
ben-aaron188 | 5bcd911 | 2022-09-10 21:33:50 +0200 | [diff] [blame] | 88 | , temperature = 0.0 |
| 89 | , max_tokens = 50) |
ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 90 | |
| 91 | ## The same example with a different GPT-3 model: |
ben-aaron188 | 718e3a6 | 2022-10-24 14:28:51 +0200 | [diff] [blame^] | 92 | gpt3_single_completion(prompt_input = 'Finish this sentence:/n There is no easier way to learn R than' |
ben-aaron188 | 5bcd911 | 2022-09-10 21:33:50 +0200 | [diff] [blame] | 93 | , model = 'text-babbage-001' |
| 94 | , temperature = 0.0 |
| 95 | , max_tokens = 50) |
ben-aaron188 | 85c32a0 | 2022-09-10 20:30:30 +0200 | [diff] [blame] | 96 | } |