## ----setup, include = FALSE---------------------------------------------------
fixture_dir <- "getting-started"
recording <- nzchar(Sys.getenv("FOUNDRY_RECORD_DOCS"))
have_fixtures <- dir.exists(fixture_dir) && length(list.files(fixture_dir)) > 0
run_api <- requireNamespace("httptest2", quietly = TRUE) &&
  (recording || have_fixtures)

# Attach foundryR before start_vignette(): httptest2 only sources the package's
# inst/httptest2/start-vignette.R (which sets replay placeholders) from attached
# packages.
library(foundryR)

if (run_api) {
  httptest2::start_vignette(fixture_dir)
}

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = run_api
)

## ----install, eval = FALSE----------------------------------------------------
# install.packages("pak")
# pak::pak("farach/foundryR")

## ----config-session, eval = FALSE---------------------------------------------
# library(foundryR)
# 
# foundry_set_endpoint(Sys.getenv("AZURE_FOUNDRY_ENDPOINT"))
# foundry_set_key("your-api-key")

## ----config-persistent, eval = FALSE------------------------------------------
# local({
#   config_file <- tempfile("foundryR-config-", fileext = ".json")
#   old_options <- options(foundryR.config_file = config_file)
#   old_env <- Sys.getenv(
#     c("AZURE_FOUNDRY_ENDPOINT", "AZURE_FOUNDRY_KEY"),
#     unset = NA_character_
#   )
#   on.exit({
#     options(old_options)
#     Sys.unsetenv(names(old_env)[is.na(old_env)])
#     keep <- !is.na(old_env)
#     if (any(keep)) {
#       do.call(Sys.setenv, as.list(old_env[keep]))
#     }
#     unlink(config_file)
#   }, add = TRUE)
# 
#   foundry_set_endpoint("https://example.openai.azure.com", store = TRUE)
#   foundry_set_key("example-key-not-a-secret", store = TRUE)
# })

## ----entra-token, eval = FALSE------------------------------------------------
# foundry_set_token("your-entra-token")

## ----check-setup, eval = FALSE------------------------------------------------
# foundry_check_setup()

## ----check-setup-model, eval = FALSE------------------------------------------
# foundry_check_setup(model = "gpt-5-nano")

## ----models, eval = FALSE-----------------------------------------------------
# models <- foundry_models()
# models[, c("id", "owned_by")]

## ----first-response-----------------------------------------------------------
library(foundryR)

response <- foundry_response("Answer in one sentence: what is R?")

response$output_text

## ----response-chain-----------------------------------------------------------
follow_up <- foundry_response(
  "Explain why that matters for data analysis in one sentence.",
  previous_response_id = response$response_id
)

follow_up$output_text

## ----first-extract------------------------------------------------------------
schema <- list(
  type = "object",
  properties = list(
    sentiment = list(type = "string", enum = c("positive", "negative", "neutral")),
    topic = list(type = "string")
  ),
  required = c("sentiment", "topic"),
  additionalProperties = FALSE
)

foundry_extract(
  c("The tutorial was clear.", "I needed more examples."),
  schema = schema
)

## ----first-embed--------------------------------------------------------------
texts <- c(
  "The tutorial was clear.",
  "The lecture needed more examples.",
  "The assignment instructions were easy to follow."
)

embeddings <- foundry_embed(texts, model = "text-embedding-3-small")
foundry_similarity(embeddings)

## ----content-safety-config, eval = FALSE--------------------------------------
# foundry_set_content_safety_endpoint(Sys.getenv("AZURE_CONTENT_SAFETY_ENDPOINT"))
# foundry_set_content_safety_key("your-content-safety-key")

## ----safety-gate--------------------------------------------------------------
source <- "The program enrolled 82 students in 2026."
answer <- "The program enrolled 82 students in 2026."

grounded <- foundry_groundedness(
  text = answer,
  grounding_sources = source,
  query = "How many students enrolled?",
  task = "QnA"
)

shield <- foundry_shield(user_prompt = "Summarize this document.")

grounded
shield

## ----first-chat---------------------------------------------------------------
foundry_chat("Answer in one sentence: what is the tidyverse?")

## ----cleanup, include = FALSE-------------------------------------------------
if (run_api) {
  httptest2::end_vignette()
}

