-
Notifications
You must be signed in to change notification settings - Fork 12k
server: add llama2 chat template #5425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2ebedda
27976c3
269437e
7efef47
ebe3079
1a27406
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there should be a space in front of the eos token. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed in #5425 (comment) , the space is not consistent among different variations of this template. I leave it here to be sure. It changes nothing on the behavior of the model. Ideally if I had access to And also, what is important in this template is the placement of As long as the model see |
||
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(); | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.