102 OpenAI Chat Completions wrapper
call_openai_chat <- function(messages, model = “gpt-3.5-turbo”, temperature = 0.7, max_tokens = 60) { req <- POST( url = “https://api.openai.com/v1/chat/completions”, add_headers( Authorization = paste(“Bearer”, api_key) ), content_type_json(), body = toJSON(list(model = model, messages = messages, temperature = temperature, max_tokens = max_tokens), auto_unbox = TRUE) ) stop_for_status(req) res <- content(req, as = “parsed”, type = “application/json”) res }