|
7 | 7 | #include <sstream> |
8 | 8 | #include <string> |
9 | 9 | #include <vector> |
10 | | - |
| 10 | +#include "utils/format_utils.h" |
11 | 11 | namespace config { |
12 | 12 | struct ModelConfig { |
13 | 13 | std::string name; |
@@ -58,7 +58,7 @@ struct ModelConfig { |
58 | 58 | int n_probs = 0; |
59 | 59 | int min_keep = 0; |
60 | 60 | std::string grammar; |
61 | | - |
| 61 | + |
62 | 62 | void FromJson(const Json::Value& json) { |
63 | 63 | // do now allow to update ID and model field because it is unique identifier |
64 | 64 | // if (json.isMember("id")) |
@@ -236,116 +236,90 @@ struct ModelConfig { |
236 | 236 | std::string ToString() const { |
237 | 237 | std::ostringstream oss; |
238 | 238 |
|
239 | | - // Color codes |
240 | | - const std::string RESET = "\033[0m"; |
241 | | - const std::string BOLD = "\033[1m"; |
242 | | - const std::string GREEN = "\033[1;32m"; |
243 | | - const std::string YELLOW = "\033[0;33m"; |
244 | | - const std::string BLUE = "\033[0;34m"; |
245 | | - const std::string MAGENTA = "\033[0;35m"; |
246 | | - const std::string GRAY = "\033[1;90m"; |
247 | | - |
248 | | - // Helper function to print comments |
249 | | - auto print_comment = [&oss, &GRAY, &RESET](const std::string& comment) { |
250 | | - oss << GRAY << "# " << comment << RESET << "\n"; |
251 | | - }; |
252 | | - |
253 | | - // Helper function to print key-value pairs |
254 | | - auto print_kv = [&oss, &GREEN, &RESET]( |
255 | | - const std::string& key, const auto& value, |
256 | | - const std::string& color = "\033[0m") { |
257 | | - oss << GREEN << key << ":" << RESET << " " << color << value << RESET |
258 | | - << "\n"; |
259 | | - }; |
260 | | - |
261 | | - // Helper function to print boolean values |
262 | | - auto print_bool = [&print_kv, &MAGENTA](const std::string& key, |
263 | | - bool value) { |
264 | | - print_kv(key, value ? "true" : "false", MAGENTA); |
265 | | - }; |
266 | | - |
267 | | - // Helper function to print float values with fixed precision |
268 | | - auto print_float = [&print_kv, &BLUE](const std::string& key, float value) { |
269 | | - if (!std::isnan(value)) { |
270 | | - std::ostringstream float_oss; |
271 | | - float_oss << std::fixed << std::setprecision(9) << value; |
272 | | - print_kv(key, float_oss.str(), BLUE); |
273 | | - } |
274 | | - }; |
275 | | - |
276 | | - print_comment("BEGIN GENERAL GGUF METADATA"); |
| 239 | + oss << format_utils::print_comment("BEGIN GENERAL GGUF METADATA"); |
277 | 240 | if (!id.empty()) |
278 | | - print_kv("id", id, YELLOW); |
| 241 | + oss << format_utils::print_kv("id", id, format_utils::YELLOW); |
279 | 242 | if (!name.empty()) |
280 | | - print_kv("name", name, YELLOW); |
| 243 | + oss << format_utils::print_kv("name", name, format_utils::YELLOW); |
281 | 244 | if (!model.empty()) |
282 | | - print_kv("model", model, YELLOW); |
| 245 | + oss << format_utils::print_kv("model", model, format_utils::YELLOW); |
283 | 246 | if (!version.empty()) |
284 | | - print_kv("version", version, YELLOW); |
| 247 | + oss << format_utils::print_kv("version", version, format_utils::YELLOW); |
285 | 248 | if (!files.empty()) { |
286 | | - oss << GREEN << "files:" << RESET << "\n"; |
| 249 | + oss << format_utils::GREEN << "files:" << format_utils::RESET << "\n"; |
287 | 250 | for (const auto& file : files) { |
288 | | - oss << " - " << YELLOW << file << RESET << "\n"; |
| 251 | + oss << " - " << format_utils::YELLOW << file << format_utils::RESET |
| 252 | + << "\n"; |
289 | 253 | } |
290 | 254 | } |
291 | | - print_comment("END GENERAL GGUF METADATA"); |
| 255 | + oss << format_utils::print_comment("END GENERAL GGUF METADATA"); |
292 | 256 |
|
293 | | - print_comment("BEGIN INFERENCE PARAMETERS"); |
294 | | - print_comment("BEGIN REQUIRED"); |
| 257 | + oss << format_utils::print_comment("BEGIN INFERENCE PARAMETERS"); |
| 258 | + oss << format_utils::print_comment("BEGIN REQUIRED"); |
295 | 259 | if (!stop.empty()) { |
296 | | - oss << GREEN << "stop:" << RESET << "\n"; |
| 260 | + oss << format_utils::GREEN << "stop:" << format_utils::RESET << "\n"; |
297 | 261 | for (const auto& s : stop) { |
298 | | - oss << " - " << YELLOW << s << RESET << "\n"; |
| 262 | + oss << " - " << format_utils::YELLOW << s << format_utils::RESET |
| 263 | + << "\n"; |
299 | 264 | } |
300 | 265 | } |
301 | | - print_comment("END REQUIRED"); |
302 | | - print_comment("BEGIN OPTIONAL"); |
| 266 | + oss << format_utils::print_comment("END REQUIRED"); |
| 267 | + oss << format_utils::print_comment("BEGIN OPTIONAL"); |
303 | 268 |
|
304 | | - print_bool("stream", stream); |
305 | | - print_float("top_p", top_p); |
306 | | - print_float("temperature", temperature); |
307 | | - print_float("frequency_penalty", frequency_penalty); |
308 | | - print_float("presence_penalty", presence_penalty); |
| 269 | + oss << format_utils::print_bool("stream", stream); |
| 270 | + oss << format_utils::print_float("top_p", top_p); |
| 271 | + oss << format_utils::print_float("temperature", temperature); |
| 272 | + oss << format_utils::print_float("frequency_penalty", frequency_penalty); |
| 273 | + oss << format_utils::print_float("presence_penalty", presence_penalty); |
309 | 274 | if (max_tokens != std::numeric_limits<int>::quiet_NaN()) |
310 | | - print_kv("max_tokens", max_tokens, MAGENTA); |
| 275 | + oss << format_utils::print_kv("max_tokens", std::to_string(max_tokens), |
| 276 | + format_utils::MAGENTA); |
311 | 277 | if (seed != -1) |
312 | | - print_kv("seed", seed, MAGENTA); |
313 | | - print_float("dynatemp_range", dynatemp_range); |
314 | | - print_float("dynatemp_exponent", dynatemp_exponent); |
315 | | - print_kv("top_k", top_k, MAGENTA); |
316 | | - print_float("min_p", min_p); |
317 | | - print_kv("tfs_z", tfs_z, MAGENTA); |
318 | | - print_float("typ_p", typ_p); |
319 | | - print_kv("repeat_last_n", repeat_last_n, MAGENTA); |
320 | | - print_float("repeat_penalty", repeat_penalty); |
321 | | - print_bool("mirostat", mirostat); |
322 | | - print_float("mirostat_tau", mirostat_tau); |
323 | | - print_float("mirostat_eta", mirostat_eta); |
324 | | - print_bool("penalize_nl", penalize_nl); |
325 | | - print_bool("ignore_eos", ignore_eos); |
326 | | - print_kv("n_probs", n_probs, MAGENTA); |
327 | | - print_kv("min_keep", min_keep, MAGENTA); |
| 278 | + oss << format_utils::print_kv("seed", std::to_string(seed), |
| 279 | + format_utils::MAGENTA); |
| 280 | + oss << format_utils::print_float("dynatemp_range", dynatemp_range); |
| 281 | + oss << format_utils::print_float("dynatemp_exponent", dynatemp_exponent); |
| 282 | + oss << format_utils::print_kv("top_k", std::to_string(top_k), |
| 283 | + format_utils::MAGENTA); |
| 284 | + oss << format_utils::print_float("min_p", min_p); |
| 285 | + oss << format_utils::print_float("tfs_z", tfs_z); |
| 286 | + oss << format_utils::print_float("typ_p", typ_p); |
| 287 | + oss << format_utils::print_kv( |
| 288 | + "repeat_last_n", std::to_string(repeat_last_n), format_utils::MAGENTA); |
| 289 | + oss << format_utils::print_float("repeat_penalty", repeat_penalty); |
| 290 | + oss << format_utils::print_bool("mirostat", mirostat); |
| 291 | + oss << format_utils::print_float("mirostat_tau", mirostat_tau); |
| 292 | + oss << format_utils::print_float("mirostat_eta", mirostat_eta); |
| 293 | + oss << format_utils::print_bool("penalize_nl", penalize_nl); |
| 294 | + oss << format_utils::print_bool("ignore_eos", ignore_eos); |
| 295 | + oss << format_utils::print_kv("n_probs", std::to_string(n_probs), |
| 296 | + format_utils::MAGENTA); |
| 297 | + oss << format_utils::print_kv("min_keep", std::to_string(min_keep), |
| 298 | + format_utils::MAGENTA); |
328 | 299 |
|
329 | | - print_comment("END OPTIONAL"); |
330 | | - print_comment("END INFERENCE PARAMETERS"); |
331 | | - print_comment("BEGIN MODEL LOAD PARAMETERS"); |
332 | | - print_comment("BEGIN REQUIRED"); |
| 300 | + oss << format_utils::print_comment("END OPTIONAL"); |
| 301 | + oss << format_utils::print_comment("END INFERENCE PARAMETERS"); |
| 302 | + oss << format_utils::print_comment("BEGIN MODEL LOAD PARAMETERS"); |
| 303 | + oss << format_utils::print_comment("BEGIN REQUIRED"); |
333 | 304 |
|
334 | 305 | if (!engine.empty()) |
335 | | - print_kv("engine", engine, YELLOW); |
| 306 | + oss << format_utils::print_kv("engine", engine, format_utils::YELLOW); |
336 | 307 | if (!prompt_template.empty()) |
337 | | - print_kv("prompt_template", prompt_template, YELLOW); |
| 308 | + oss << format_utils::print_kv("prompt_template", prompt_template, |
| 309 | + format_utils::YELLOW); |
338 | 310 |
|
339 | | - print_comment("END REQUIRED"); |
340 | | - print_comment("BEGIN OPTIONAL"); |
| 311 | + oss << format_utils::print_comment("END REQUIRED"); |
| 312 | + oss << format_utils::print_comment("BEGIN OPTIONAL"); |
341 | 313 |
|
342 | 314 | if (ctx_len != std::numeric_limits<int>::quiet_NaN()) |
343 | | - print_kv("ctx_len", ctx_len, MAGENTA); |
| 315 | + oss << format_utils::print_kv("ctx_len", std::to_string(ctx_len), |
| 316 | + format_utils::MAGENTA); |
344 | 317 | if (ngl != std::numeric_limits<int>::quiet_NaN()) |
345 | | - print_kv("ngl", ngl, MAGENTA); |
| 318 | + oss << format_utils::print_kv("ngl", std::to_string(ngl), |
| 319 | + format_utils::MAGENTA); |
346 | 320 |
|
347 | | - print_comment("END OPTIONAL"); |
348 | | - print_comment("END MODEL LOAD PARAMETERS"); |
| 321 | + oss << format_utils::print_comment("END OPTIONAL"); |
| 322 | + oss << format_utils::print_comment("END MODEL LOAD PARAMETERS"); |
349 | 323 |
|
350 | 324 | return oss.str(); |
351 | 325 | } |
|
0 commit comments