79534153

Date: 2025-03-25 15:38:08
Score: 0.5
Natty:
Report link

Here is another solution as suggested by @Parfait in the comments below

library(httr)
library(jsonlite)

# Set your DeepSeek API key
api_key <- "sk-000000000"

# Set the API endpoint URL
api_endpoint <- "https://api.deepseek.com/chat/completions"

# Prepare the request payload
payload <- list(
  model = "deepseek-chat",
  messages = list(
    list(role = "system", content = "You are a helpful assistant"),
    list(role = "user", content = "Hello")
  ),
  stream = FALSE  # Explicitly setting stream to FALSE
)

# Add the necessary headers
headers <- c(
  "Content-Type" = "application/json",
  "Authorization" = paste("Bearer", api_key)  # Correct way to pass API key
)

# Make the POST request
response <- POST(
  url = api_endpoint,
  add_headers(.headers = headers),  # Fixing the header formatting
  body = toJSON(payload, auto_unbox = TRUE, encoding = "UTF-8"),
  encode = "json"
)

# Check the response status code
if (response$status_code == 200) {
  # Parse the response content
  result <- fromJSON(content(response, "text", encoding = "UTF-8"))
  
  # Print the assistant's response
  cat(result$choices[[1]]$message$content, "\n")
} else {
  # Handle errors
  cat("Request failed with status code:", response$status_code, "\n")
  cat("Error message:", content(response, "text", encoding = "UTF-8"), "\n")
}
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Parfait
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: LocusClassicus