Switch to storing the pointer in a std::optional<std::ofstream *> as part of the context class.
This commit is contained in:
parent
80391cd613
commit
c522288ab6
|
|
@ -60,8 +60,7 @@ struct cli_context {
|
||||||
int reasoning_budget = -1;
|
int reasoning_budget = -1;
|
||||||
std::string reasoning_budget_message;
|
std::string reasoning_budget_message;
|
||||||
common_reasoning_format reasoning_format;
|
common_reasoning_format reasoning_format;
|
||||||
bool file_streaming = false;
|
std::optional<std::ofstream *> file_out = std::nullopt;
|
||||||
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;
|
||||||
|
|
@ -84,7 +83,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_to_use = nullptr) {
|
std::string generate_completion(result_timings & out_timings) {
|
||||||
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();
|
||||||
{
|
{
|
||||||
|
|
@ -131,9 +130,6 @@ struct cli_context {
|
||||||
console::set_display(DISPLAY_TYPE_RESET);
|
console::set_display(DISPLAY_TYPE_RESET);
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if we are doing file output
|
|
||||||
file_out = file_to_use;
|
|
||||||
file_streaming = (file_out != nullptr && file_out->is_open());
|
|
||||||
append_file_out(
|
append_file_out(
|
||||||
"[Prompt]: " + messages.back()["content"].get<std::string>() + "\n\n",
|
"[Prompt]: " + messages.back()["content"].get<std::string>() + "\n\n",
|
||||||
chat_params.prompt
|
chat_params.prompt
|
||||||
|
|
@ -230,15 +226,15 @@ struct cli_context {
|
||||||
}
|
}
|
||||||
|
|
||||||
void append_file_out(const std::string & content, const std::optional<std::string> & special_characters_content = std::nullopt) {
|
void append_file_out(const std::string & content, const std::optional<std::string> & special_characters_content = std::nullopt) {
|
||||||
if (!file_streaming) {
|
if (!file_out.has_value()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (defaults.special_characters && special_characters_content.has_value()) {
|
if (defaults.special_characters && special_characters_content.has_value()) {
|
||||||
*file_out << special_characters_content.value();
|
*file_out.value() << special_characters_content.value();
|
||||||
} else {
|
} else {
|
||||||
*file_out << content;
|
*file_out.value() << content;
|
||||||
}
|
}
|
||||||
file_out->flush();
|
file_out.value()->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
common_chat_params format_chat() {
|
common_chat_params format_chat() {
|
||||||
|
|
@ -409,11 +405,13 @@ int main(int argc, char ** argv) {
|
||||||
std::ofstream output_file;
|
std::ofstream output_file;
|
||||||
if (!params.out_file.empty()) {
|
if (!params.out_file.empty()) {
|
||||||
output_file.open(params.out_file, std::ios::binary);
|
output_file.open(params.out_file, std::ios::binary);
|
||||||
if (!output_file) {
|
if (!output_file || !output_file.is_open()) {
|
||||||
console::error("Failed to open output file '%s'\n", params.out_file.c_str());
|
console::error("Failed to open output file '%s'\n", params.out_file.c_str());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
ctx_cli.file_out = &output_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
console::set_display(DISPLAY_TYPE_RESET);
|
console::set_display(DISPLAY_TYPE_RESET);
|
||||||
console::set_completion_callback(auto_completion_callback);
|
console::set_completion_callback(auto_completion_callback);
|
||||||
|
|
||||||
|
|
@ -604,7 +602,7 @@ int main(int argc, char ** argv) {
|
||||||
cur_msg.clear();
|
cur_msg.clear();
|
||||||
}
|
}
|
||||||
result_timings timings;
|
result_timings timings;
|
||||||
std::string assistant_content = ctx_cli.generate_completion(timings, params.out_file.empty() ? nullptr : &output_file);
|
std::string assistant_content = ctx_cli.generate_completion(timings);
|
||||||
ctx_cli.messages.push_back({
|
ctx_cli.messages.push_back({
|
||||||
{"role", "assistant"},
|
{"role", "assistant"},
|
||||||
{"content", assistant_content}
|
{"content", assistant_content}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue