Skip to content

Commit 5c19c70

Browse files
authored
fix coloring of last n_batch of prompt, and refactor line input (#221)
* fix coloring of last `n_batch` of prompt, and refactor line input * forgot the newline that needs to be sent to the model * (per #283) try to force flush of color reset in SIGINT handler
1 parent 2456837 commit 5c19c70

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

main.cpp

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <cstdio>
88
#include <cstring>
99
#include <fstream>
10+
#include <iostream>
1011
#include <map>
1112
#include <string>
1213
#include <vector>
@@ -997,11 +998,6 @@ int main(int argc, char ** argv) {
997998
break;
998999
}
9991000
}
1000-
1001-
// reset color to default if we there is no pending user input
1002-
if (!input_noecho && params.use_color && (int) embd_inp.size() == input_consumed) {
1003-
printf(ANSI_COLOR_RESET);
1004-
}
10051001
}
10061002

10071003
// display text
@@ -1011,6 +1007,10 @@ int main(int argc, char ** argv) {
10111007
}
10121008
fflush(stdout);
10131009
}
1010+
// reset color to default if we there is no pending user input
1011+
if (!input_noecho && params.use_color && (int)embd_inp.size() == input_consumed) {
1012+
printf(ANSI_COLOR_RESET);
1013+
}
10141014

10151015
// in interactive mode, and not currently processing queued inputs;
10161016
// check if we should prompt the user for more
@@ -1032,43 +1032,33 @@ int main(int argc, char ** argv) {
10321032
}
10331033

10341034
// currently being interactive
1035+
if (params.use_color) printf(ANSI_BOLD ANSI_COLOR_GREEN);
1036+
std::string buffer;
1037+
std::string line;
10351038
bool another_line = true;
1036-
while (another_line) {
1037-
fflush(stdout);
1038-
char buf[256] = {0};
1039-
int n_read;
1040-
if (params.use_color) printf(ANSI_BOLD ANSI_COLOR_GREEN);
1041-
if (scanf("%255[^\n]%n%*c", buf, &n_read) <= 0) {
1042-
// presumable empty line, consume the newline
1043-
std::ignore = scanf("%*c");
1044-
n_read=0;
1045-
}
1046-
if (params.use_color) printf(ANSI_COLOR_RESET);
1047-
1048-
if (n_read > 0 && buf[n_read-1]=='\\') {
1049-
another_line = true;
1050-
buf[n_read-1] = '\n';
1051-
buf[n_read] = 0;
1052-
} else {
1039+
do {
1040+
std::getline(std::cin, line);
1041+
if (line.empty() || line.back() != '\\') {
10531042
another_line = false;
1054-
buf[n_read] = '\n';
1055-
buf[n_read+1] = 0;
1056-
}
1057-
1058-
std::vector<gpt_vocab::id> line_inp = ::llama_tokenize(vocab, buf, false);
1059-
embd_inp.insert(embd_inp.end(), line_inp.begin(), line_inp.end());
1060-
1061-
if (params.instruct) {
1062-
embd_inp.insert(embd_inp.end(), inp_sfx.begin(), inp_sfx.end());
1043+
} else {
1044+
line.pop_back(); // Remove the continue character
10631045
}
1046+
buffer += line + '\n'; // Append the line to the result
1047+
} while (another_line);
1048+
if (params.use_color) printf(ANSI_COLOR_RESET);
10641049

1065-
remaining_tokens -= line_inp.size();
1050+
std::vector<gpt_vocab::id> line_inp = ::llama_tokenize(vocab, buffer, false);
1051+
embd_inp.insert(embd_inp.end(), line_inp.begin(), line_inp.end());
10661052

1067-
input_noecho = true; // do not echo this again
1053+
if (params.instruct) {
1054+
embd_inp.insert(embd_inp.end(), inp_sfx.begin(), inp_sfx.end());
10681055
}
10691056

1070-
is_interacting = false;
1057+
remaining_tokens -= line_inp.size();
1058+
1059+
input_noecho = true; // do not echo this again
10711060
}
1061+
is_interacting = false;
10721062
}
10731063

10741064
// end of text token

0 commit comments

Comments
 (0)