Streaming Chat With LLM Example.
#include <agents-cpp/config_loader.h>
#include <agents-cpp/logger.h>
#include <agents-cpp/tools/tool_registry.h>
#include <iostream>
Task<int> runStreamingChat(
int argc,
char* argv[]) {
api_key = config.get("GEMINI_API_KEY", "");
if (api_key.empty() && argc > 1) {
api_key = argv[1];
}
if (api_key.empty()) {
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;
}
auto context = std::make_shared<AgentContext>();
auto llm =
createLLM(
"google", api_key,
"gemini-2.0-flash");
llm->setOptions(options);
context->setLLM(llm);
Logger::info(
"Enter a question or task for the model (or 'exit' to quit):");
while (true) {
std::getline(std::cin, user_input);
if (user_input == "exit" || user_input == "quit" || user_input == "q") {
break;
}
if (user_input.empty()) {
continue;
}
try {
auto generator = context->streamChat(user_input);
while (auto item = co_await generator.next()) {
std::cout << chunk << std::flush;
}
std::cout << std::endl;
} catch (const std::exception& e) {
}
}
co_return EXIT_SUCCESS;
}
int main(int argc, char* argv[]) {
}
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
Framework Namespace.
Definition agent.h:18
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