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

Simple Chatbot Example.

Simple Chatbot Example

Version
0.1
Date
2025-07-20
#include <agents-cpp/agents/actor_agent.h>
#include <agents-cpp/config_loader.h>
#include <agents-cpp/logger.h>
#include <agents-cpp/tools/tool_registry.h>
#include <iostream>
using namespace agents;
// Main agent task
Task<int> runAgentApp(int argc, char* argv[]) {
// Initialize the logger
// 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()) {
Logger::error("API key not found. Please:");
Logger::error("1. Create a .env file with GEMINI_API_KEY=your_key, or");
Logger::error("2. Set the GEMINI_API_KEY environment variable, or");
Logger::error("3. Provide an API key as a command line argument");
co_return EXIT_FAILURE;
}
// Create the context
auto context = std::make_shared<AgentContext>();
// Configure the LLM
auto llm = createLLM("google", api_key, "gemini-2.0-flash");
// Configure LLM options
LLMOptions options;
options.temperature = (0.7);
llm->setOptions(options);
context->setLLM(llm);
// Register some tools
context->registerTool(tools::createWebSearchTool());
context->registerTool(tools::createWikipediaTool());
// Create the agent
ActorAgent agent(context);
// Personalize the actor agent's character
"You are a friendly 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."
);
// Set up callbacks
agent.setStatusCallback([](const String& status) {
Logger::info("Agent status: {}", status);
});
// Initialize the agent
agent.init();
// Get user input
Logger::info("Enter a question or task for the agent (or 'exit' to quit):");
String user_input;
while (true) {
Logger::info("> ");
std::getline(std::cin, user_input);
if (user_input == "exit" || user_input == "quit" || user_input == "q") {
break;
}
if (user_input.empty()) {
continue;
}
try {
// Run the agent with coroutines
JsonObject result = co_await agent.run(user_input);
// Display the final result
Logger::info("\nFinal Result:\n{}", result["answer"].get<String>());
} catch (const std::exception& e) {
Logger::error("Error: {}", e.what());
}
}
co_return EXIT_SUCCESS;
}
// Entry point
int main(int argc, char* argv[]) {
// Use blockingWait to execute the coroutine
return blockingWait(runAgentApp(argc, argv));
}
Actor-based agent implementation.
Definition actor_agent.h:31
void setAgentPrompt(const String &agent_prompt)
Set the agent prompt for the agent.
Task< JsonObject > run(const String &task) override
Run the agent with a task using coroutines.
void init() override
Initialize the agent.
void setStatusCallback(std::function< void(const String &)> callback)
Set a callback for status updates.
static ConfigLoader & getInstance()
Get the singleton instance of ConfigLoader.
@ INFO
Info logging level.
Definition logger.h:40
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
Standard C++20 coroutine-based Task implementation (no external deps)
Definition coroutine_utils.h:38
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
nlohmann::json JsonObject
JSON object type.
Definition types.h:39
std::shared_ptr< LLMInterface > createLLM(const String &provider, const String &api_key, const String &model="")
Factory function to create a specific LLM provider.
std::string String
String type.
Definition types.h:27
T blockingWait(Task< T > &&task)
Helper to run a coroutine and get the result synchronously.
Definition coroutine_utils.h:496
Options for LLM API calls.
Definition llm_interface.h:25
double temperature
The temperature of the LLM.
Definition llm_interface.h:29