Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added --disable-tty flag for interactive mode #1491

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions examples/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
break;
}
params.input_suffix = argv[i];
} else if (arg == "--disable-tty") {
params.disable_tty = true;
} else {
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
gpt_print_usage(argc, argv, default_params);
Expand Down Expand Up @@ -376,6 +378,7 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
fprintf(stderr, " halt generation at PROMPT, return control in interactive mode\n");
fprintf(stderr, " (can be specified more than once for multiple prompts).\n");
fprintf(stderr, " --color colorise output to distinguish prompt and user input from generations\n");
fprintf(stderr, " --disable-tty disable the use of TTY in interactive mode in favor of stderr\n");
fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1, use random seed for < 0)\n");
fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
fprintf(stderr, " -p PROMPT, --prompt PROMPT\n");
Expand Down Expand Up @@ -495,7 +498,7 @@ struct llama_context * llama_init_from_gpt_params(const gpt_params & params) {
return lctx;
}

void console_init(console_state & con_st) {
void console_init(console_state & con_st, bool disable_tty) {
#if defined(_WIN32)
// Windows-specific console initialization
DWORD dwMode = 0;
Expand Down Expand Up @@ -533,9 +536,13 @@ void console_init(console_state & con_st) {
new_termios.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);

con_st.tty = fopen("/dev/tty", "w+");
if (con_st.tty != nullptr) {
con_st.out = con_st.tty;
if (!disable_tty) {
con_st.tty = fopen("/dev/tty", "w+");
if (con_st.tty != nullptr) {
con_st.out = con_st.tty;
}
} else {
con_st.out = stderr;
}

setlocale(LC_ALL, "");
Expand Down
3 changes: 2 additions & 1 deletion examples/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct gpt_params {
bool use_mlock = false; // use mlock to keep model in memory
bool mem_test = false; // compute maximum memory usage
bool verbose_prompt = false; // print prompt tokens before generation
bool disable_tty = false; // disable TTY mode
};

bool gpt_params_parse(int argc, char ** argv, gpt_params & params);
Expand Down Expand Up @@ -124,7 +125,7 @@ struct console_state {
#endif
};

void console_init(console_state & con_st);
void console_init(console_state & con_st, bool disable_tty);
void console_cleanup(console_state & con_st);
void console_set_color(console_state & con_st, console_color_t color);
bool console_readline(console_state & con_st, std::string & line);
2 changes: 1 addition & 1 deletion examples/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int main(int argc, char ** argv) {
// (note for later: this is a slightly awkward choice)
con_st.use_color = params.use_color;
con_st.multiline_input = params.multiline_input;
console_init(con_st);
console_init(con_st, params.disable_tty);
atexit([]() { console_cleanup(con_st); });

if (params.perplexity) {
Expand Down