How create a function for get books summary from open AI ?

Hi community, Im want to create a function to connect to open IA with a API services and try to find the summary of a title books list.

When run print show me NULL

library(httr)
library(jsonlite)

api_key <- 'sk-proj-API'

get_book_summary <- function(book_title) {
  url <- "https://api.openai.com/v1/chat/completions"
  
  body <- toJSON(list(
    model = "gpt-4", # Im try with gpt-3.5-turbo
    messages = list(
      list(role = "system", content = "Summarize books"),
      list(role = "user", content = book_title)
    ),
    max_tokens = 150
  ), auto_unbox = TRUE)
  
  response <- POST(url,
                   add_headers(`Authorization` = paste("Bearer",'sk-proj-API'),
                               `Content-Type` = "application/json"),
                   body = body)
  
  if (status_code(response) != 200) {
    print(paste("Error:", status_code(response)))
    print(content(response, "text"))
    return(NULL)
  }

  result <- fromJSON(rawToChar(response$content), simplifyVector = FALSE)

  if (length(result$choices) > 0 && nzchar(result$choices[[1]]$text)) {
    return(result$choices[[1]]$text)
  } else {
    print("No summary returned")
    return(NULL)
  }
}

book_title <- "Deep R Programming"
summary <- get_book_summary(book_title)
print(summary)
# NULL

Tnks for any advance!

Next to try many times, Im find this solution.

Im not sure if is the better solutions but work for me:

library(httr)
library(jsonlite)
library(purrr)

get_book_summary <- function(book_title) {
  url <- "https://api.openai.com/v1/chat/completions"
  
  body <- toJSON(list(
    model = "gpt-4",
    messages = list(
      list(role = "system", content = "Summarize books"),
      list(role = "user", content = book_title)
    ),
    max_tokens = 150
  ), auto_unbox = TRUE)
  
  response <- POST(url,
                   add_headers(`Authorization` = paste("Bearer",'sk-proj-API'),
                               `Content-Type` = "application/json"),
                   body = body)
  
  if (status_code(response) != 200) {
    print(paste("Error:", status_code(response)))
    print(content(response, "text"))  
    return("Error: Unable to retrieve summary")
  }
  
  result <- fromJSON(rawToChar(response$content), simplifyVector = FALSE)
  print(result) 
  
  if (!is.null(result$choices) && length(result$choices) > 0 &&
      !is.null(result$choices[[1]]$message) && 
      !is.null(result$choices[[1]]$message$content) && 
      nzchar(result$choices[[1]]$message$content)) {
    return(result$choices[[1]]$message$content)
  } else {
    print("No summary returned or incorrect structure")
    return("No summary available")
  }
}

# Get summaries for each book using map
summaries <- map(book_title, get_book_summary) # get the summaries 

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.