## ----setup, include = FALSE--------------------------------------------------- fixture_dir <- "responses-api" 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%") ## ----project-route, eval = FALSE---------------------------------------------- # foundry_set_project_endpoint(Sys.getenv("AZURE_FOUNDRY_PROJECT_ENDPOINT")) # # foundry_response( # "Summarize the project route in one sentence.", # project_endpoint = Sys.getenv("AZURE_FOUNDRY_PROJECT_ENDPOINT") # ) ## ----basic-response----------------------------------------------------------- basic <- foundry_response( "Answer in one sentence: what is retrieval-augmented generation?" ) basic$output_text basic[, c( "input_tokens", "output_tokens", "reasoning_tokens", "cached_input_tokens", "total_tokens" )] ## ----stateful-turns----------------------------------------------------------- first <- foundry_response( "Define catastrophic forgetting in one sentence." ) second <- foundry_response( "Explain it for a college freshman in one sentence.", previous_response_id = first$response_id ) second$output_text second[, c("response_id", "input_tokens", "output_tokens", "total_tokens")] ## ----structured-extraction---------------------------------------------------- comment_schema <- foundry_schema( sentiment = schema_enum(c("positive", "negative", "neutral")), entities = schema_array(schema_string()), summary = schema_string() ) comments <- c( "The new data pipeline reduced manual coding time by half.", "Participants reported confusion about the consent form." ) comment_codes <- foundry_extract( comments, schema = comment_schema, schema_name = "CommentCode" ) comment_codes[, c("sentiment", "entities", "summary", ".status")] ## ----ellmer-schema, eval = requireNamespace("ellmer", quietly = TRUE)--------- sentiment_spec <- ellmer::type_object( sentiment = ellmer::type_enum( c("positive", "negative", "neutral"), description = "Overall sentiment of the response." ), theme = ellmer::type_string("A short theme label for the response.") ) sentiment_schema <- as_foundry_schema(sentiment_spec) jsonlite::toJSON(sentiment_schema, auto_unbox = TRUE, pretty = TRUE) ## ----function-tools----------------------------------------------------------- get_weather <- function(location) { list(location = location, temperature = "70 F") } weather_tool <- foundry_tool( get_weather, description = "Get weather for a location", parameters = foundry_schema( location = schema_string("City and state.") ) ) tool_turns <- foundry_agent( "What is the weather in San Francisco?", tools = list(weather_tool), max_iterations = 4 ) tool_turns[, c("iteration", "final", "output_text")] tool_turns$tool_calls[[1]][, c("type", "name", "call_id", "arguments")] tool_turns$tool_results[[1]] ## ----mcp-tool, eval = FALSE--------------------------------------------------- # mcp_tool <- list( # type = "mcp", # server_label = "approved_server", # server_url = Sys.getenv("MY_MCP_SERVER_URL"), # require_approval = "never" # ) # # foundry_response( # "Use the MCP server if it helps answer the question.", # tools = list(mcp_tool) # ) ## ----web-search--------------------------------------------------------------- options(foundryR.web_search_warning = TRUE) web_answer <- foundry_web_search( "Which version of R does the R Project website list as the latest release, and when was it released?", search_context_size = "medium" ) web_answer$output_text web_answer$citations[[1]][, c("title", "url")] web_answer$tool_calls[[1]][, c("type", "status", "action_type", "query")] ## ----web-search-location, eval = FALSE---------------------------------------- # foundry_web_search( # "Find a recent AI research event near me.", # country = "US", # region = "Washington", # city = "Seattle", # timezone = "America/Los_Angeles" # ) ## ----reasoning---------------------------------------------------------------- reasoned <- foundry_response( "Compare the two arguments and identify the weaker premise: A says the survey item is valid because it is short. B says it is valid because respondents interpret it consistently.", reasoning_effort = "medium" ) reasoned$output_text reasoned[, c( "input_tokens", "output_tokens", "reasoning_tokens", "cached_input_tokens", "total_tokens" )] ## ----cleanup, include = FALSE------------------------------------------------- if (run_api) { httptest2::end_vignette() }