## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----eval=FALSE---------------------------------------------------------------
#  Sys.setenv(
#    AZURE_OPENAI_ENDPOINT    = "<your-endpoint>",
#    AZURE_OPENAI_API_KEY     = "<your-key>",
#    AZURE_OPENAI_API_VERSION = "<api-version>"
#  )

## ----eval=FALSE---------------------------------------------------------------
#  library(finnts)
#  library(dplyr)
#  
#  project <- set_project_info(
#    project_name = "ai_agent_demo",
#    path = tempdir(), # or a persistent folder
#    combo_variables = c("id"),
#    target_variable = "value",
#    date_type = "month", # day|week|month|quarter|year
#    fiscal_year_start = 1 # fiscal month (1 = Jan)
#  )

## ----eval=FALSE---------------------------------------------------------------
#  hist_data <- timetk::m4_monthly %>%
#    dplyr::filter(date >= as.Date("2013-01-01")) %>%
#    dplyr::rename(Date = date) %>%
#    dplyr::mutate(id = as.character(id))

## ----eval=FALSE---------------------------------------------------------------
#  llm <- ellmer::chat_azure_openai(model = "gpt-4o-mini")

## ----eval=FALSE---------------------------------------------------------------
#  agent <- set_agent_info(
#    project_info = project,
#    llm = llm,
#    input_data = hist_data,
#    forecast_horizon = 6, # number of future periods
#    external_regressors = NULL, # e.g., c("Price","Promo")
#    allow_hierarchical_forecast = FALSE, # set TRUE to let agent use hierarchies
#    negative_forecast = FALSE, # set TRUE to allow forecasts below zero
#    overwrite = TRUE # start a fresh run_id if inputs changed
#  )

## ----eval=FALSE---------------------------------------------------------------
#  iterate_forecast(
#    agent_info          = agent,
#    weighted_mape_goal  = 0.05, # your accuracy target of 5%
#    max_iter            = 3, # stop after N iterations if not hitting goal
#  )

## ----eval=FALSE---------------------------------------------------------------
#  best_runs <- get_best_agent_run(agent_info = agent, full_run_info = TRUE)
#  head(best_runs)
#  
#  fcst <- get_agent_forecast(agent_info = agent)
#  head(fcst)

## ----eval=FALSE---------------------------------------------------------------
#  # Ask about forecast accuracy
#  answer <- ask_agent(
#    agent_info = agent,
#    question = "What is the average weighted MAPE across all time series?"
#  )
#  
#  # Ask about models used
#  answer <- ask_agent(
#    agent_info = agent,
#    question = "Which models were selected as best for each time series?"
#  )
#  
#  # Ask about feature importance
#  answer <- ask_agent(
#    agent_info = agent,
#    question = "What are the top 3 most important features for the forecast models?"
#  )
#  
#  # Ask about data quality
#  answer <- ask_agent(
#    agent_info = agent,
#    question = "Were there any missing values or outliers in the data?"
#  )
#  
#  # Ask about specific forecasts
#  answer <- ask_agent(
#    agent_info = agent,
#    question = "What are the forecasted values for M750 for the next 3 months?"
#  )
#  
#  # Ask about time series characteristics
#  answer <- ask_agent(
#    agent_info = agent,
#    question = "Which time series show strong seasonality patterns?"
#  )
#  
#  # Ask comparative questions
#  answer <- ask_agent(
#    agent_info = agent,
#    question = "Which time series have the highest forecast uncertainty?"
#  )

## ----eval=FALSE---------------------------------------------------------------
#  # suppose you've appended more months to hist_data:
#  hist_data2 <- hist_data %>% dplyr::filter(Date <= as.Date("2016-06-01"))
#  
#  agent2 <- set_agent_info(
#    project_info = project,
#    llm = llm,
#    input_data = hist_data2,
#    forecast_horizon = 6,
#    overwrite = TRUE # required to create a new agent version when running update_forecast()
#  )
#  
#  update_forecast(
#    agent_info             = agent2,
#    weighted_mape_goal     = 0.05,
#    allow_iterate_forecast = TRUE, # if degradation detected, allow the agent to re-iterate
#    max_iter               = 2 # cap re-iteration cost
#  )
#  
#  updated_fcst <- get_agent_forecast(agent2)
#  
#  # Ask questions about the updated forecast
#  answer <- ask_agent(
#    agent_info = agent2,
#    question = "Summarize the forecast accuracy."
#  )

