Skip to content

Commit

Permalink
Integrate linenoise
Browse files Browse the repository at this point in the history
We get prompt history etc.

Signed-off-by: Eric Curtin <ecurtin@redhat.com>
  • Loading branch information
ericcurtin committed Jan 15, 2025
1 parent bd3e9ae commit 29d40de
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions examples/run/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "common.h"
#include "json.hpp"
#include "linenoise.h"
#include "llama-cpp.h"

#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(_WIN32)
Expand Down Expand Up @@ -807,21 +808,22 @@ static int generate(LlamaData & llama_data, const std::string & prompt, std::str
batch = llama_batch_get_one(&new_token_id, 1);
}

printf("\033[0m");
return 0;
}

static int read_user_input(std::string & user) {
std::getline(std::cin, user);
static int read_user_input(std::string & user_input) {
std::getline(std::cin, user_input);
if (std::cin.eof()) {
printf("\n");
return 1;
}

if (user == "/bye") {
if (user_input == "/bye") {
return 1;
}

if (user.empty()) {
if (user_input.empty()) {
return 2;
}

Expand Down Expand Up @@ -858,18 +860,45 @@ static int apply_chat_template_with_error_handling(LlamaData & llama_data, const
return 0;
}

struct c_str {
const char * data = nullptr;

~c_str() { free(const_cast<char *>(data)); }
};

// Helper function to handle user input
static int handle_user_input(std::string & user_input, const std::string & user) {
if (!user.empty()) {
user_input = user;
return 0; // No need for interactive input
}

static const char * prompt_prefix = "> ";
#ifdef WIN32
printf(
"\r%*s"
"\r\033[32m> \033[0m",
get_terminal_width(), " ");
"\r\033[0m%s",
get_terminal_width(), " ", prompt_prefix);
return read_user_input(user_input); // Returns true if input ends the loop
#else
c_str line;
line.data = linenoise(prompt_prefix);
if (!line.data) {
return 1;
}

user_input = line.data;
if (user_input == "/bye") {
return 1;
}

if (user_input.empty()) {
return 2;
}

linenoiseHistoryAdd(line.data);
return 0;
#endif
}

static bool is_stdin_a_terminal() {
Expand Down

0 comments on commit 29d40de

Please sign in to comment.