@@ -51,6 +51,8 @@ void sigint_handler(int signo) {
51
51
#endif
52
52
53
53
54
+ void process_interactive_input (llama_context& ctx, const gpt_params& params);
55
+
54
56
int main (int argc, char ** argv) {
55
57
ggml_time_init ();
56
58
const int64_t t_main_start_us = ggml_time_us ();
@@ -95,7 +97,7 @@ int main(int argc, char ** argv) {
95
97
// tokenize the reverse prompt
96
98
std::vector<gpt_vocab::id> antiprompt_inp = llama_tokenize_text (ctx, params.antiprompt );
97
99
98
-
100
+ // Setup interactive mode
99
101
if (params.interactive ) {
100
102
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
101
103
struct sigaction sigint_action;
@@ -176,43 +178,16 @@ int main(int argc, char ** argv) {
176
178
177
179
// in interactive mode, and not currently processing queued inputs;
178
180
// check if we should prompt the user for more
179
- if (params.interactive && !llama_has_unconsumed_input (ctx)) {
180
- const std::vector<gpt_vocab::id>& last_n_tokens = llama_context_get_last_n_tokens (ctx);
181
+ if (params.interactive ) {
181
182
// check for reverse prompt
182
- if (antiprompt_inp.size () && std::equal (antiprompt_inp. rbegin () , antiprompt_inp. rend (), last_n_tokens. rbegin () )) {
183
+ if (antiprompt_inp.size () && llama_is_anti_prompt_present (ctx , antiprompt_inp)) {
183
184
// reverse prompt found
184
185
is_interacting = true ;
185
186
}
186
187
if (is_interacting) {
187
188
// currently being interactive
188
- bool another_line=true ;
189
- while (another_line) {
190
- fflush (stdout);
191
- char buf[256 ] = {0 };
192
- int n_read;
193
- if (params.use_color ) printf (ANSI_BOLD ANSI_COLOR_GREEN);
194
- if (scanf (" %255[^\n ]%n%*c" , buf, &n_read) <= 0 ) {
195
- // presumable empty line, consume the newline
196
- scanf (" %*c" );
197
- n_read=0 ;
198
- }
199
- if (params.use_color ) printf (ANSI_COLOR_RESET);
200
-
201
- if (n_read > 0 && buf[n_read-1 ]==' \\ ' ) {
202
- another_line = true ;
203
- buf[n_read-1 ] = ' \n ' ;
204
- buf[n_read] = 0 ;
205
- } else {
206
- another_line = false ;
207
- buf[n_read] = ' \n ' ;
208
- buf[n_read+1 ] = 0 ;
209
- }
210
-
211
- // Do not clear existing context in interactive mode
212
- llama_update_input (ctx, buf);
213
- input_noecho = true ; // do not echo this again
214
- }
215
-
189
+ process_interactive_input (ctx, params);
190
+ input_noecho = true ; // do not echo this input again
216
191
is_interacting = false ;
217
192
}
218
193
}
@@ -243,3 +218,33 @@ int main(int argc, char ** argv) {
243
218
}
244
219
return 0 ;
245
220
}
221
+
222
+ void process_interactive_input (llama_context& ctx, const gpt_params& params)
223
+ {
224
+ bool another_line=true ;
225
+ while (another_line) {
226
+ fflush (stdout);
227
+ char buf[256 ] = {0 };
228
+ int n_read;
229
+ if (params.use_color ) printf (ANSI_BOLD ANSI_COLOR_GREEN);
230
+ if (scanf (" %255[^\n ]%n%*c" , buf, &n_read) <= 0 ) {
231
+ // presumable empty line, consume the newline
232
+ scanf (" %*c" );
233
+ n_read=0 ;
234
+ }
235
+ if (params.use_color ) printf (ANSI_COLOR_RESET);
236
+
237
+ if (n_read > 0 && buf[n_read-1 ]==' \\ ' ) {
238
+ another_line = true ;
239
+ buf[n_read-1 ] = ' \n ' ;
240
+ buf[n_read] = 0 ;
241
+ } else {
242
+ another_line = false ;
243
+ buf[n_read] = ' \n ' ;
244
+ buf[n_read+1 ] = 0 ;
245
+ }
246
+
247
+ // Do not clear existing context in interactive mode
248
+ llama_update_input (ctx, buf);
249
+ }
250
+ }
0 commit comments