Skip to content

Commit c0b1b49

Browse files
committed
Added chat template support to llama-run
Fixes: #11178 The llama-run CLI currently doesn't take the chat template of a model into account. Thus executing llama-run on a model requiring a chat template will fail. In order to solve this, the chat template is being downloaded from ollama or huggingface as well and applied during the chat. Signed-off-by: Michael Engel <[email protected]>
1 parent 00b4c3d commit c0b1b49

File tree

1 file changed

+157
-33
lines changed

1 file changed

+157
-33
lines changed

examples/run/run.cpp

+157-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#if defined(_WIN32)
2-
# include <windows.h>
32
# include <io.h>
3+
# include <windows.h>
44
#else
55
# include <sys/file.h>
66
# include <sys/ioctl.h>
@@ -12,12 +12,14 @@
1212
#endif
1313

1414
#include <signal.h>
15+
#include <sys/stat.h>
1516

1617
#include <climits>
1718
#include <cstdarg>
1819
#include <cstdio>
1920
#include <cstring>
2021
#include <filesystem>
22+
#include <fstream>
2123
#include <iostream>
2224
#include <sstream>
2325
#include <string>
@@ -34,14 +36,17 @@
3436
}
3537
#endif
3638

39+
#define LLAMA_USE_CURL
40+
3741
GGML_ATTRIBUTE_FORMAT(1, 2)
42+
3843
static std::string fmt(const char * fmt, ...) {
3944
va_list ap;
4045
va_list ap2;
4146
va_start(ap, fmt);
4247
va_copy(ap2, ap);
4348
const int size = vsnprintf(NULL, 0, fmt, ap);
44-
GGML_ASSERT(size >= 0 && size < INT_MAX); // NOLINT
49+
GGML_ASSERT(size >= 0 && size < INT_MAX); // NOLINT
4550
std::string buf;
4651
buf.resize(size);
4752
const int size2 = vsnprintf(const_cast<char *>(buf.data()), buf.size() + 1, fmt, ap2);
@@ -53,6 +58,7 @@ static std::string fmt(const char * fmt, ...) {
5358
}
5459

5560
GGML_ATTRIBUTE_FORMAT(1, 2)
61+
5662
static int printe(const char * fmt, ...) {
5763
va_list args;
5864
va_start(args, fmt);
@@ -101,7 +107,8 @@ class Opt {
101107

102108
llama_context_params ctx_params;
103109
llama_model_params model_params;
104-
std::string model_;
110+
std::string model_;
111+
std::string chat_template_;
105112
std::string user;
106113
int context_size = -1, ngl = -1;
107114
float temperature = -1;
@@ -137,7 +144,7 @@ class Opt {
137144
}
138145

139146
int parse(int argc, const char ** argv) {
140-
bool options_parsing = true;
147+
bool options_parsing = true;
141148
for (int i = 1, positional_args_i = 0; i < argc; ++i) {
142149
if (options_parsing && (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--context-size") == 0)) {
143150
if (handle_option_with_value(argc, argv, i, context_size) == 1) {
@@ -166,6 +173,11 @@ class Opt {
166173

167174
++positional_args_i;
168175
model_ = argv[i];
176+
} else if (options_parsing && strcmp(argv[i], "--chat-template") == 0) {
177+
if (i + 1 >= argc) {
178+
return 1;
179+
}
180+
chat_template_ = argv[++i];
169181
} else if (positional_args_i == 1) {
170182
++positional_args_i;
171183
user = argv[i];
@@ -475,7 +487,9 @@ class HttpClient {
475487
return (now_downloaded_plus_file_size * 100) / total_to_download;
476488
}
477489

478-
static std::string generate_progress_prefix(curl_off_t percentage) { return fmt("%3ld%% |", static_cast<long int>(percentage)); }
490+
static std::string generate_progress_prefix(curl_off_t percentage) {
491+
return fmt("%3ld%% |", static_cast<long int>(percentage));
492+
}
479493

480494
static double calculate_speed(curl_off_t now_downloaded, const std::chrono::steady_clock::time_point & start_time) {
481495
const auto now = std::chrono::steady_clock::now();
@@ -515,6 +529,7 @@ class HttpClient {
515529
printe("\r%*s\r%s%s| %s", get_terminal_width(), " ", progress_prefix.c_str(), progress_bar.c_str(),
516530
progress_suffix.c_str());
517531
}
532+
518533
// Function to write data to a file
519534
static size_t write_data(void * ptr, size_t size, size_t nmemb, void * stream) {
520535
FILE * out = static_cast<FILE *>(stream);
@@ -538,19 +553,23 @@ class LlamaData {
538553
std::vector<llama_chat_message> messages;
539554
std::vector<std::string> msg_strs;
540555
std::vector<char> fmtted;
556+
std::string chat_template;
541557

542558
int init(Opt & opt) {
543559
model = initialize_model(opt);
544560
if (!model) {
545561
return 1;
546562
}
547563

564+
chat_template = initialize_chat_template(model, opt);
565+
548566
context = initialize_context(model, opt);
549567
if (!context) {
550568
return 1;
551569
}
552570

553571
sampler = initialize_sampler(opt);
572+
554573
return 0;
555574
}
556575

@@ -573,21 +592,76 @@ class LlamaData {
573592
}
574593
#endif
575594

576-
int huggingface_dl(const std::string & model, const std::vector<std::string> headers, const std::string & bn) {
595+
int huggingface_dl_tmpl(const std::string & hfr, const std::vector<std::string> headers, const std::string & tn) {
596+
// if template already exists, don't download it
597+
struct stat info;
598+
if (stat(tn.c_str(), &info) == 0) {
599+
return 0;
600+
}
601+
602+
const std::string config_url = "https://huggingface.co/" + hfr + "/resolve/main/tokenizer_config.json";
603+
std::string tokenizer_config_str;
604+
download(config_url, headers, "", true, &tokenizer_config_str);
605+
if (tokenizer_config_str.empty()) {
606+
// still return success since tokenizer_config is optional
607+
return 0;
608+
}
609+
610+
nlohmann::json config = nlohmann::json::parse(tokenizer_config_str);
611+
std::string tmpl = config["chat_template"];
612+
613+
FILE * tmpl_file = fopen(tn.c_str(), "w");
614+
if (tmpl_file == NULL) {
615+
return 1;
616+
}
617+
fprintf(tmpl_file, "%s", tmpl.c_str());
618+
fclose(tmpl_file);
619+
620+
return 0;
621+
}
622+
623+
int huggingface_dl(const std::string & model, const std::vector<std::string> headers, const std::string & bn,
624+
const std::string & tn) {
625+
bool model_exists = std::filesystem::exists(bn);
626+
bool chat_tmpl_exists = std::filesystem::exists(tn);
627+
if (model_exists && chat_tmpl_exists) {
628+
return 0;
629+
}
630+
577631
// Find the second occurrence of '/' after protocol string
578632
size_t pos = model.find('/');
579633
pos = model.find('/', pos + 1);
580634
if (pos == std::string::npos) {
581635
return 1;
582636
}
583-
584637
const std::string hfr = model.substr(0, pos);
585638
const std::string hff = model.substr(pos + 1);
586-
const std::string url = "https://huggingface.co/" + hfr + "/resolve/main/" + hff;
587-
return download(url, headers, bn, true);
639+
640+
if (!chat_tmpl_exists) {
641+
const int ret = huggingface_dl_tmpl(hfr, headers, tn);
642+
if (ret) {
643+
return ret;
644+
}
645+
}
646+
647+
if (!model_exists) {
648+
const std::string url = "https://huggingface.co/" + hfr + "/resolve/main/" + hff;
649+
const int ret = download(url, headers, bn, true);
650+
if (ret) {
651+
return ret;
652+
}
653+
}
654+
return 0;
588655
}
589656

590-
int ollama_dl(std::string & model, const std::vector<std::string> headers, const std::string & bn) {
657+
int ollama_dl(std::string & model, const std::vector<std::string> headers, const std::string & bn,
658+
const std::string & tn) {
659+
bool model_exists = std::filesystem::exists(bn);
660+
bool chat_tmpl_exists = std::filesystem::exists(tn);
661+
if (model_exists && chat_tmpl_exists) {
662+
return 0;
663+
}
664+
591665
if (model.find('/') == std::string::npos) {
592666
model = "library/" + model;
593667
}
@@ -607,16 +681,34 @@ class LlamaData {
607681
}
608682

609683
nlohmann::json manifest = nlohmann::json::parse(manifest_str);
610-
std::string layer;
684+
std::string sha_model;
685+
std::string sha_template;
611686
for (const auto & l : manifest["layers"]) {
612687
if (l["mediaType"] == "application/vnd.ollama.image.model") {
613-
layer = l["digest"];
614-
break;
688+
sha_model = l["digest"];
689+
}
690+
if (l["mediaType"] == "application/vnd.ollama.image.template") {
691+
sha_template = l["digest"];
692+
}
693+
}
694+
695+
if (!chat_tmpl_exists && !sha_template.empty()) {
696+
std::string tmpl_blob_url = "https://registry.ollama.ai/v2/" + model + "/blobs/" + sha_template;
697+
const int tmpl_ret = download(tmpl_blob_url, headers, tn, true);
698+
if (tmpl_ret) {
699+
return tmpl_ret;
700+
}
701+
}
702+
703+
if (!model_exists) {
704+
std::string model_blob_url = "https://registry.ollama.ai/v2/" + model + "/blobs/" + sha_model;
705+
const int model_ret = download(model_blob_url, headers, bn, true);
706+
if (model_ret) {
707+
return model_ret;
615708
}
616709
}
617710

618-
std::string blob_url = "https://registry.ollama.ai/v2/" + model + "/blobs/" + layer;
619-
return download(blob_url, headers, bn, true);
711+
return 0;
620712
}
621713

622714
std::string basename(const std::string & path) {
@@ -628,6 +720,15 @@ class LlamaData {
628720
return path.substr(pos + 1);
629721
}
630722

723+
std::string get_proto(const std::string & model_) {
724+
const std::string::size_type pos = model_.find("://");
725+
if (pos == std::string::npos) {
726+
return "";
727+
}
728+
729+
return model_.substr(0, pos + 3); // Include "://"
730+
}
731+
631732
int remove_proto(std::string & model_) {
632733
const std::string::size_type pos = model_.find("://");
633734
if (pos == std::string::npos) {
@@ -638,38 +739,40 @@ class LlamaData {
638739
return 0;
639740
}
640741

641-
int resolve_model(std::string & model_) {
642-
int ret = 0;
643-
if (string_starts_with(model_, "file://") || std::filesystem::exists(model_)) {
742+
int resolve_model(std::string & model_, std::string & chat_template_) {
743+
int ret = 0;
744+
if (string_starts_with(model_, "file://")) {
644745
remove_proto(model_);
645-
646746
return ret;
647747
}
648748

749+
std::string proto = get_proto(model_);
750+
remove_proto(model_);
751+
649752
const std::string bn = basename(model_);
753+
const std::string tn = chat_template_.empty() ? bn + ".template" : chat_template_;
650754
const std::vector<std::string> headers = { "--header",
651755
"Accept: application/vnd.docker.distribution.manifest.v2+json" };
652-
if (string_starts_with(model_, "hf://") || string_starts_with(model_, "huggingface://")) {
653-
remove_proto(model_);
654-
ret = huggingface_dl(model_, headers, bn);
655-
} else if (string_starts_with(model_, "ollama://")) {
656-
remove_proto(model_);
657-
ret = ollama_dl(model_, headers, bn);
658-
} else if (string_starts_with(model_, "https://")) {
756+
if (string_starts_with(proto, "hf://") || string_starts_with(proto, "huggingface://")) {
757+
ret = huggingface_dl(model_, headers, bn, tn);
758+
} else if (string_starts_with(proto, "ollama://")) {
759+
ret = ollama_dl(model_, headers, bn, tn);
760+
} else if (string_starts_with(proto, "https://")) {
659761
download(model_, headers, bn, true);
660762
} else {
661-
ret = ollama_dl(model_, headers, bn);
763+
ret = ollama_dl(model_, headers, bn, tn);
662764
}
663765

664-
model_ = bn;
766+
model_ = bn;
767+
chat_template_ = tn;
665768

666769
return ret;
667770
}
668771

669772
// Initializes the model and returns a unique pointer to it
670773
llama_model_ptr initialize_model(Opt & opt) {
671774
ggml_backend_load_all();
672-
resolve_model(opt.model_);
775+
resolve_model(opt.model_, opt.chat_template_);
673776
printe(
674777
"\r%*s"
675778
"\rLoading model",
@@ -702,6 +805,27 @@ class LlamaData {
702805

703806
return sampler;
704807
}
808+
809+
std::string initialize_chat_template(const llama_model_ptr & model, const Opt & opt) {
810+
// if no template file doesn't exists, just return an empty string
811+
struct stat info;
812+
if (stat(opt.chat_template_.c_str(), &info) != 0) {
813+
return common_get_builtin_chat_template(model.get());
814+
}
815+
816+
std::ifstream tmpl_file;
817+
tmpl_file.open(opt.chat_template_);
818+
if (tmpl_file.fail()) {
819+
printe("failed to open chat template: '%s'\n", opt.chat_template_.c_str());
820+
return "";
821+
}
822+
823+
std::stringstream stream;
824+
stream << tmpl_file.rdbuf();
825+
tmpl_file.close();
826+
827+
return stream.str();
828+
}
705829
};
706830

707831
// Add a message to `messages` and store its content in `msg_strs`
@@ -713,11 +837,11 @@ static void add_message(const char * role, const std::string & text, LlamaData &
713837
// Function to apply the chat template and resize `formatted` if needed
714838
static int apply_chat_template(LlamaData & llama_data, const bool append) {
715839
int result = llama_chat_apply_template(
716-
llama_model_chat_template(llama_data.model.get()), llama_data.messages.data(), llama_data.messages.size(), append,
840+
llama_data.chat_template.c_str(), llama_data.messages.data(), llama_data.messages.size(), append,
717841
append ? llama_data.fmtted.data() : nullptr, append ? llama_data.fmtted.size() : 0);
718842
if (append && result > static_cast<int>(llama_data.fmtted.size())) {
719843
llama_data.fmtted.resize(result);
720-
result = llama_chat_apply_template(llama_model_chat_template(llama_data.model.get()), llama_data.messages.data(),
844+
result = llama_chat_apply_template(llama_data.chat_template.c_str(), llama_data.messages.data(),
721845
llama_data.messages.size(), append, llama_data.fmtted.data(),
722846
llama_data.fmtted.size());
723847
}
@@ -730,8 +854,8 @@ static int tokenize_prompt(const llama_vocab * vocab, const std::string & prompt
730854
std::vector<llama_token> & prompt_tokens) {
731855
const int n_prompt_tokens = -llama_tokenize(vocab, prompt.c_str(), prompt.size(), NULL, 0, true, true);
732856
prompt_tokens.resize(n_prompt_tokens);
733-
if (llama_tokenize(vocab, prompt.c_str(), prompt.size(), prompt_tokens.data(), prompt_tokens.size(), true,
734-
true) < 0) {
857+
if (llama_tokenize(vocab, prompt.c_str(), prompt.size(), prompt_tokens.data(), prompt_tokens.size(), true, true) <
858+
0) {
735859
printe("failed to tokenize the prompt\n");
736860
return -1;
737861
}

0 commit comments

Comments
 (0)