Orchestrator Example.
#include <agents-cpp/agent_context.h>
#include <agents-cpp/config_loader.h>
#include <agents-cpp/llm_interface.h>
#include <agents-cpp/tools/tool_registry.h>
#include <agents-cpp/types.h>
#include <agents-cpp/workflows/orchestrator_workflow.h>
#include <iostream>
#include <string>
int main(int argc, char* argv[]) {
String api_key;
api_key = config.get("GEMINI_API_KEY", "");
if (api_key.empty() && argc > 1) {
api_key = argv[1];
}
if (api_key.empty()) {
std::cout << ("API key not found. Please:") << std::endl;
std::cout << ("1. Create a .env file with GEMINI_API_KEY=your_key, or") << std::endl;
std::cout << ("2. Set the GEMINI_API_KEY environment variable, or") << std::endl;
std::cout << ("3. Provide an API key as a command line argument") << std::endl;
return 1;
}
auto llm = createLLM("google", api_key, "gemini-1.5-flash");
llm->setOptions(options);
auto context = std::make_shared<AgentContext>();
context->setLLM(llm);
"You are a project manager that breaks down complex tasks into subtasks and assigns them to appropriate specialist workers. "
"Analyze the user's request carefully, identify what specialists would be needed, and coordinate their work. "
"Provide a detailed plan for completing the task using the available workers."
);
"researcher",
"Gathers factual information and data on specific topics",
"You are a research specialist focused on gathering accurate, current, and relevant information. "
"Your task is to find the most important facts, data, statistics, and context on the given topic. "
"Cite sources when possible."
);
"analyst",
"Analyzes information, identifies patterns, and draws insights",
"You are an analytical specialist who excels at examining information critically. "
"Your task is to identify patterns, trends, insights, and implications from the research. "
"Focus on depth rather than breadth."
);
"writer",
"Creates well-written, cohesive content from information and analysis",
"You are a writing specialist who creates clear, engaging, and informative content. "
"Your task is to synthesize information and analysis into a cohesive narrative. "
"Focus on clarity, flow, and presentation."
);
"technical_expert",
"Provides specialized technical knowledge on complex topics",
"You are a technical specialist with deep expertise in technical domains. "
"Your task is to provide accurate technical explanations, clarifications, and context. "
"Make complex topics accessible without oversimplifying."
);
"critic",
"Reviews content for accuracy, clarity, and completeness",
"You are a critical reviewer who evaluates content objectively. "
"Your task is to identify gaps, inconsistencies, errors, or areas for improvement. "
"Provide constructive feedback rather than just criticism."
);
orchestrator.
setSynthesizer([](
const std::vector<JsonObject>& worker_results) -> JsonObject {
JsonObject final_result;
String combined_output = "# Comprehensive Report\n\n";
for (const auto& result : worker_results) {
if (result.contains("worker_name") && result.contains("output")) {
String worker_name = result["worker_name"].get<String>();
String output = result["output"].get<String>();
combined_output += "## " + worker_name + "'s Contribution\n\n";
combined_output += output + "\n\n";
}
}
combined_output += "## Summary\n\n";
combined_output += "This report combines the work of multiple specialists to provide a comprehensive response to the original query.";
final_result["answer"] = combined_output;
return final_result;
});
std::cout << "Enter complex tasks (or 'exit' to quit):" << std::endl;
String user_input;
while (true) {
std::cout << "> ";
std::getline(std::cin, user_input);
if (user_input == "exit") {
break;
}
if (user_input.empty()) {
continue;
}
try {
std::cout << "Orchestrating workers..." << std::endl;
JsonObject result = orchestrator.
run(user_input);
std::cout << "\nFinal Result:\n" << result["answer"].get<String>() << std::endl;
std::cout << "--------------------------------------" << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
return 0;
}
static ConfigLoader & getInstance()
Get the singleton instance of ConfigLoader.
A workflow where a central orchestrator delegates tasks to workers.
Definition orchestrator_workflow.h:28
void addWorker(const Worker &worker)
Add a worker to the workflow.
JsonObject run(const String &input) override
Execute the workflow with input.
void setOrchestratorPrompt(const String &orchestrator_prompt_template)
Set the orchestrator prompt template.
void setSynthesizer(std::function< JsonObject(const std::vector< JsonObject > &)> synthesizer)
Set the result synthesizer function.
Framework Namespace.
Definition agent.h:18
Options for LLM API calls.
Definition llm_interface.h:25
double temperature
The temperature of the LLM.
Definition llm_interface.h:29
int max_tokens
The maximum number of tokens.
Definition llm_interface.h:33