Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Introduce new --chat flag for chatbot
Justine Tunney7b395be84b67
diff --git a/llama.cpp/common.cpp b/llama.cpp/common.cppindex 80d6353b..41eab79b 100644--- a/llama.cpp/common.cpp+++ b/llama.cpp/common.cpp@@ -199,6 +199,12 @@ bool gpt_params_find_arg(int argc, char ** argv, const std::string & arg, gpt_paif (arg == "--cli") {return true;}+ if (arg == "--chat") {+ return true;+ }+ if (arg == "--server") {+ return true;+ }if (arg == "--trace") {FLAG_trace = true;FLAG_unsecure = true;diff --git a/llama.cpp/common.h b/llama.cpp/common.hindex 8e731d01..44fd7a80 100644--- a/llama.cpp/common.h+++ b/llama.cpp/common.h@@ -81,7 +81,7 @@ struct gpt_params {int32_t n_threads_batch = -1; // number of threads to use for batch processing (-1 = use n_threads)int32_t n_threads_batch_draft = -1;int32_t n_predict = -1; // new tokens to predict- int32_t n_ctx = 0; // context size+ int32_t n_ctx = 8192; // context size [jart]int32_t n_batch = 2048; // logical batch size for prompt processing (must be >=32 to use BLAS)int32_t n_ubatch = 512; // physical batch size for prompt processing (must be >=32 to use BLAS)int32_t n_keep = 0; // number of tokens to keep from initial promptdiff --git a/llama.cpp/ggml-backend.c b/llama.cpp/ggml-backend.cindex 47207a12..bd4aeacd 100644--- a/llama.cpp/ggml-backend.c+++ b/llama.cpp/ggml-backend.c@@ -11,6 +11,7 @@#include <stdio.h>#include <stdlib.h>#include <string.h>+#include <cosmo.h>#include "llamafile/log.h"@@ -2259,10 +2260,14 @@ GGML_CALL static void *system_malloc(size_t n) {}GGML_CALL static char *system_getenv(const char *s) {+ if (IsWindows())+ return 0;return getenv(s);}GGML_CALL static long system_write(int fd, const void *p, long n) {+ if (IsWindows())+ return 0;return write(fd, p, n);}diff --git a/llama.cpp/main/main.cpp b/llama.cpp/main/main.cppindex 5cebaebf..abdf66e5 100644--- a/llama.cpp/main/main.cpp+++ b/llama.cpp/main/main.cpp@@ -146,6 +146,30 @@ static std::string chat_add_and_format(struct llama_model * model, std::vector<lreturn formatted;}+enum Program {+ UNKNOWN,+ MAIN,+ SERVER,+ CHATBOT,+ EMBEDDING,+};++enum Program determine_program(char *argv[]) {+ enum Program prog = UNKNOWN;+ for (int i = 0; argv[i]; ++i) {+ if (!strcmp(argv[i], "--cli")) {+ prog = MAIN;+ } else if (!strcmp(argv[i], "--chat")) {+ prog = CHATBOT;+ } else if (!strcmp(argv[i], "--server")) {+ prog = SERVER;+ } else if (!strcmp(argv[i], "--embedding")) {+ prog = EMBEDDING;+ }+ }+ return prog;+}+int main(int argc, char ** argv) {mallopt(M_GRANULARITY, 2 * 1024 * 1024);@@ -167,21 +191,29 @@ int main(int argc, char ** argv) {llamafile_check_cpu();ShowCrashReports();LoadZipArgs(&argc, &argv);- launch_sigint_thread();- if (!llamafile_has(argv, "--cli") &&- (llamafile_has(argv, "--server") ||- (!llamafile_has(argv, "-p") &&- !llamafile_has(argv, "-f") &&- !llamafile_has(argv, "--random-prompt")))) {+ enum Program prog = determine_program(argv);++ if (prog == SERVER ||+ (prog == UNKNOWN &&+ !llamafile_has(argv, "-p") &&+ !llamafile_has(argv, "-f") &&+ !llamafile_has(argv, "--random-prompt"))) {return server_cli(argc, argv);}- if (llamafile_has(argv, "--embedding")) {+ if (prog == CHATBOT) {+ int chatbot_main(int, char **);+ return chatbot_main(argc, argv);+ }++ if (prog == EMBEDDING) {int embedding_cli(int, char **);return embedding_cli(argc, argv);}+ launch_sigint_thread();+gpt_params params;g_params = ¶ms;diff --git a/llama.cpp/server/server.cpp b/llama.cpp/server/server.cppindex e6652512..c94eb158 100644--- a/llama.cpp/server/server.cpp+++ b/llama.cpp/server/server.cpp@@ -2526,6 +2526,15 @@ static void server_params_parse(int argc, char **argv, server_params &sparams,else if (arg == "--server"){}+ else if (arg == "--chat")+ {+ }+ else if (arg == "--cli")+ {+ }+ else if (arg == "--embedding")+ {+ }else if (arg == "--fast"){FLAG_fast = true;@@ -2586,13 +2595,6 @@ static void server_params_parse(int argc, char **argv, server_params &sparams,else { invalid_param = true; break; }}}- else if (arg == "--embedding")- {- fprintf(stderr, "error: the --embedding endpoint is no longer supported in the the standard llamafile --server. "- "please use our new llamafiler command, which gives you a 4x faster embedding server. this is our new "- "server for llamafile that, once feature complete, will replace this one entirely.\n");- exit(1);- }else if (arg == "-cb" || arg == "--cont-batching"){params.cont_batching = true;diff --git a/llamafile/BUILD.mk b/llamafile/BUILD.mkindex bc51c3a5..3d260a15 100644--- a/llamafile/BUILD.mk+++ b/llamafile/BUILD.mk@@ -39,10 +39,6 @@ o/$(MODE)/llamafile/simple: \o/$(MODE)/llamafile/simple.o \o/$(MODE)/llama.cpp/llama.cpp.a \-o/$(MODE)/llamafile/chatbot: \- o/$(MODE)/llamafile/chatbot.o \- o/$(MODE)/llama.cpp/llama.cpp.a \-o/$(MODE)/llamafile/tokenize: \o/$(MODE)/llamafile/tokenize.o \o/$(MODE)/llama.cpp/llama.cpp.a \@@ -52,7 +48,6 @@ o/$(MODE)/llamafile: \$(LLAMAFILE_OBJS) \o/$(MODE)/llamafile/server \o/$(MODE)/llamafile/simple \- o/$(MODE)/llamafile/chatbot \o/$(MODE)/llamafile/zipalign \o/$(MODE)/llamafile/zipcheck \o/$(MODE)/llamafile/tokenize \diff --git a/llamafile/chatbot.cpp b/llamafile/chatbot.cppindex 3379ea33..be589703 100644--- a/llamafile/chatbot.cpp+++ b/llamafile/chatbot.cpp@@ -20,6 +20,7 @@#include <ctype.h>#include <math.h>#include <signal.h>+#include <sstream>#include <stdio.h>#include <string>#include <vector>@@ -29,7 +30,21 @@#include "llamafile/bestline.h"#include "llamafile/llamafile.h"-static sig_atomic_t g_got_sigint;+#define BOLD "\e[1m"+#define FAINT "\e[2m"+#define UNBOLD "\e[22m"+#define RED "\e[31m"+#define MAGENTA "\e[35m"+#define UNFOREGROUND "\e[39m"+#define BRIGHT_BLACK "\e[90m"+#define BRIGHT_RED "\e[91m"+#define BRIGHT_GREEN "\e[92m"+#define CLEAR_FORWARD "\e[K"++static int n_past;+static llama_model *g_model;+static llama_context *g_ctx;+static volatile sig_atomic_t g_got_sigint;static void on_sigint(int sig) {g_got_sigint = 1;@@ -57,62 +72,122 @@ static std::string basename(const std::string_view path) {}}-static bool eval_tokens(struct llama_context *ctx_llama, std::vector<llama_token> tokens,- int n_batch, int *n_past) {+static void on_completion(const char *line, bestlineCompletions *comp) {+ static const char *const kCompletions[] = {+ "/context", //+ "/stats", //+ };+ for (int i = 0; i < sizeof(kCompletions) / sizeof(*kCompletions); ++i)+ if (startswith(kCompletions[i], line))+ bestlineAddCompletion(comp, kCompletions[i]);+}++// handle irc style commands like: `/arg0 arg1 arg2`+static bool handle_command(const char *command) {+ if (!(command[0] == '/' && std::isalpha(command[1])))+ return false;+ std::vector<std::string> args;+ std::istringstream iss(command + 1);+ std::string arg;+ while (iss >> arg)+ args.push_back(arg);+ if (args[0] == "stats") {+ FLAG_log_disable = false;+ llama_print_timings(g_ctx);+ FLAG_log_disable = true;+ } else if (args[0] == "context") {+ int configured_context = llama_n_ctx(g_ctx);+ int max_context = llama_n_ctx_train(g_model);+ printf("%d out of %d context tokens used (%d tokens remaining)\n", n_past,+ configured_context, configured_context - n_past);+ if (configured_context < max_context)+ printf("use the `-c %d` flag at startup for maximum context\n", max_context);+ } else {+ printf("%s: unrecognized command\n", args[0].c_str());+ }+ return true;+}++static void print_logo(const char16_t *s) {+ for (int i = 0; s[i]; ++i) {+ switch (s[i]) {+ case u'█':+ printf(MAGENTA "█" UNFOREGROUND);+ break;+ case u'╚':+ case u'═':+ case u'╝':+ case u'╗':+ case u'║':+ case u'╔':+ printf(FAINT "%C" UNBOLD, s[i]);+ break;+ default:+ printf("%C", s[i]);+ break;+ }+ }+}++static void die_out_of_context(void) {+ fprintf(stderr,+ "\n" BRIGHT_RED+ "error: ran out of context window at %d tokens; you can use the maximum "+ "context window size by passing the flag `-c %d` to llamafile." UNFOREGROUND "\n",+ n_past, llama_n_ctx_train(g_model));+ exit(1);+}++static void eval_tokens(std::vector<llama_token> tokens, int n_batch) {int N = (int)tokens.size();for (int i = 0; i < N; i += n_batch) {int n_eval = (int)tokens.size() - i;if (n_eval > n_batch)n_eval = n_batch;- if (llama_decode(ctx_llama, llama_batch_get_one(&tokens[i], n_eval, *n_past, 0)))- return false; // probably ran out of context- *n_past += n_eval;+ if (llama_decode(g_ctx, llama_batch_get_one(&tokens[i], n_eval, n_past, 0)))+ die_out_of_context();+ n_past += n_eval;}- return true;}-static bool eval_id(struct llama_context *ctx_llama, int id, int *n_past) {+static void eval_id(int id) {std::vector<llama_token> tokens;tokens.push_back(id);- return eval_tokens(ctx_llama, tokens, 1, n_past);+ eval_tokens(tokens, 1);}-static bool eval_string(struct llama_context *ctx_llama, const char *str, int n_batch, int *n_past,- bool add_special, bool parse_special) {+static void eval_string(const char *str, int n_batch, bool add_special, bool parse_special) {std::string str2 = str;- std::vector<llama_token> embd_inp =- ::llama_tokenize(ctx_llama, str2, add_special, parse_special);- return eval_tokens(ctx_llama, embd_inp, n_batch, n_past);+ eval_tokens(llama_tokenize(g_ctx, str2, add_special, parse_special), n_batch);}static void print_ephemeral(const char *description) {- fprintf(stderr, " \e[90m%s\e[39m\r", description);+ fprintf(stderr, " " BRIGHT_BLACK "%s" UNFOREGROUND "\r", description);}static void clear_ephemeral(void) {- fprintf(stderr, "\e[K");+ fprintf(stderr, CLEAR_FORWARD);}-int main(int argc, char **argv) {+int chatbot_main(int argc, char **argv) {llamafile_check_cpu();ShowCrashReports();log_disable();gpt_params params;- params.n_ctx = 8192; // make default context more reasonable-if (!gpt_params_parse(argc, argv, params))return 1;- printf("\n\e[32m\+ print_logo(u"\n\██╗ ██╗ █████╗ ███╗ ███╗ █████╗ ███████╗██╗██╗ ███████╗\n\██║ ██║ ██╔══██╗████╗ ████║██╔══██╗██╔════╝██║██║ ██╔════╝\n\██║ ██║ ███████║██╔████╔██║███████║█████╗ ██║██║ █████╗\n\██║ ██║ ██╔══██║██║╚██╔╝██║██╔══██║██╔══╝ ██║██║ ██╔══╝\n\███████╗███████╗██║ ██║██║ ╚═╝ ██║██║ ██║██║ ██║███████╗███████╗\n\-╚══════╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚══════╝\e[39m\n\-\e[1msoftware\e[22m: llamafile " LLAMAFILE_VERSION_STRING "\n\-\e[1mmodel\e[22m: %s\n\n",+╚══════╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚══════╝\n");++ printf(BOLD "software" UNBOLD ": llamafile " LLAMAFILE_VERSION_STRING "\n" //+ BOLD "model" UNBOLD ": %s\n\n",basename(params.model).c_str());print_ephemeral("initializing backend...");@@ -122,15 +197,15 @@ int main(int argc, char **argv) {print_ephemeral("initializing model...");llama_model_params model_params = llama_model_default_params();model_params.n_gpu_layers = llamafile_gpu_layers(35);- llama_model *model = llama_load_model_from_file(params.model.c_str(), model_params);- if (model == NULL)+ g_model = llama_load_model_from_file(params.model.c_str(), model_params);+ if (g_model == NULL)return 2;clear_ephemeral();print_ephemeral("initializing context...");llama_context_params ctx_params = llama_context_params_from_gpt_params(params);- llama_context *ctx = llama_new_context_with_model(model, ctx_params);- if (ctx == NULL)+ g_ctx = llama_new_context_with_model(g_model, ctx_params);+ if (g_ctx == NULL)return 3;clear_ephemeral();@@ -140,11 +215,10 @@ int main(int argc, char **argv) {"assistant gives helpful, detailed, and polite answers to the human's questions.";print_ephemeral("loading prompt...");- int n_past = 0;- bool add_bos = llama_should_add_bos_token(llama_get_model(ctx));+ bool add_bos = llama_should_add_bos_token(llama_get_model(g_ctx));std::vector<llama_chat_msg> chat = {{"system", params.prompt}};- std::string msg = llama_chat_apply_template(model, params.chat_template, chat, false);- eval_string(ctx, msg.c_str(), params.n_batch, &n_past, add_bos, true);+ std::string msg = llama_chat_apply_template(g_model, params.chat_template, chat, false);+ eval_string(msg.c_str(), params.n_batch, add_bos, true);clear_ephemeral();printf("%s\n", params.special ? msg.c_str() : params.prompt.c_str());@@ -153,27 +227,33 @@ int main(int argc, char **argv) {signal(SIGINT, on_sigint);// run chatbot- char *line;- bestlineLlamaMode(true);- while ((line = bestlineWithHistory(">>> ", "llamafile"))) {+ for (;;) {+ bestlineLlamaMode(true);+ bestlineSetCompletionCallback(on_completion);+ write(1, BRIGHT_GREEN, strlen(BRIGHT_GREEN));+ char *line = bestlineWithHistory(">>> ", "llamafile");+ write(1, UNFOREGROUND, strlen(UNFOREGROUND));+ if (!line)+ break;if (is_empty(line)) {free(line);continue;}+ if (handle_command(line)) {+ free(line);+ continue;+ }std::vector<llama_chat_msg> chat = {{"user", line}};- std::string msg = llama_chat_apply_template(model, params.chat_template, chat, true);- eval_string(ctx, msg.c_str(), params.n_batch, &n_past, false, true);+ std::string msg = llama_chat_apply_template(g_model, params.chat_template, chat, true);+ eval_string(msg.c_str(), params.n_batch, false, true);while (!g_got_sigint) {- llama_token id = llama_sampling_sample(ctx_sampling, ctx, NULL);- llama_sampling_accept(ctx_sampling, ctx, id, true);- printf("%s", llama_token_to_piece(ctx, id, params.special).c_str());- if (llama_token_is_eog(model, id))+ llama_token id = llama_sampling_sample(ctx_sampling, g_ctx, NULL);+ llama_sampling_accept(ctx_sampling, g_ctx, id, true);+ if (llama_token_is_eog(g_model, id))break;+ printf("%s", llama_token_to_piece(g_ctx, id, params.special).c_str());fflush(stdout);- if (!eval_id(ctx, id, &n_past)) {- fprintf(stderr, "[out of context]\n");- exit(1);- }+ eval_id(id);}g_got_sigint = 0;printf("\n");@@ -181,7 +261,8 @@ int main(int argc, char **argv) {}llama_sampling_free(ctx_sampling);- llama_free(ctx);- llama_free_model(model);+ llama_free(g_ctx);+ llama_free_model(g_model);llama_backend_free();+ return 0;}