R - OPENAI - API
Simon-Pierre Boucher
2024-09-14

Made with R Powered by R R OpenAI Powered by OpenAI Using OpenAI OpenAI GPT-4 GPT-3

In [1]:
library(httr)
library(jsonlite)

# Définition de la fonction
appel_api_openai <- function(
  model = "gpt-4",
  messages,
  temperature = 1,
  max_tokens = 2048,
  top_p = 1,
  frequency_penalty = 0,
  presence_penalty = 0
) {
  # Récupérer la clé API depuis les variables d'environnement
  api_key <- Sys.getenv("OPENAI_API_KEY")
  
  if (api_key == "") {
    stop("La clé API n'a pas été trouvée dans les variables d'environnement. Veuillez définir OPENAI_API_KEY.")
  }

  url <- "https://api.openai.com/v1/chat/completions"

  headers <- add_headers(
    `Content-Type` = "application/json",
    Authorization = paste("Bearer", api_key)
  )

  body <- list(
    model = model,
    messages = messages,
    temperature = temperature,
    max_tokens = max_tokens,
    top_p = top_p,
    frequency_penalty = frequency_penalty,
    presence_penalty = presence_penalty
  )

  response <- POST(url, headers, body = toJSON(body, auto_unbox = TRUE))

  if (status_code(response) != 200) {
    stop("La requête API a échoué : ", content(response, "text", encoding = "UTF-8"))
  }

  result <- content(response, as = "parsed", encoding = "UTF-8")
  return(result)
}
In [2]:
messages <- list(
  list(role = "system", content = "Tu es un assistant"),
  list(role = "user", content = "give me a list of gift idea for my girldfriend")
)
In [3]:
réponse <- appel_api_openai(
  model = "gpt-4",
  messages = messages,
  temperature = 1,
  max_tokens = 2048,
  top_p = 1,
  frequency_penalty = 0,
  presence_penalty = 0
)

# Afficher la réponse de l'assistant
cat(réponse$choices[[1]]$message$content)
1. Jewelry: If she loves accessories, pieces like necklaces, bracelets, earrings, or rings could be a great choice.

2. Luxury Skincare Products: High-end skincare can offer a taste of luxury she may not buy for herself.

3. Personalized Art: This could be something like a custom portrait, a piece featuring a quote she loves, or a piece that represents a shared memory.

4. Hand-written letter: Pour out your feelings onto paper and showcase your affection towards her.

5. Cooking Class for Two: Spending time together learning a new skill can be fun and romantic.

6. High-Quality Makeup: If she enjoys wearing makeup, she would likely appreciate receiving new products to try.

7. Designer Handbag: If your budget allows, a nice handbag from a designer she loves could be a great choice.

8. Spa Day: Everyone appreciates a day of relaxation and pampering.

9. Books: If she loves to read, consider a book from her favorite author or in a genre she enjoys.

10. A gadget: If she is into technology, think about getting her a new phone, smart watch, tablet, or a pair of headphones.

11. Subscription Box: A monthly box can bring joy all year, consider ones of beauty products, books, healthy snacks, etc.

12. Romantic Getaway: Plan a surprise weekend trip to a place she's been wanting to visit.

13. Personalized Jewelry: Jewellery with an engraving or her initials adds a personal touch.

14. Custom-Made Ornaments or Pottery: These items can serve as wonderful keepsakes.

15. Cooking Appliances: If she enjoys cooking, a fancy new blender or stand mixer might be much appreciated.

16. Fitness Gear: This could be anything from a yoga mat to a set of weights or even a bicycle if she loves cycling.

17. Plants: If she loves nature, she might enjoy some new houseplants or flowers for her garden.

18. Concert or Theater Tickets: If she has a favorite band or show, she would likely love to attend a live performance.

19. Quality Chocolates: A box of high-quality chocolates can be a great way to treat her.

20. Perfume: A bottle of her favorite scent or something new you think she would enjoy.

Remember that the best gift is personal and thoughtful. Try to think about what she would truly appreciate. Happy gifting.