Entropic 2.3.8
Local-first agentic inference engine
Loading...
Searching...
No Matches
classification.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
10#include <algorithm>
11#include <cctype>
12
13namespace entropic::prompts {
14
15namespace {
16auto logger = entropic::log::get("prompts.classification");
17} // anonymous namespace
18
26static std::string to_upper(const std::string& s)
27{
28 std::string result = s;
29 std::transform(result.begin(), result.end(), result.begin(),
30 [](unsigned char c) { return std::toupper(c); });
31 return result;
32}
33
41std::vector<std::string> interleave_examples(
42 const std::vector<TierDescriptor>& tiers)
43{
44 struct Queue {
45 int digit;
46 const std::vector<std::string>* examples;
47 };
48
49 std::vector<Queue> queues;
50 for (size_t i = 0; i < tiers.size(); ++i) {
51 if (!tiers[i].examples.empty()) {
52 queues.push_back(
53 {static_cast<int>(i + 1), &tiers[i].examples});
54 }
55 }
56
57 std::vector<std::string> lines;
58 size_t round_idx = 0;
59 while (true) {
60 bool added = false;
61 for (const auto& q : queues) {
62 if (round_idx < q.examples->size()) {
63 lines.push_back("\"" + (*q.examples)[round_idx]
64 + "\" -> "
65 + std::to_string(q.digit));
66 added = true;
67 }
68 }
69 if (!added) {
70 break;
71 }
72 ++round_idx;
73 }
74 return lines;
75}
76
85static std::string join(
86 const std::vector<std::string>& items, const std::string& sep)
87{
88 std::string result;
89 for (size_t i = 0; i < items.size(); ++i) {
90 if (i > 0) result += sep;
91 result += items[i];
92 }
93 return result;
94}
95
103static std::string format_tier_definitions(
104 const std::vector<TierDescriptor>& tiers)
105{
106 std::string result;
107 for (size_t i = 0; i < tiers.size(); ++i) {
108 result += std::to_string(i + 1) + " = "
109 + to_upper(tiers[i].name) + ": "
110 + join(tiers[i].focus, ", ") + "\n";
111 }
112 return result;
113}
114
126 const std::vector<TierDescriptor>& tiers,
127 const std::string& message,
128 const std::vector<std::string>& history,
129 const std::vector<std::string>& recent_tiers)
130{
131 std::string result = "Classify the message. Reply with the number only.\n\n";
132 result += format_tier_definitions(tiers) + "\n";
133
134 if (!recent_tiers.empty()) {
135 result += "Recent tiers: " + join(recent_tiers, " -> ") + "\n\n";
136 }
137
138 if (!history.empty()) {
139 size_t start = history.size() > 5 ? history.size() - 5 : 0;
140 std::vector<std::string> recent(history.begin() + static_cast<long>(start), history.end());
141 result += "Recent messages: " + join(recent, " | ") + "\n\n";
142 }
143
144 for (const auto& line : interleave_examples(tiers)) {
145 result += line + "\n";
146 }
147
148 result += "\"" + message + "\" -> ";
149 logger->info("Classification prompt: {} tiers, {} history, "
150 "{} chars",
151 tiers.size(), history.size(), result.size());
152 return result;
153}
154
155} // namespace entropic::prompts
static std::string to_upper(const std::string &s)
Convert string to uppercase.
static std::string join(const std::vector< std::string > &items, const std::string &sep)
Join strings with a separator.
static std::string format_tier_definitions(const std::vector< TierDescriptor > &tiers)
Build tier definition lines for classification prompt.
Auto-generated classification prompt for the router model.
ENTROPIC_EXPORT std::vector< std::string > interleave_examples(const std::vector< TierDescriptor > &tiers)
Round-robin interleave few-shot examples across tiers.
ENTROPIC_EXPORT std::string build_classification_prompt(const std::vector< TierDescriptor > &tiers, const std::string &message, const std::vector< std::string > &history={}, const std::vector< std::string > &recent_tiers={})
Auto-generate classification prompt from tier focus + examples.
spdlog initialization and logger access.
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > get(const std::string &name)
Get or create a named logger.
Definition logging.cpp:211