## ----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)
library(foundryR)
if (run_api) {
  httptest2::start_vignette(fixture_dir)
}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", eval = run_api,
  fig.width = 7, fig.height = 4.5, out.width = "100%")

## ----install-cran, eval = FALSE-----------------------------------------------
# install.packages("foundryR")

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

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

## ----config-entra, eval = FALSE-----------------------------------------------
# foundry_set_endpoint(Sys.getenv("AZURE_FOUNDRY_ENDPOINT"), store = TRUE)
# foundry_set_token_provider(foundry_token_azure_cli(), scope = "resource")
# foundry_set_token_provider(foundry_token_azure_cli("https://ai.azure.com"), scope = "project")

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

## ----config-project, eval = FALSE---------------------------------------------
# foundry_set_project_endpoint(Sys.getenv("AZURE_FOUNDRY_PROJECT_ENDPOINT"), store = TRUE)

## ----deployment-name, eval = FALSE--------------------------------------------
# foundry_response("Code this comment.", model = "course-coder")

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

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

response$output_text

## ----first-response-tokens----------------------------------------------------
response[, c(
  "input_tokens",
  "output_tokens",
  "reasoning_tokens",
  "cached_input_tokens",
  "total_tokens"
)]

## ----first-extract------------------------------------------------------------
schema <- foundry_schema(
  sentiment = schema_enum(c("positive", "negative", "mixed")),
  issue = schema_string("A short label for what the comment is about.")
)

comments <- c(
  "The lecture made regression much clearer.",
  "The homework instructions were hard to follow.",
  "The examples helped, but I wanted more time for practice."
)

coded <- foundry_extract(comments, schema = schema)
coded[, c("sentiment", "issue")]

## ----first-embed--------------------------------------------------------------
texts <- c(
  "The lecture made regression much clearer.",
  "Regression finally made sense after this class.",
  "The homework instructions were hard to follow."
)

embeddings <- foundry_embed(texts, model = "text-embedding-3-small")
embeddings[, c("text", "n_dims")]

foundry_similarity(embeddings, top_k = 1)

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

