Parallel Example.
#include <agents-cpp/agent_context.h>
#include <agents-cpp/config_loader.h>
#include <agents-cpp/llm_interface.h>
#include <agents-cpp/types.h>
#include <agents-cpp/workflows/parallelization_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;
}
std::cout << "Select parallelization mode (1 for SECTIONING, 2 for VOTING): ";
int mode_choice;
std::cin >> mode_choice;
std::cin.ignore();
auto llm = createLLM("google", api_key, "gemini-1.5-flash");
llm->setOptions(options);
auto context = std::make_shared<AgentContext>();
context->setLLM(llm);
"research",
"You are a research assistant focused on gathering factual information. "
"Present only verified facts and data, citing sources when possible.",
[](const String& input) -> String {
return "Research task: " + input +
"\nFocus on finding the most relevant facts and data points about this topic.";
},
[](const String& output) -> JsonObject {
JsonObject result;
result["research"] = output;
return result;
}
);
"analysis",
"You are an analytical assistant that excels at critical thinking. "
"Analyze information objectively, identifying patterns, trends, and insights.",
[](const String& input) -> String {
return "Analysis task: " + input +
"\nProvide a thoughtful analysis, including implications and significance.";
},
[](const String& output) -> JsonObject {
JsonObject result;
result["analysis"] = output;
return result;
}
);
"recommendations",
"You are a recommendation assistant that provides practical advice. "
"Suggest actionable steps based on the query.",
[](const String& input) -> String {
return "Recommendation task: " + input +
"\nProvide concrete, actionable recommendations related to this topic.";
},
[](const String& output) -> JsonObject {
JsonObject result;
result["recommendations"] = output;
return result;
}
);
parallel.
setAggregator([](
const std::vector<JsonObject>& results) -> JsonObject {
JsonObject combined;
String research, analysis, recommendations;
for (const auto& result : results) {
if (result.contains("research")) {
research = result["research"].get<String>();
} else if (result.contains("analysis")) {
analysis = result["analysis"].get<String>();
} else if (result.contains("recommendations")) {
recommendations = result["recommendations"].get<String>();
}
}
combined["answer"] =
"# Research Findings\n\n" + research +
"\n\n# Analysis\n\n" + analysis +
"\n\n# Recommendations\n\n" + recommendations;
return combined;
});
} else {
for (int i = 0; i < 5; i++) {
"agent_" + std::to_string(i+1),
"You are assistant " + std::to_string(i+1) + ". "
"Provide your best answer to the query, thinking independently.",
[i](const String& input) -> String {
return "Task for agent " + std::to_string(i+1) + ": " + input;
},
[](const String& output) -> JsonObject {
JsonObject result;
result["response"] = output;
return result;
}
);
}
}
std::cout << "Enter queries (or 'exit' to quit):" << std::endl;
String user_input;
while (true) {
std::cout << "> ";
std::getline(std::cin, user_input);
if (user_input == "exit" || user_input == "quit" || user_input == "q") {
break;
}
if (user_input.empty()) {
continue;
}
try {
std::cout << "Running parallel tasks..." << std::endl;
JsonObject result = parallel.
run(user_input);
std::cout << "\nResult:\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 that runs multiple tasks in parallel.
Definition parallelization_workflow.h:27
void addTask(const Task &task)
Add a task to the workflow.
void setAggregator(std::function< JsonObject(const std::vector< JsonObject > &)> aggregator)
Set the aggregation function for task results.
JsonObject run(const String &input) override
Execute the workflow with input.
Strategy
Enum for parallelization strategy.
Definition parallelization_workflow.h:33
@ VOTING
Run same task multiple times for consensus.
Definition parallelization_workflow.h:41
@ SECTIONING
Break task into independent subtasks.
Definition parallelization_workflow.h:37
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