Skip to content

Instantly share code, notes, and snippets.

@kevin-m-kent
Last active August 5, 2024 17:08
Show Gist options
  • Save kevin-m-kent/42e25781ac1af9fd753aa58b057259e5 to your computer and use it in GitHub Desktop.
Save kevin-m-kent/42e25781ac1af9fd753aa58b057259e5 to your computer and use it in GitHub Desktop.
library(httr2)
library(purrr)
endpoint <- "https://models.inference.ai.azure.com"
token <- Sys.getenv("github_token")
model <- "gpt-4o-mini"
system <- list(
"role" = "system",
"content" = "You are a helpful assistant."
)
user <- list(
"role" = "user",
"content" = "What is the capital of France?"
)
req <- request(endpoint) |>
req_url_path_append("chat") |>
req_url_path_append("completions") |>
req_body_json(
list(
"messages" = list(
system,
user
),
"model" = model,
"temperature" = 1,
"max_tokens" = 1000,
"top_p" = 1
)
) |>
req_auth_bearer_token(token)
req |>
req_perform() |>
resp_body_json() |>
pluck("choices", 1, "message", "content") |>
cat() # to do nicer formatting in the console
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment