## ----include=FALSE------------------------------------------------------------ knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = identical(tolower(Sys.getenv("LLMRAGENT_RUN_VIGNETTES", "false")), "true") ) ## ----setup-------------------------------------------------------------------- # library(LLMRagent) # cfg <- LLMR::llm_config("groq", "openai/gpt-oss-20b", temperature = 0.7) ## ----first-------------------------------------------------------------------- # ada <- agent( # "Ada", cfg, # persona = "You are Ada, a meticulous statistician. Answer in one or two sentences." # ) # ada$chat("What is overfitting?") # ada$chat("How would I detect it in practice?") # remembers the thread # ada$usage() ## ----stream------------------------------------------------------------------- # ada$chat("Explain cross-validation to a newcomer, in one paragraph.", # stream = TRUE) ## ----tools-------------------------------------------------------------------- # lookup_gdp <- LLMR::llm_tool( # function(country) { # gdp <- c(chile = 335, uruguay = 81, bolivia = 47) # USD bn, illustrative # val <- gdp[tolower(country)] # if (is.na(val)) "unknown" else paste0("$", val, " billion") # }, # name = "lookup_gdp", # description = "Look up a country's GDP in USD billions.", # parameters = list(country = list(type = "string", description = "Country name")) # ) # # analyst <- agent("Analyst", cfg, tools = lookup_gdp, # persona = "A careful economic analyst. Use tools for any figure.") # analyst$chat("Compare the GDPs of Chile and Uruguay using the lookup tool.") # analyst$trace() # every model call and tool call, with tokens and timing ## ----budgets------------------------------------------------------------------ # frugal <- agent("Frugal", cfg, budget = budget(max_calls = 2)) # frugal$chat("one") # frugal$chat("two") # tryCatch(frugal$chat("three"), # llmragent_budget_error = function(e) "stopped by budget, as designed") ## ----structured--------------------------------------------------------------- # schema <- list( # type = "object", # properties = list(stance = list(type = "string", # enum = list("support", "oppose", "unsure")), # reason = list(type = "string")), # required = list("stance", "reason") # ) # ada$ask_structured("Should small samples use t or z intervals?", schema) ## ----delegation--------------------------------------------------------------- # stat <- agent("Stat", cfg, # persona = "A PhD statistician. Precise about assumptions.") # hist <- agent("Hist", cfg, # persona = "An economic historian. Institutional context.") # # lead <- agent("Lead", cfg, # persona = "A research lead. Consult specialists, then synthesize.", # tools = list(agent_as_tool(stat), agent_as_tool(hist))) # # lead$chat("Crime fell while policing budgets rose, across many cities. # What would it take to argue causality?") # stat$usage() # the consultation showed up here ## ----pipeline----------------------------------------------------------------- # run <- agent_pipeline( # list( # agent("Extractor", cfg, persona = # "Extract every factual claim as a numbered list. Nothing else."), # agent("Checker", cfg, persona = # "Mark each numbered claim VERIFIABLE or VAGUE, one line each."), # agent("Editor", cfg, persona = # "Rewrite the original passage keeping only VERIFIABLE claims.") # ), # input = "Our app doubled retention, won three design awards, and users love it." # ) # run$output # run$steps # step, agent, input, output -- the full audit trail ## ----conversation------------------------------------------------------------- # rosa <- agent("Rosa", cfg, persona = "A pragmatic city planner. Concrete and brief.") # hugo <- agent("Hugo", cfg, persona = "A skeptical economist. Numbers first. Brief.") # # conv <- conversation( # list(rosa, hugo), # topic = "Should the city pedestrianize its center?", # max_turns = 4, # instruction = "At most three sentences per turn." # ) # conv$transcript