Skip to content

Commit 1e91256

Browse files
server: log prompts to directory (#22031)
* server: log prompts to directory Add `--log-prompts-dir` to write each prompt to a separate text file in the specified directory. * Apply suggestion from @ngxson --------- Co-authored-by: Xuan-Son Nguyen <thichthat@gmail.com>
1 parent efbacf8 commit 1e91256

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

common/arg.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3333,6 +3333,13 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
33333333
common_log_set_file(common_log_main(), value.c_str());
33343334
}
33353335
).set_env("LLAMA_ARG_LOG_FILE"));
3336+
add_opt(common_arg(
3337+
{"--log-prompts-dir"}, "PATH",
3338+
"Log prompts to directory (only used for debugging, default: disabled)",
3339+
[](common_params & params, const std::string & value) {
3340+
params.path_prompts_log_dir = value;
3341+
}
3342+
).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
33363343
add_opt(common_arg(
33373344
{"--log-colors"}, "[on|off|auto]",
33383345
"Set colored logging ('on', 'off', or 'auto', default: 'auto')\n"

common/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ struct common_params {
489489
std::string input_prefix = ""; // string to prefix user inputs with // NOLINT
490490
std::string input_suffix = ""; // string to suffix user inputs with // NOLINT
491491
std::string logits_file = ""; // file for saving *all* logits // NOLINT
492+
std::string path_prompts_log_dir = ""; // directory with logged prompts // NOLINT
492493

493494
// llama-debug specific options
494495
std::string logits_output_dir = "data"; // directory for saving logits output files // NOLINT

tools/server/server-context.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <memory>
2828
#include <filesystem>
2929
#include <utility>
30+
#include <fstream>
3031

3132
// fix problem with std::min and std::max
3233
#if defined(_WIN32)
@@ -3719,6 +3720,16 @@ std::unique_ptr<server_res_generator> server_routes::handle_completions_impl(
37193720
// TODO: this log can become very long, put it behind a flag or think about a more compact format
37203721
//SRV_DBG("Prompt: %s\n", prompt.is_string() ? prompt.get<std::string>().c_str() : prompt.dump(2).c_str());
37213722

3723+
if (!params.path_prompts_log_dir.empty()) {
3724+
const auto file_path = std::filesystem::path(params.path_prompts_log_dir) / string_format("%012" PRId64 ".txt", ggml_time_ms());
3725+
std::ofstream f(file_path);
3726+
if (f) {
3727+
f << (prompt.is_string() ? prompt.get<std::string>().c_str() : prompt.dump(2).c_str());
3728+
} else {
3729+
SRV_ERR("failed to create %s\n", file_path.string().c_str());
3730+
}
3731+
}
3732+
37223733
// process prompt
37233734
std::vector<server_tokens> inputs;
37243735

0 commit comments

Comments
 (0)