Refactor to use a common function to do file output, which both outputs to file and selects different outputs for special token and plain text cases
This commit is contained in:
parent
a0319dd06c
commit
c2baff9161
|
|
@ -58,6 +58,8 @@ struct cli_context {
|
||||||
task_params defaults;
|
task_params defaults;
|
||||||
bool verbose_prompt;
|
bool verbose_prompt;
|
||||||
common_reasoning_format reasoning_format;
|
common_reasoning_format reasoning_format;
|
||||||
|
bool file_streaming = false;
|
||||||
|
std::ofstream * file_out = nullptr;
|
||||||
|
|
||||||
// thread for showing "loading" animation
|
// thread for showing "loading" animation
|
||||||
std::atomic<bool> loading_show;
|
std::atomic<bool> loading_show;
|
||||||
|
|
@ -78,7 +80,7 @@ struct cli_context {
|
||||||
reasoning_format = params.reasoning_format;
|
reasoning_format = params.reasoning_format;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string generate_completion(result_timings & out_timings, std::ofstream * file_out = nullptr) {
|
std::string generate_completion(result_timings & out_timings, std::ofstream * file_to_use = nullptr) {
|
||||||
server_response_reader rd = ctx_server.get_response_reader();
|
server_response_reader rd = ctx_server.get_response_reader();
|
||||||
auto chat_params = format_chat();
|
auto chat_params = format_chat();
|
||||||
{
|
{
|
||||||
|
|
@ -108,16 +110,12 @@ struct cli_context {
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if we are doing file output
|
// check if we are doing file output
|
||||||
bool file_streaming = (file_out != nullptr && file_out->is_open());
|
file_out = file_to_use;
|
||||||
if (file_streaming) {
|
file_streaming = (file_out != nullptr && file_out->is_open());
|
||||||
if (defaults.special_characters) {
|
append_file_out(
|
||||||
*file_out << chat_params.prompt;
|
"[Prompt]: " + messages.back()["content"].get<std::string>() + "\n\n",
|
||||||
}
|
chat_params.prompt
|
||||||
else {
|
);
|
||||||
*file_out << "[Prompt]: " << messages.back()["content"].get<std::string>() << "\n\n";
|
|
||||||
}
|
|
||||||
file_out->flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait for first result
|
// wait for first result
|
||||||
console::spinner::start();
|
console::spinner::start();
|
||||||
|
|
@ -149,60 +147,40 @@ struct cli_context {
|
||||||
if (is_thinking) {
|
if (is_thinking) {
|
||||||
console::log("\n[End thinking]\n\n");
|
console::log("\n[End thinking]\n\n");
|
||||||
console::set_display(DISPLAY_TYPE_RESET);
|
console::set_display(DISPLAY_TYPE_RESET);
|
||||||
if (file_streaming && is_thinking) {
|
append_file_out("\n\n", "</think>");
|
||||||
if (defaults.special_characters) {
|
|
||||||
*file_out << "<\\think>";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
*file_out << "\n\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
is_thinking = false;
|
is_thinking = false;
|
||||||
}
|
}
|
||||||
curr_content += diff.content_delta;
|
curr_content += diff.content_delta;
|
||||||
console::log("%s", diff.content_delta.c_str());
|
console::log("%s", diff.content_delta.c_str());
|
||||||
console::flush();
|
console::flush();
|
||||||
if (file_streaming) {
|
if (!content_started) {
|
||||||
if (!content_started && !defaults.special_characters) {
|
append_file_out("[Assistant]: ", "");
|
||||||
*file_out << "[Assistant]: ";
|
|
||||||
}
|
|
||||||
content_started = true;
|
content_started = true;
|
||||||
*file_out << diff.content_delta;
|
|
||||||
file_out->flush();
|
|
||||||
}
|
}
|
||||||
|
append_file_out(diff.content_delta);
|
||||||
}
|
}
|
||||||
if (!diff.reasoning_content_delta.empty()) {
|
if (!diff.reasoning_content_delta.empty()) {
|
||||||
console::set_display(DISPLAY_TYPE_REASONING);
|
console::set_display(DISPLAY_TYPE_REASONING);
|
||||||
|
std::string reasoning_delta = diff.reasoning_content_delta;
|
||||||
if (!is_thinking) {
|
if (!is_thinking) {
|
||||||
console::log("[Start thinking]\n");
|
console::log("[Start thinking]\n");
|
||||||
if (file_streaming) {
|
append_file_out("[Thinking]: ", "<think>");
|
||||||
if (defaults.special_characters) {
|
if (reasoning_delta == "<think>") {
|
||||||
*file_out << "<think>";
|
reasoning_delta = "";
|
||||||
}
|
|
||||||
else {
|
|
||||||
*file_out << "[Thinking]: ";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
is_thinking = true;
|
is_thinking = true;
|
||||||
console::log("%s", diff.reasoning_content_delta.c_str());
|
console::log("%s", reasoning_delta.c_str());
|
||||||
console::flush();
|
console::flush();
|
||||||
if (file_streaming) {
|
append_file_out(reasoning_delta);
|
||||||
*file_out << diff.reasoning_content_delta;
|
|
||||||
file_out->flush();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto res_final = dynamic_cast<server_task_result_cmpl_final *>(result.get());
|
auto res_final = dynamic_cast<server_task_result_cmpl_final *>(result.get());
|
||||||
if (res_final) {
|
if (res_final) {
|
||||||
out_timings = std::move(res_final->timings);
|
out_timings = std::move(res_final->timings);
|
||||||
if (file_streaming) {
|
append_file_out("\n\n","");
|
||||||
if (!defaults.special_characters) {
|
|
||||||
*file_out << "\n\n";
|
|
||||||
}
|
|
||||||
file_out->flush();
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
result = rd.next(should_stop);
|
result = rd.next(should_stop);
|
||||||
|
|
@ -229,6 +207,18 @@ struct cli_context {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void append_file_out(const std::string & content, const std::optional<std::string> & special_characters_content = std::nullopt) {
|
||||||
|
if (!file_streaming) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (defaults.special_characters && special_characters_content.has_value()) {
|
||||||
|
*file_out << special_characters_content.value();
|
||||||
|
} else {
|
||||||
|
*file_out << content;
|
||||||
|
}
|
||||||
|
file_out->flush();
|
||||||
|
}
|
||||||
|
|
||||||
common_chat_params format_chat() {
|
common_chat_params format_chat() {
|
||||||
auto meta = ctx_server.get_meta();
|
auto meta = ctx_server.get_meta();
|
||||||
auto & chat_params = meta.chat_params;
|
auto & chat_params = meta.chat_params;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue