Interacts with the OpenAI API to obtain chat completions based on the GPT model. The function supports various customization parameters for the request. Additionally, it includes functionality to re-call the API if the returned 'message.content' is not properly formed JSON, ensuring more robust API interaction.

api_openai_chat_completions(
  model = "gpt-3.5-turbo",
  system_message = "",
  user_message = "",
  temperature = 1,
  top_p = 1,
  n = 1,
  stream = FALSE,
  stop = NULL,
  max_tokens = NULL,
  presence_penalty = 0,
  frequency_penalty = 0,
  logit_bias = NULL,
  user = NULL,
  openai_api_key = Sys.getenv("OPENAI_API_KEY"),
  openai_organization = NULL,
  is_json_output = TRUE
)

Arguments

model

The model to use, defaults to 'gpt-3.5-turbo'.

system_message

System prompt

user_message

User prompt

temperature

Controls randomness in generation, default 1.

top_p

Controls diversity of generation, default 1.

n

Number of completions to generate, default 1.

stream

If TRUE, returns a stream of responses, default FALSE.

stop

Sequence of tokens which will automatically complete generation.

max_tokens

The maximum number of tokens to generate, NULL for no limit.

presence_penalty

Alters likelihood of new topics, default 0.

frequency_penalty

Alters likelihood of repeated topics, default 0.

logit_bias

A named list of biases to apply to token logits.

user

An identifier for the user, if applicable.

openai_api_key

The API key for OpenAI, defaults to the environment variable OPENAI_API_KEY.

openai_organization

Optional organization identifier for the API.

is_json_output

If TRUE, ensures output is valid JSON, default TRUE.

Value

Returns a string with the API response, or JSON if is_json_output is TRUE.

Examples

if (FALSE) {
api_openai_chat_completions(
   model = "gpt-3.5-turbo",
   messages = list(
       list(
           "role" = "system",
           "content" = "You are an expert at life."
       ),
       list(
           "role" = "user",
           "content" = "Where is the party at?"
       )
   )
)
}