feat(clients): optimize: evaluation, simulation, observability and optimizer on a discovery-checked call table
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
#include "google/optimize/clusterer.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
|
||||
#include "google/errors.h"
|
||||
#include "google/optimize/caps.h"
|
||||
|
||||
namespace molo::google::optimize {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
namespace {
|
||||
bool valid_id(const std::string& id) {
|
||||
if (id.empty() || id.size() > 64) return false;
|
||||
for (char c : id)
|
||||
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
|
||||
(c >= '0' && c <= '9') || c == '-' || c == '_'))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
std::string clip(std::string s, std::size_t n) {
|
||||
if (s.size() > n) s.resize(n);
|
||||
return s;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Json ClusterReport::to_json() const {
|
||||
Json out = Json::object();
|
||||
Json arr = Json::array();
|
||||
for (const FailureCluster& c : clusters) {
|
||||
Json o = Json::object();
|
||||
o.set("label", Json(c.label));
|
||||
o.set("description", Json(c.description));
|
||||
o.set("count", Json(static_cast<int>(c.failure_ids.size())));
|
||||
Json ids = Json::array();
|
||||
for (const std::string& id : c.failure_ids) ids.push(Json(id));
|
||||
o.set("failure_ids", ids);
|
||||
arr.push(o);
|
||||
}
|
||||
out.set("clusters", arr);
|
||||
Json un = Json::array();
|
||||
for (const std::string& id : unassigned) un.push(Json(id));
|
||||
out.set("unassigned", un);
|
||||
out.set("dropped", Json(static_cast<int>(dropped)));
|
||||
return out;
|
||||
}
|
||||
|
||||
FailureClusterer::FailureClusterer(std::shared_ptr<LanguageModel> model,
|
||||
std::string model_name)
|
||||
: model_(std::move(model), std::move(model_name)) {}
|
||||
|
||||
ClusterReport FailureClusterer::cluster(const std::vector<Failure>& failures,
|
||||
std::size_t max_clusters) const {
|
||||
if (failures.empty()) throw GoogleError::invalid("no failures to cluster");
|
||||
if (max_clusters < 1 || max_clusters > OptimizeCaps::kMaxClusters)
|
||||
throw GoogleError::invalid("max_clusters must be 1.." +
|
||||
std::to_string(OptimizeCaps::kMaxClusters));
|
||||
ClusterReport report;
|
||||
std::vector<const Failure*> used;
|
||||
std::set<std::string> ids;
|
||||
for (const Failure& f : failures) {
|
||||
if (!valid_id(f.id)) throw GoogleError::invalid("bad failure id");
|
||||
if (!ids.insert(f.id).second) throw GoogleError::invalid("duplicate failure id");
|
||||
if (f.summary.empty()) throw GoogleError::invalid("failure summary is empty");
|
||||
if (f.summary.size() > OptimizeCaps::kMaxTextChars ||
|
||||
f.excerpt.size() > OptimizeCaps::kMaxTextChars)
|
||||
throw GoogleError::invalid("failure text over the character cap");
|
||||
if (used.size() < OptimizeCaps::kMaxFailures) used.push_back(&f);
|
||||
else ++report.dropped;
|
||||
}
|
||||
|
||||
std::string prompt = "Failures:\n";
|
||||
for (const Failure* f : used) {
|
||||
prompt += "[" + f->id + "] " + clip(f->summary, 400);
|
||||
if (!f->excerpt.empty()) prompt += " | " + clip(f->excerpt, 300);
|
||||
prompt += "\n";
|
||||
}
|
||||
std::string system =
|
||||
"Group these agent failures into at most " + std::to_string(max_clusters) +
|
||||
" clusters by root cause. Reply with one JSON object: "
|
||||
"{\"clusters\":[{\"label\":\"<3-6 words>\",\"description\":\"<one sentence>\","
|
||||
"\"failure_ids\":[\"<id>\",...]}]}. Use each id at most once.";
|
||||
Json reply = model_.ask(system, prompt);
|
||||
|
||||
std::set<std::string> offered;
|
||||
for (const Failure* f : used) offered.insert(f->id);
|
||||
std::set<std::string> placed;
|
||||
const Json* list = reply.find("clusters");
|
||||
if (list && list->is_array()) {
|
||||
for (const Json& c : list->as_array()) {
|
||||
if (!c.is_object()) continue;
|
||||
FailureCluster fc;
|
||||
if (const Json* v = c.find("label"); v && v->is_string()) fc.label = clip(v->as_string(), 120);
|
||||
if (const Json* v = c.find("description"); v && v->is_string())
|
||||
fc.description = clip(v->as_string(), 500);
|
||||
if (fc.label.empty()) fc.label = "unlabeled";
|
||||
if (const Json* v = c.find("failure_ids"); v && v->is_array())
|
||||
for (const Json& id : v->as_array())
|
||||
if (id.is_string() && offered.count(id.as_string()) &&
|
||||
placed.insert(id.as_string()).second)
|
||||
fc.failure_ids.push_back(id.as_string());
|
||||
if (!fc.failure_ids.empty()) report.clusters.push_back(std::move(fc));
|
||||
}
|
||||
}
|
||||
std::stable_sort(report.clusters.begin(), report.clusters.end(),
|
||||
[](const FailureCluster& a, const FailureCluster& b) {
|
||||
return a.failure_ids.size() > b.failure_ids.size();
|
||||
});
|
||||
while (report.clusters.size() > max_clusters) {
|
||||
for (const std::string& id : report.clusters.back().failure_ids) placed.erase(id);
|
||||
report.clusters.pop_back();
|
||||
}
|
||||
for (const Failure* f : used)
|
||||
if (!placed.count(f->id)) report.unassigned.push_back(f->id);
|
||||
return report;
|
||||
}
|
||||
|
||||
} // namespace molo::google::optimize
|
||||
Reference in New Issue
Block a user