Prompt Chain Example.
#include <agents-cpp/agent_context.h>
#include <agents-cpp/config_loader.h>
#include <agents-cpp/llm_interface.h>
#include <agents-cpp/logger.h>
#include <agents-cpp/types.h>
#include <agents-cpp/workflows/prompt_chaining_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()) {
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");
return 1;
}
auto llm = createLLM("google", api_key, "gemini-2.0-flash");
llm->setOptions(options);
auto context = std::make_shared<AgentContext>();
context->setLLM(llm);
"outline",
"You are an expert document planner. Your task is to create a detailed outline for a document about '{input}'. "
"The outline should include main sections and subsections. Be comprehensive but focused."
);
"validate_outline",
"You are a document validator. Your task is to evaluate an outline and determine if it's comprehensive and well-structured. "
"Check if it covers all important aspects of the topic and has a logical flow. Outline: {context}",
[](const JsonObject& result) -> bool {
String response = result["response"].get<String>();
return response.find("approved") != String::npos ||
response.find("looks good") != String::npos ||
response.find("comprehensive") != String::npos;
}
);
"write_document",
"You are an expert content writer. Your task is to write a comprehensive document following the provided outline: {context}. "
"Make sure to cover each section in detail and maintain a professional tone."
);
"proofread",
"You are a professional editor. Your task is to proofread and improve the provided document: {context}. "
"Fix any grammatical errors, improve clarity and flow, and ensure consistency."
);
chain.
setStepCallback([](
const String& step_name,
const JsonObject& result) {
});
String user_input;
std::getline(std::cin, user_input);
try {
JsonObject result = chain.
run(user_input);
if (result.contains("proofread") && result["proofread"].contains("response")) {
Logger::info(
"\nFinal Document:\n{}", result[
"proofread"][
"response"].get<String>());
} else if (result.contains("response")) {
Logger::info(
"\nFinal Document:\n{}", result[
"response"].get<String>());
} else {
}
} catch (const std::exception& e) {
}
return 0;
}
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
A workflow that chains multiple prompts together.
Definition prompt_chaining_workflow.h:26
void addStep(const Step &step)
Add a step to the workflow.
JsonObject run(const String &input) override
Execute the workflow with input.
@ INFO
Info logging level.
Definition logger.h:40
void setStepCallback(std::function< void(const String &, const JsonObject &)> callback)
Set a callback for intermediate steps.
Worflows Namespace.
Definition workflow.h:22
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