From a3dbfabe93ff16674b38e75ceb3287adb6e29914 Mon Sep 17 00:00:00 2001 From: ngxson Date: Mon, 24 Jun 2024 10:52:17 +0200 Subject: [PATCH] add llama_chat_format_example --- common/common.cpp | 11 +++++++++++ common/common.h | 4 ++++ examples/main/main.cpp | 2 ++ examples/server/server.cpp | 12 ++---------- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index b1de5615b..54e68accc 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -3020,6 +3020,17 @@ std::string llama_chat_format_single(const struct llama_model * model, return formatted; } +std::string llama_chat_format_example(const struct llama_model * model, + const std::string & tmpl) { + std::vector msgs = { + {"system", "You are a helpful assistant"}, + {"user", "Hello"}, + {"assistant", "Hi there"}, + {"user", "How are you?"}, + }; + return llama_chat_apply_template(model, tmpl, msgs, true); +} + // // KV cache utils // diff --git a/common/common.h b/common/common.h index 6a64bb22b..04cb8c30c 100644 --- a/common/common.h +++ b/common/common.h @@ -382,6 +382,10 @@ std::string llama_chat_format_single(const struct llama_model * model, const llama_chat_msg & new_msg, bool add_ass); +// Returns an example of formatted chat +std::string llama_chat_format_example(const struct llama_model * model, + const std::string & tmpl); + // // KV cache utils // diff --git a/examples/main/main.cpp b/examples/main/main.cpp index e1f0a1a12..f76c885f4 100644 --- a/examples/main/main.cpp +++ b/examples/main/main.cpp @@ -224,6 +224,8 @@ int main(int argc, char ** argv) { __func__, n_ctx_train, n_ctx); } + LOG_TEE("%s: chat template example: %s\n", __func__, llama_chat_format_example(model, params.chat_template).c_str()); + // print system information { LOG_TEE("\n"); diff --git a/examples/server/server.cpp b/examples/server/server.cpp index f9a86961f..3aad57284 100644 --- a/examples/server/server.cpp +++ b/examples/server/server.cpp @@ -2606,17 +2606,9 @@ int main(int argc, char ** argv) { // print sample chat example to make it clear which template is used { - json chat; - chat.push_back({{"role", "system"}, {"content", "You are a helpful assistant"}}); - chat.push_back({{"role", "user"}, {"content", "Hello"}}); - chat.push_back({{"role", "assistant"}, {"content", "Hi there"}}); - chat.push_back({{"role", "user"}, {"content", "How are you?"}}); - - const std::string chat_example = format_chat(ctx_server.model, params.chat_template, chat); - LOG_INFO("chat template", { - {"chat_example", chat_example}, - {"built_in", params.chat_template.empty()}, + {"chat_example", llama_chat_format_example(model)}, + {"built_in", params.chat_template.empty()}, }); }