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

R Powered by R Made with R Mistral

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

# Define the function
call_mistral_api <- function(
  model,
  messages,
  temperature = 0.7,
  top_p = 1,
  max_tokens = NULL,
  min_tokens = NULL,
  stream = FALSE,
  stop = NULL,
  random_seed = NULL,
  response_format = NULL,
  tools = NULL,
  tool_choice = "auto",
  safe_prompt = FALSE
) {
  # Get the API key from environment variables
  api_key <- Sys.getenv("MISTRAL_API_KEY")
  
  if (api_key == "") {
    stop("API key not found in environment variables. Please set MISTRAL_API_KEY.")
  }

  url <- "https://api.mistral.ai/v1/chat/completions"

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

  body <- list(
    model = model,
    messages = messages,
    temperature = temperature,
    top_p = top_p,
    stream = stream,
    tool_choice = tool_choice,
    safe_prompt = safe_prompt
  )

  # Add optional parameters if they are not NULL
  if (!is.null(max_tokens)) {
    body$max_tokens <- max_tokens
  }
  if (!is.null(min_tokens)) {
    body$min_tokens <- min_tokens
  }
  if (!is.null(stop)) {
    body$stop <- stop
  }
  if (!is.null(random_seed)) {
    body$random_seed <- random_seed
  }
  if (!is.null(response_format)) {
    body$response_format <- response_format
  }
  if (!is.null(tools)) {
    body$tools <- tools
  }

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

  if (status_code(response) != 200) {
    stop("API request failed: ", content(response, "text", encoding = "UTF-8"))
  }

  result <- content(response, as = "parsed", encoding = "UTF-8")
  return(result)
}
In [2]:
messages <- list(
  list(role = "user", content = "give me a list of gift idea for my girl friend")
)
In [3]:
response <- call_mistral_api(
  model = "mistral-large-latest",  # Replace with the model ID you wish to use
  messages = messages,
  temperature = 0.7,
  top_p = 1,
  max_tokens = 2024
)

# Display the assistant's response
cat(response$choices[[1]]$message$content)
Here's a list of gift ideas for your girlfriend, categorized by interest:

1. **Romantic & Sentimental:**
   - Personalized jewelry, like a necklace with her initial or a significant date engraved
   - A memory jar filled with notes about cherished moments together
   - A custom star map of the night sky on a significant date
   - Love letters or a heartfelt poem written by you
   - A photo album or scrapbook documenting your journey together

2. **Self-Care & Beauty:**
   - High-quality skincare or makeup products she's been eyeing
   - A plush robe and luxurious body wash for relaxing baths
   - Aromatherapy diffuser with essential oils
   - A cozy blanket and a good book by her favorite author
   - A spa day at home with face masks, bath bombs, and a foot massager

3. **Fashion & Accessories:**
   - A stylish purse or wallet she's been wanting
   - A trendy pair of shoes or boots
   - A chic scarf or hat to complement her wardrobe
   - A piece of clothing she's had her eye on, in her size and favorite color
   - Sunglasses that suit her face shape and style

4. **Hobbies & Interests:**
   - If she loves cooking: A high-quality cookbook, kitchen gadget, or cooking classes
   - If she loves reading: A highly-rated book by her favorite author or a stylish book club subscription
   - If she loves fitness: New workout gear, a trendy water bottle, or a subscription to a fitness app
   - If she loves art: Art supplies, a beautiful sketchbook, or a museum membership
   - If she loves music: Concert tickets, a stylish portable speaker, or a vinyl record of her favorite album

5. **Unique Experiences:**
   - A weekend getaway to a nearby bed and breakfast or resort
   - Tickets to a local event, like a play, musical, or comedy show
   - A wine tasting tour or brewery visit
   - A hot air balloon ride or scenic helicopter tour
   - A couples' cooking class or dance lessons

6. **Tech Gadgets:**
   - Wireless earbuds for music or podcasts on the go
   - A Kindle e-reader if she loves to read
   - A smart speaker for her bedroom or kitchen
   - A portable phone charger for her busy days
   - A digital photo frame to display cherished memories

Choose a gift that shows you listen to her and care about her interests. The thought and effort you put into her gift will mean the most.