#include <agents-cpp/agent_context.h>
#include <agents-cpp/agents/autonomous_agent.h>
#include <agents-cpp/config_loader.h>
#include <agents-cpp/coroutine_utils.h>
#include <agents-cpp/llm_interface.h>
#include <agents-cpp/logger.h>
#include <agents-cpp/tools/tool_registry.h>
#include <agents-cpp/types.h>
#include <iostream>
#include <chrono>
#include <string>
#include <thread>
} else {
}
}
bool humanApproval(const String& message, const JsonObject& context) {
char response;
std::cin >> response;
return (response == 'y' || response == 'Y');
}
Task<int> runAgentApp(
int argc,
char* argv[]) {
String api_key;
api_key = config.get("OPENAI_API_KEY", "");
if (api_key.empty() && argc > 1) {
api_key = argv[1];
}
if (api_key.empty()) {
Logger::error(
"1. Create a .env file with OPENAI_API_KEY=your_key, or");
Logger::error(
"2. Set the OPENAI_API_KEY environment variable, or");
Logger::error(
"3. Provide an API key as a command line argument");
co_return 1;
}
auto context = std::make_shared<AgentContext>();
auto llm = createLLM("openai", api_key, "gpt-4o");
llm->setOptions(options);
context->setLLM(llm);
agent.setSystemPrompt(
"You are a research assistant that helps users find information and answer questions. "
"Use the tools available to you to gather information and provide comprehensive answers. "
"When searching for information, try multiple queries if necessary."
);
agent.setOptions(agent_options);
agent.setStepCallback(stepCallback);
agent.setStatusCallback([](const String& status) {
});
agent.init();
Logger::info(
"Enter a question or task for the agent (or 'exit' to quit):");
String user_input;
while (true) {
std::getline(std::cin, user_input);
if (user_input == "exit") {
break;
}
if (user_input.empty()) {
continue;
}
try {
JsonObject result = co_await agent.run(user_input);
Logger::info(
"\nFinal Result:\n{}", result[
"answer"].get<String>());
} catch (const std::exception& e) {
}
}
co_return 0;
}
int main(int argc, char* argv[]) {
return blockingWait(runAgentApp(argc, argv));
}
static ConfigLoader & getInstance()
Get the singleton instance of ConfigLoader.
static void error(fmt::format_string< Args... > fmt, Args &&... args)
Log a message at error level.
Definition logger.h:124
static void init(Level level=Level::INFO)
Initialize the logger.
static void info(fmt::format_string< Args... > fmt, Args &&... args)
Log a message at info level.
Definition logger.h:104
An agent that operates autonomously to complete a task.
Definition autonomous_agent.h:28
@ REACT
Reasoning and acting.
Definition autonomous_agent.h:75
@ INFO
Info logging level.
Definition logger.h:40
Provide a future-based fallback for Task.
Definition coroutine_utils.h:115
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
Agent execution options.
Definition agent.h:61
bool human_feedback_enabled
Whether human feedback is enabled.
Definition agent.h:75
int max_iterations
The maximum number of iterations.
Definition agent.h:65
std::function< bool(const String &, const JsonObject &)> human_in_the_loop
The human in the loop function.
Definition agent.h:80
Step in the agent's execution.
Definition autonomous_agent.h:33
JsonObject result
The result of the step.
Definition autonomous_agent.h:45
bool success
Whether the step was successful.
Definition autonomous_agent.h:49
String description
The description of the step.
Definition autonomous_agent.h:37
String status
The status of the step.
Definition autonomous_agent.h:41