Evaluator Optimizer 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/evaluator_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;
}
auto llm = createLLM("google", api_key, "gemini-1.5-flash");
llm->setOptions(options);
auto context = std::make_shared<AgentContext>();
context->setLLM(llm);
"You are an optimizer assistant that produces high-quality responses to user queries. "
"Your task is to generate the best possible response to the user's query. "
"If you receive feedback, use it to improve your response."
);
"You are an evaluator assistant that critically assesses the quality of responses. "
"Your task is to provide honest, detailed feedback on the response to help improve it. "
"Focus on specific areas where the response could be enhanced."
);
"Accuracy: Is the information provided accurate and factually correct?",
"Completeness: Does the response address all aspects of the query?",
"Clarity: Is the response clear, well-organized, and easy to understand?",
"Relevance: Is the response directly relevant to the query?",
"Actionability: Does the response provide practical, actionable information where appropriate?"
});
workflow.
setEvaluator([](
const String& input,
const String& output) -> JsonObject {
return JsonObject();
});
workflow.
setOptimizer([](
const String& input,
const JsonObject& feedback) -> String {
return "";
});
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") {
break;
}
if (user_input.empty()) {
continue;
}
try {
std::cout << "Starting evaluator-optimizer workflow..." << std::endl;
JsonObject result = workflow.
run(user_input);
std::cout << "\nFinal Response:" << std::endl;
std::cout << result["final_response"].get<String>() << std::endl;
std::cout << "\nEvaluation Information:" << std::endl;
std::cout << "Iterations: " << result["iterations"].get<int>() << std::endl;
std::cout << "Final Score: " << result["final_score"].get<double>() << std::endl;
if (result.contains("evaluations")) {
std::cout << "\nEvaluation History:" << std::endl;
for (const auto& eval : result["evaluations"]) {
std::cout << "Iteration " << eval["iteration"].get<int>() << ": Score = "
<< eval["score"].get<double>() << std::endl;
std::cout << "Feedback: " << eval["feedback"].get<String>() << std::endl;
std::cout << "----------" << 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.
An evaluator-optimizer workflow where one LLM optimizes output based on another's feedback.
Definition evaluator_workflow.h:27
void setEvaluator(std::function< JsonObject(const String &, const String &)> evaluator)
Set the evaluator function.
void setOptimizerPrompt(const String &prompt_template)
Set the optimizer prompt.
Definition evaluator_workflow.h:87
void setOptimizer(std::function< String(const String &, const JsonObject &)> optimizer)
Set the optimizer function.
JsonObject run(const String &input) override
Execute the workflow with input.
void setMinimumAcceptableScore(double threshold)
Set the minimum acceptable score.
Definition evaluator_workflow.h:75
void setEvaluatorPrompt(const String &prompt_template)
Set the evaluator prompt.
Definition evaluator_workflow.h:99
void setEvaluationCriteria(const std::vector< String > &criteria)
Set the evaluation criteria for the evaluator.
void setMaxIterations(int max_iterations)
Set the max number of feedback iterations.
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