summaryrefslogtreecommitdiff
path: root/examples/server/utils.hpp
diff options
context:
space:
mode:
authorXuan Son Nguyen <thichthat@gmail.com>2024-02-11 11:16:22 +0100
committerGitHub <noreply@github.com>2024-02-11 12:16:22 +0200
commit907e08c1109f498b01036367804cff3082c44524 (patch)
tree333330e10039769bb10fa2f387c39a5ec2c6d98e /examples/server/utils.hpp
parentf026f8120f97090d34a52b3dc023c82e0ede3f7d (diff)
server : add llama2 chat template (#5425)
* server: add mistral chat template * server: fix typo * server: rename template mistral to llama2 * server: format_llama2: remove BOS * server: validate "--chat-template" argument * server: clean up using_chatml variable Co-authored-by: Jared Van Bortel <cebtenzzre@gmail.com> --------- Co-authored-by: Jared Van Bortel <cebtenzzre@gmail.com>
Diffstat (limited to 'examples/server/utils.hpp')
-rw-r--r--examples/server/utils.hpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/examples/server/utils.hpp b/examples/server/utils.hpp
index 70cce072..54854896 100644
--- a/examples/server/utils.hpp
+++ b/examples/server/utils.hpp
@@ -167,6 +167,34 @@ static T json_value(const json &body, const std::string &key, const T &default_v
: default_value;
}
+inline std::string format_llama2(std::vector<json> messages)
+{
+ std::ostringstream output;
+ bool is_inside_turn = false;
+
+ for (auto it = messages.begin(); it != messages.end(); ++it) {
+ if (!is_inside_turn) {
+ output << "[INST] ";
+ }
+ std::string role = json_value(*it, "role", std::string("user"));
+ std::string content = json_value(*it, "content", std::string(""));
+ if (role == "system") {
+ output << "<<SYS>>\n" << content << "\n<<SYS>>\n\n";
+ is_inside_turn = true;
+ } else if (role == "user") {
+ output << content << " [/INST]";
+ is_inside_turn = true;
+ } else {
+ output << " " << content << " </s>";
+ is_inside_turn = false;
+ }
+ }
+
+ LOG_VERBOSE("format_llama2", {{"text", output.str()}});
+
+ return output.str();
+}
+
inline std::string format_chatml(std::vector<json> messages)
{
std::ostringstream chatml_msgs;
@@ -180,6 +208,8 @@ inline std::string format_chatml(std::vector<json> messages)
chatml_msgs << "<|im_start|>assistant" << '\n';
+ LOG_VERBOSE("format_chatml", {{"text", chatml_msgs.str()}});
+
return chatml_msgs.str();
}