Learn R Programming

chatAI4R (version 0.3.6)

completions4R: completions4R: Generate text using OpenAI completions API (Legacy)

Description

This function sends a request to the OpenAI completions API (Legacy) to generate text based on the provided prompt and parameters.

Usage

completions4R(
  prompt,
  api_key = Sys.getenv("OPENAI_API_KEY"),
  Model = "davinci-002",
  max_tokens = 50,
  temperature = 1,
  simple = TRUE
)

Value

The generated text or the full response from the API, depending on the value of `simple`.

Arguments

prompt

A string. The initial text that the model responds to.

api_key

A string. The API key for OpenAI. Defaults to the value of the environment variable "OPENAI_API_KEY".

Model

A string. The model ID to use, such as "davinci-002".

max_tokens

Integer. The maximum number of tokens to generate.

temperature

A numeric value to control the randomness of the generated text. A value close to 0 produces more deterministic output, while a higher value (up to 2) produces more random output.

simple

If TRUE, returns the generated text without newline characters. If FALSE, returns the full response from the API.

Author

Satoshi Kume

Examples

Run this code
if (FALSE) {
#This is a legacy function
Sys.setenv(OPENAI_API_KEY = "Your API key")

prompt <- "Translate the following English text to French: 'Hello, world!'"

completions4R(prompt)
}

Run the code above in your browser using DataLab