diff --git a/examples/common.cpp b/examples/common.cpp index 1308f84109519..23054b6f39931 100644 --- a/examples/common.cpp +++ b/examples/common.cpp @@ -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); @@ -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"); @@ -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; @@ -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, ""); diff --git a/examples/common.h b/examples/common.h index 2b66382a6a5e0..7f25804eff549 100644 --- a/examples/common.h +++ b/examples/common.h @@ -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); @@ -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); diff --git a/examples/main/main.cpp b/examples/main/main.cpp index 47b418d972bbc..3d948506c2c0c 100644 --- a/examples/main/main.cpp +++ b/examples/main/main.cpp @@ -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) {