From 8470458b64a547b0933e460b46f6c54184e6a145 Mon Sep 17 00:00:00 2001 From: Rickey Bowers Jr Date: Thu, 16 Mar 2023 20:23:32 -0600 Subject: [PATCH 1/3] fix coloring of last `n_batch` of prompt, and refactor line input --- main.cpp | 55 +++++++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/main.cpp b/main.cpp index ca0fca8b36455..7cb69f5737118 100644 --- a/main.cpp +++ b/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -976,11 +977,6 @@ int main(int argc, char ** argv) { break; } } - - // reset color to default if we there is no pending user input - if (!input_noecho && params.use_color && embd_inp.size() == input_consumed) { - printf(ANSI_COLOR_RESET); - } } // display text @@ -990,6 +986,10 @@ int main(int argc, char ** argv) { } fflush(stdout); } + // reset color to default if we there is no pending user input + if (!input_noecho && params.use_color && embd_inp.size() == input_consumed) { + printf(ANSI_COLOR_RESET); + } // in interactive mode, and not currently processing queued inputs; // check if we should prompt the user for more @@ -1001,39 +1001,30 @@ int main(int argc, char ** argv) { } if (is_interacting) { // currently being interactive - bool another_line=true; - while (another_line) { - fflush(stdout); - char buf[256] = {0}; - int n_read; - if(params.use_color) printf(ANSI_BOLD ANSI_COLOR_GREEN); - if (scanf("%255[^\n]%n%*c", buf, &n_read) <= 0) { - // presumable empty line, consume the newline - scanf("%*c"); - n_read=0; - } - if(params.use_color) printf(ANSI_COLOR_RESET); - - if (n_read > 0 && buf[n_read-1]=='\\') { - another_line = true; - buf[n_read-1] = '\n'; - buf[n_read] = 0; - } else { + if (params.use_color) printf(ANSI_BOLD ANSI_COLOR_GREEN); + std::string buffer; + std::string line; + bool another_line = true; + do { + std::getline(std::cin, line); + if (line.empty() || line.back() != '\\') { another_line = false; - buf[n_read] = '\n'; - buf[n_read+1] = 0; } + else { + line.pop_back(); // Remove the continue character + } + buffer += line; // Append the line to the result + } while (another_line); + if (params.use_color) printf(ANSI_COLOR_RESET); - std::vector line_inp = ::llama_tokenize(vocab, buf, false); - embd_inp.insert(embd_inp.end(), line_inp.begin(), line_inp.end()); - - remaining_tokens -= line_inp.size(); + std::vector line_inp = ::llama_tokenize(vocab, buffer, false); + embd_inp.insert(embd_inp.end(), line_inp.begin(), line_inp.end()); - input_noecho = true; // do not echo this again - } + remaining_tokens -= line_inp.size(); - is_interacting = false; + input_noecho = true; // do not echo this again } + is_interacting = false; } // end of text token From 07d2da0cb06b8533bd241886ded12a10c250828b Mon Sep 17 00:00:00 2001 From: Rickey Bowers Jr Date: Thu, 16 Mar 2023 20:54:06 -0600 Subject: [PATCH 2/3] forgot the newline that needs to be sent to the model --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 7cb69f5737118..d21a4b1d18b16 100644 --- a/main.cpp +++ b/main.cpp @@ -1013,7 +1013,7 @@ int main(int argc, char ** argv) { else { line.pop_back(); // Remove the continue character } - buffer += line; // Append the line to the result + buffer += line + '\n'; // Append the line to the result } while (another_line); if (params.use_color) printf(ANSI_COLOR_RESET); From 77117c7e4d14dbd9d7e8d587b3b6e75f45c30d2e Mon Sep 17 00:00:00 2001 From: Rickey Bowers Jr Date: Sun, 19 Mar 2023 11:27:16 -0600 Subject: [PATCH 3/3] (per #283) try to force flush of color reset in SIGINT handler --- main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/main.cpp b/main.cpp index 5436074ba9eb1..1bc6a6dd149d8 100644 --- a/main.cpp +++ b/main.cpp @@ -748,6 +748,7 @@ static bool is_interacting = false; #if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32) void sigint_handler(int signo) { printf(ANSI_COLOR_RESET); + printf("\n"); // this also force flush stdout. if (signo == SIGINT) { if (!is_interacting) { is_interacting=true;