Skip to content

Commit 71f75c1

Browse files
committed
Refactor interactive mode in main.cpp
1 parent 6ea19fe commit 71f75c1

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

main.cpp

+37-32
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ void sigint_handler(int signo) {
5151
#endif
5252

5353

54+
void process_interactive_input(llama_context& ctx, const gpt_params& params);
55+
5456
int main(int argc, char ** argv) {
5557
ggml_time_init();
5658
const int64_t t_main_start_us = ggml_time_us();
@@ -95,7 +97,7 @@ int main(int argc, char ** argv) {
9597
// tokenize the reverse prompt
9698
std::vector<gpt_vocab::id> antiprompt_inp = llama_tokenize_text(ctx, params.antiprompt);
9799

98-
100+
// Setup interactive mode
99101
if (params.interactive) {
100102
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
101103
struct sigaction sigint_action;
@@ -176,43 +178,16 @@ int main(int argc, char ** argv) {
176178

177179
// in interactive mode, and not currently processing queued inputs;
178180
// 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) {
181182
// 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)) {
183184
// reverse prompt found
184185
is_interacting = true;
185186
}
186187
if (is_interacting) {
187188
// 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
216191
is_interacting = false;
217192
}
218193
}
@@ -243,3 +218,33 @@ int main(int argc, char ** argv) {
243218
}
244219
return 0;
245220
}
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

Comments
 (0)