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 <[email protected]>
  • Loading branch information
ericcurtin committed Jan 15, 2025
1 parent bd3e9ae commit 47487ae
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 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,24 +808,51 @@ 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);
struct c_str {
const char * data = nullptr;

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

static int read_user_input(std::string & user_input) {
static const char * prompt_prefix = "> ";
#ifdef WIN32
printf(
"\r%*s"
"\r\033[0m%s",
get_terminal_width(), " ", prompt_prefix);

std::getline(std::cin, user_input);
if (std::cin.eof()) {
printf("\n");
return 1;
}
#else
c_str line;
line.data = linenoise(prompt_prefix);
if (!line.data) {
return 1;
}

user_input = line.data;
#endif

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

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

#ifndef WIN32
linenoiseHistoryAdd(line.data);
#endif

return 0; // Should have data in happy path
}

Expand Down Expand Up @@ -865,10 +893,6 @@ static int handle_user_input(std::string & user_input, const std::string & user)
return 0; // No need for interactive input
}

printf(
"\r%*s"
"\r\033[32m> \033[0m",
get_terminal_width(), " ");
return read_user_input(user_input); // Returns true if input ends the loop
}

Expand Down

0 comments on commit 47487ae

Please sign in to comment.