## ------------------------------------------------
## Method `LeadAgent$new`
## ------------------------------------------------
# An API KEY is required in order to invoke the agents
openai_4_1_mini <- ellmer::chat(
name = "openai/gpt-4.1-mini",
api_key = Sys.getenv("OPENAI_API_KEY"),
echo = "none"
)
lead_agent <- LeadAgent$new(
name = "Leader",
llm_object = openai_4_1_mini
)
## ------------------------------------------------
## Method `LeadAgent$clear_agents`
## ------------------------------------------------
# An API KEY is required in order to invoke the agents
openai_4_1_mini <- ellmer::chat(
name = "openai/gpt-4.1-mini",
api_key = Sys.getenv("OPENAI_API_KEY"),
echo = "none"
)
researcher <- Agent$new(
name = "researcher",
instruction = paste0(
"You are a research assistant. ",
"Your job is to answer factual questions with detailed and accurate information. ",
"Do not answer with more than 2 lines"
),
llm_object = openai_4_1_mini
)
summarizer <- Agent$new(
name = "summarizer",
instruction = paste0(
"You are an agent designed to summarise ",
"a given text into 3 distinct bullet points."
),
llm_object = openai_4_1_mini
)
translator <- Agent$new(
name = "translator",
instruction = "Your role is to translate a text from English to German",
llm_object = openai_4_1_mini
)
lead_agent <- LeadAgent$new(
name = "Leader",
llm_object = openai_4_1_mini
)
lead_agent$register_agents(c(researcher, summarizer, translator))
lead_agent$agents
lead_agent$clear_agents()
lead_agent$agents
## ------------------------------------------------
## Method `LeadAgent$remove_agents`
## ------------------------------------------------
# An API KEY is required in order to invoke the agents
openai_4_1_mini <- ellmer::chat(
name = "openai/gpt-4.1-mini",
api_key = Sys.getenv("OPENAI_API_KEY"),
echo = "none"
)
researcher <- Agent$new(
name = "researcher",
instruction = paste0(
"You are a research assistant. ",
"Your job is to answer factual questions with detailed and accurate information. ",
"Do not answer with more than 2 lines"
),
llm_object = openai_4_1_mini
)
summarizer <- Agent$new(
name = "summarizer",
instruction = "You are agent designed to summarise a given text into 3 distinct bullet points.",
llm_object = openai_4_1_mini
)
translator <- Agent$new(
name = "translator",
instruction = "Your role is to translate a text from English to German",
llm_object = openai_4_1_mini
)
lead_agent <- LeadAgent$new(
name = "Leader",
llm_object = openai_4_1_mini
)
lead_agent$register_agents(c(researcher, summarizer, translator))
lead_agent$agents
# deleting the translator agent
id_translator_agent <- translator$agent_id
lead_agent$remove_agents(id_translator_agent)
lead_agent$agents
## ------------------------------------------------
## Method `LeadAgent$register_agents`
## ------------------------------------------------
# An API KEY is required in order to invoke the agents
openai_4_1_mini <- ellmer::chat(
name = "openai/gpt-4.1-mini",
api_key = Sys.getenv("OPENAI_API_KEY"),
echo = "none"
)
researcher <- Agent$new(
name = "researcher",
instruction = paste0(
"You are a research assistant. ",
"Your job is to answer factual questions with detailed and accurate information. ",
"Do not answer with more than 2 lines"
),
llm_object = openai_4_1_mini
)
summarizer <- Agent$new(
name = "summarizer",
instruction = "You are agent designed to summarise a given text into 3 distinct bullet points.",
llm_object = openai_4_1_mini
)
translator <- Agent$new(
name = "translator",
instruction = "Your role is to translate a text from English to German",
llm_object = openai_4_1_mini
)
lead_agent <- LeadAgent$new(
name = "Leader",
llm_object = openai_4_1_mini
)
lead_agent$register_agents(c(researcher, summarizer, translator))
lead_agent$agents
## ------------------------------------------------
## Method `LeadAgent$invoke`
## ------------------------------------------------
if (FALSE) {
# An API KEY is required in order to invoke the agents
openai_4_1_mini <- ellmer::chat(
name = "openai/gpt-4.1-mini",
api_key = Sys.getenv("OPENAI_API_KEY"),
echo = "none"
)
researcher <- Agent$new(
name = "researcher",
instruction = paste0(
"You are a research assistant. ",
"Your job is to answer factual questions with detailed ",
"and accurate information. Do not answer with more than 2 lines"
),
llm_object = openai_4_1_mini
)
summarizer <- Agent$new(
name = "summarizer",
instruction = "You are agent designed to summarise a given text into 3 distinct bullet points.",
llm_object = openai_4_1_mini
)
translator <- Agent$new(
name = "translator",
instruction = "Your role is to translate a text from English to German",
llm_object = openai_4_1_mini
)
lead_agent <- LeadAgent$new(
name = "Leader",
llm_object = openai_4_1_mini
)
lead_agent$register_agents(c(researcher, summarizer, translator))
lead_agent$invoke(
paste0(
"Describe the economic situation in Algeria in 3 sentences. ",
"Answer in German"
)
)
}
## ------------------------------------------------
## Method `LeadAgent$generate_plan`
## ------------------------------------------------
if (FALSE) {
# An API KEY is required in order to invoke the agents
openai_4_1_mini <- ellmer::chat(
name = "openai/gpt-4.1-mini",
api_key = Sys.getenv("OPENAI_API_KEY"),
echo = "none"
)
researcher <- Agent$new(
name = "researcher",
instruction = paste0(
"You are a research assistant. Your job is to answer factual questions ",
"with detailed and accurate information. Do not answer with more than 2 lines"
),
llm_object = openai_4_1_mini
)
summarizer <- Agent$new(
name = "summarizer",
instruction = "You are agent designed to summarise a given text into 3 distinct bullet points.",
llm_object = openai_4_1_mini
)
translator <- Agent$new(
name = "translator",
instruction = "Your role is to translate a text from English to German",
llm_object = openai_4_1_mini
)
lead_agent <- LeadAgent$new(
name = "Leader",
llm_object = openai_4_1_mini
)
lead_agent$register_agents(c(researcher, summarizer, translator))
lead_agent$generate_plan(
paste0(
"Describe the economic situation in Algeria in 3 sentences. ",
"Answer in German"
)
)
}
## ------------------------------------------------
## Method `LeadAgent$broadcast`
## ------------------------------------------------
if (FALSE) {
# An API KEY is required in order to invoke the agents
openai_4_1_mini <- ellmer::chat(
name = "openai/gpt-4.1-mini",
api_key = Sys.getenv("OPENAI_API_KEY"),
echo = "none"
)
openai_4_1 <- ellmer::chat(
name = "openai/gpt-4.1",
api_key = Sys.getenv("OPENAI_API_KEY"),
echo = "none"
)
openai_4_1_agent <- Agent$new(
name = "openai_4_1_agent",
instruction = "You are an AI assistant. Answer in 1 sentence max.",
llm_object = openai_4_1
)
openai_4_1_nano <- ellmer::chat(
name = "openai/gpt-4.1-nano",
api_key = Sys.getenv("OPENAI_API_KEY"),
echo = "none"
)
openai_4_1_nano_agent <- Agent$new(
name = "openai_4_1_nano_agent",
instruction = "You are an AI assistant. Answer in 1 sentence max.",
llm_object = openai_4_1_nano
)
lead_agent <- LeadAgent$new(
name = "Leader",
llm_object = openai_4_1_mini
)
lead_agent$register_agents(c(openai_4_1_agent, openai_4_1_nano_agent))
lead_agent$broadcast(
prompt = paste0(
"If I were Algerian, which song would I like to sing ",
"when running under the rain? how about a flower?"
)
)
}
## ------------------------------------------------
## Method `LeadAgent$set_hitl`
## ------------------------------------------------
if (FALSE) {
# An API KEY is required in order to invoke the agents
openai_4_1_mini <- ellmer::chat(
name = "openai/gpt-4.1-mini",
api_key = Sys.getenv("OPENAI_API_KEY"),
echo = "none"
)
researcher <- Agent$new(
name = "researcher",
instruction = paste0(
"You are a research assistant. ",
"Your job is to answer factual questions with detailed and accurate information. ",
"Do not answer with more than 2 lines"
),
llm_object = openai_4_1_mini
)
summarizer <- Agent$new(
name = "summarizer",
instruction = paste0(
"You are agent designed to summarise a give text ",
"into 3 distinct bullet points."
),
llm_object = openai_4_1_mini
)
translator <- Agent$new(
name = "translator",
instruction = "Your role is to translate a text from English to German",
llm_object = openai_4_1_mini
)
lead_agent <- LeadAgent$new(
name = "Leader",
llm_object = openai_4_1_mini
)
lead_agent$register_agents(c(researcher, summarizer, translator))
# setting a human in the loop in step 2
lead_agent$set_hitl(1)
# The execution will stop at step 2 and a human will be able
# to either accept the answer, modify it or stop the execution of
# the workflow
lead_agent$invoke(
paste0(
"Describe the economic situation in Algeria in 3 sentences. ",
"Answer in German"
)
)
}
## ------------------------------------------------
## Method `LeadAgent$judge_and_choose_best_response`
## ------------------------------------------------
if (FALSE) {
openai_4_1_mini <- ellmer::chat(
name = "openai/gpt-4.1-mini",
api_key = Sys.getenv("OPENAI_API_KEY"),
echo = "none"
)
openai_4_1 <- ellmer::chat(
name = "openai/gpt-4.1",
api_key = Sys.getenv("OPENAI_API_KEY"),
echo = "none"
)
stylist <- Agent$new(
name = "stylist",
instruction = "You are an AI assistant. Answer in 1 sentence max.",
llm_object = openai_4_1
)
openai_4_1_nano <- ellmer::chat(
name = "openai/gpt-4.1-nano",
api_key = Sys.getenv("OPENAI_API_KEY"),
echo = "none"
)
stylist2 <- Agent$new(
name = "stylist2",
instruction = "You are an AI assistant. Answer in 1 sentence max.",
llm_object = openai_4_1_nano
)
lead_agent <- LeadAgent$new(
name = "Leader",
llm_object = openai_4_1_mini
)
lead_agent$register_agents(c(stylist, stylist2))
lead_agent$judge_and_choose_best_response("what's the best way to war a kalvin klein shirt?")
}
Run the code above in your browser using DataLab