8000 Added --chat-template-file to llama-run · ggml-org/llama.cpp@6e50443 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e50443

Browse files
committed
Added --chat-template-file to llama-run
Relates to: #11178 Added --chat-template-file CLI option to llama-run. If specified, the file will be read and the content passed for overwriting the chat template of the model to common_chat_templates_from_model. Signed-off-by: Michael Engel <mengel@redhat.com>
1 parent d07c621 commit 6e50443

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

examples/run/run.cpp

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class Opt {
113113
llama_context_params ctx_params;
114114
llama_model_params model_params;
115115
std::string model_;
116+
std::string chat_template_file;
116117
std::string user;
117118
bool use_jinja = false;
118119
int context_size = -1, ngl = -1;
@@ -148,6 +149,16 @@ class Opt {
148149
return 0;
149150
}
150151

152+
int handle_option_with_value(int argc, const char ** argv, int & i, std::string & option_value) {
153+
if (i + 1 >= argc) {
154+
return 1;
155+
}
156+
157+
option_value = argv[++i];
158+
159+
return 0;
160+
}
161+
151162
int parse(int argc, const char ** argv) {
152163
bool options_parsing = true;
153164
for (int i = 1, positional_args_i = 0; i < argc; ++i) {
@@ -169,6 +180,11 @@ class Opt {
169180
verbose = true;
170181
} else if (options_parsing && strcmp(argv[i], "--jinja") == 0) {
171182
use_jinja = true;
183+
} else if (options_parsing && strcmp(argv[i], "--chat-template-file") == 0){
184+
if (handle_option_with_value(argc, argv, i, chat_template_file) == 1) {
185+
return 1;
186+
}
187+
use_jinja = true;
172188
} else if (options_parsing && parse_flag(argv, i, "-h", "--help")) {
173189
help = true;
174190
return 0;
@@ -207,6 +223,11 @@ class Opt {
207223
"Options:\n"
208224
" -c, --context-size <value>\n"
209225
" Context size (default: %d)\n"
226+
" --chat-template-file <path>\n"
227+
" Path to the file containing the chat template to use with the model.\n"
228+
" Only supports jinja templates and implicitly sets the --jinja flag.\n"
229+
" --jinja\n"
230+
" Use jinja templating for the chat template of the model\n"
210231
" -n, -ngl, --ngl <value>\n"
211232
" Number of GPU layers (default: %d)\n"
212233
" --temp <value>\n"
@@ -1053,11 +1074,43 @@ static int get_user_input(std::string & user_input, const std::string & user) {
10531074
return 0;
10541075
}
10551076

1077+
// Reads a chat template file to be used
1078+
static std::string read_chat_template_file(const std::string & chat_template_file) {
1079+
if(chat_template_file.empty()){
1080+
return "";
1081+
}
1082+
1083+
FILE* file = ggml_fopen(chat_template_file.c_str(), "r");
1084+
if (!file) {
1085+
std::cerr << "Error opening chat template file '" << chat_template_file << "': " << strerror(errno) << "\n";
1086+
return "";
1087+
}
1088+
1089+
fseek(file, 0, SEEK_END);
1090+
size_t size = ftell(file);
1091+
fseek(file, 0, SEEK_SET);
1092+
1093+
std::vector<unsigned char> data(size);
1094+
size_t read_size = fread(data.data(), 1, size, file);
1095+
fclose(file);
1096+
if (read_size != size) {
1097+
std::cerr << "Error reading chat template file '" << chat_template_file << "': " << strerror(errno) << "\n";
1098+
return "";
1099+
}
1100+
return std::string(data.begin(), data.end());
1101+
}
1102+
10561103
// Main chat loop function
1057-
static int chat_loop(LlamaData & llama_data, const std::string & user, bool use_jinja) {
1104+
static int chat_loop(LlamaData & llama_data, const std::string & user, const std::string & chat_template_file, bool use_jinja) {
10581105
int prev_len = 0;
10591106
llama_data.fmtted.resize(llama_n_ctx(llama_data.context.get()));
1060-
auto chat_templates = common_chat_templates_init(llama_data.model.get(), "");
1107+
1108+
std::string chat_template = "";
1109+
if(!chat_template_file.empty()){
1110+
chat_template = read_chat_template_file(chat_template_file);
1111+
}
1112+
auto chat_templates = common_chat_templates_init(llama_data.model.get(), chat_template);
1113+
10611114
static const bool stdout_a_terminal = is_stdout_a_terminal();
10621115
while (true) {
10631116
// Get user input
@@ -1143,7 +1196,7 @@ int main(int argc, const char ** argv) {
11431196
return 1;
11441197
}
11451198

1146-
if (chat_loop(llama_data, opt.user, opt.use_jinja)) {
1199+
if (chat_loop(llama_data, opt.user, opt.chat_template_file, opt.use_jinja)) {
11471200
return 1;
11481201
}
11491202

0 commit comments

Comments
 (0)
0