Agents 0.0.2
Edge AI Agents SDK
Loading...
Searching...
No Matches
orchestrator_example.cpp

Orchestrator Example.

Orchestrator Example

Version
0.1
Date
2025-07-20
#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>
using namespace agents;
int main(int argc, char* argv[]) {
// Get API key from .env, environment, or command line
String api_key;
auto& config = ConfigLoader::getInstance();
// Try to get API key from config or environment
api_key = config.get("GEMINI_API_KEY", "");
// If not found, check command line
if (api_key.empty() && argc > 1) {
api_key = argv[1];
}
// Still not found, show error and exit
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;
}
// Create LLM
auto llm = createLLM("google", api_key, "gemini-1.5-flash");
// Configure LLM options
LLMOptions options;
options.temperature = 0.3;
options.max_tokens = 2048;
llm->setOptions(options);
// Create agent context
auto context = std::make_shared<AgentContext>();
context->setLLM(llm);
// Register tools
context->registerTool(tools::createWebSearchTool());
context->registerTool(tools::createWikipediaTool());
// Create orchestrator-workers workflow
workflows::OrchestratorWorkflow orchestrator(context);
// Set orchestrator prompt
orchestrator.setOrchestratorPrompt(
"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."
);
// Register workers
orchestrator.addWorker(
"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."
);
orchestrator.addWorker(
"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."
);
orchestrator.addWorker(
"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."
);
orchestrator.addWorker(
"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."
);
orchestrator.addWorker(
"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."
);
// Set custom synthesizer
orchestrator.setSynthesizer([](const std::vector<JsonObject>& worker_results) -> JsonObject {
JsonObject final_result;
String combined_output = "# Comprehensive Report\n\n";
// Extract each worker's contribution
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";
}
}
// Add a summary section
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;
});
// Process user inputs until exit
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;
// Run the orchestrator-workers workflow
JsonObject result = orchestrator.run(user_input);
// Display the result
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.
std::shared_ptr< Tool > createWikipediaTool()
Creates a tool for retrieving information from Wikipedia.
std::shared_ptr< Tool > createWebSearchTool()
Creates a tool for performing web searches.
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