7
7
#include < cstdio>
8
8
#include < cstring>
9
9
#include < fstream>
10
+ #include < iostream>
10
11
#include < map>
11
12
#include < string>
12
13
#include < vector>
@@ -997,11 +998,6 @@ int main(int argc, char ** argv) {
997
998
break ;
998
999
}
999
1000
}
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
- }
1005
1001
}
1006
1002
1007
1003
// display text
@@ -1011,6 +1007,10 @@ int main(int argc, char ** argv) {
1011
1007
}
1012
1008
fflush (stdout);
1013
1009
}
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
+ }
1014
1014
1015
1015
// in interactive mode, and not currently processing queued inputs;
1016
1016
// check if we should prompt the user for more
@@ -1032,43 +1032,33 @@ int main(int argc, char ** argv) {
1032
1032
}
1033
1033
1034
1034
// currently being interactive
1035
+ if (params.use_color ) printf (ANSI_BOLD ANSI_COLOR_GREEN);
1036
+ std::string buffer;
1037
+ std::string line;
1035
1038
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 () != ' \\ ' ) {
1053
1042
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
1063
1045
}
1046
+ buffer += line + ' \n ' ; // Append the line to the result
1047
+ } while (another_line);
1048
+ if (params.use_color ) printf (ANSI_COLOR_RESET);
1064
1049
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 ());
1066
1052
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 ());
1068
1055
}
1069
1056
1070
- is_interacting = false ;
1057
+ remaining_tokens -= line_inp.size ();
1058
+
1059
+ input_noecho = true ; // do not echo this again
1071
1060
}
1061
+ is_interacting = false ;
1072
1062
}
1073
1063
1074
1064
// end of text token
0 commit comments