File size: 5,694 Bytes
68888be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | #include "tokenizer/chat.h"
#include "common/base.h"
namespace hk {
ChatFormat::ChatFormat(const Tokenizer& tok) : tok_(tok) {
open_ = tok.special("<|open|>");
close_ = tok.special("<|close|>");
sep_ = tok.special("<|sep|>");
end_ = tok.special("<|end_of_msg|>");
im_user_ = tok.special("<|im_user|>");
im_asst_ = tok.special("<|im_assistant|>");
im_sys_ = tok.special("<|im_system|>");
im_mid_ = tok.special("<|im_middle|>");
im_end_ = tok.special("<|im_end|>");
eos_ = tok.special("[EOS]");
if (open_ >= 0 && close_ >= 0 && sep_ >= 0 && end_ >= 0) style_ = ChatStyle::Xtml;
else if (im_user_ >= 0 && im_asst_ >= 0 && im_mid_ >= 0 && im_end_ >= 0) style_ = ChatStyle::Im;
else style_ = ChatStyle::Raw;
}
const char* ChatFormat::style_name() const {
return style_ == ChatStyle::Xtml ? "K3 XTML" : style_ == ChatStyle::Im ? "Kimi im-tokens" : "raw text";
}
void ChatFormat::text(const std::string& s, std::vector<int32_t>& out) const {
if (!s.empty()) tok_.encode(s, out);
}
static std::string escape_attr(const std::string& v) {
// '&' first, then '"'. Angle brackets stay: this markup's brackets are tokens, not characters.
std::string o;
for (char c : v) {
if (c == '&') o += "&";
else if (c == '"') o += """;
else o += c;
}
return o;
}
void ChatFormat::open_tag(const char* tag, const char* attr, const std::string& value, std::vector<int32_t>& out) const {
out.push_back(open_);
text(tag, out);
if (attr) {
// four segments, as upstream encodes them
text(std::string(" ") + attr, out);
text("=\"", out);
text(escape_attr(value), out);
text("\"", out);
}
out.push_back(sep_);
}
void ChatFormat::close_tag(const char* tag, std::vector<int32_t>& out) const {
out.push_back(close_);
text(tag, out);
out.push_back(sep_);
}
std::vector<int32_t> ChatFormat::render(const std::vector<ChatMessage>& messages, bool think) const {
std::vector<int32_t> out;
switch (style_) {
case ChatStyle::Xtml:
for (const ChatMessage& m : messages) {
HK_CHECK(m.role == "system" || m.role == "user" || m.role == "assistant", "unknown chat role \"%s\"", m.role.c_str());
open_tag("message", "role", m.role, out);
if (m.role == "assistant") {
// a past assistant turn: with thinking on, upstream always writes the think channel, even empty
if (think) {
open_tag("think", nullptr, "", out);
close_tag("think", out);
}
open_tag("response", nullptr, "", out);
text(m.content, out);
close_tag("response", out);
} else {
text(m.content, out);
}
close_tag("message", out);
out.push_back(end_);
}
// the assistant message is left open
open_tag("message", "role", "assistant", out);
open_tag(think ? "think" : "response", nullptr, "", out);
break;
case ChatStyle::Im:
for (const ChatMessage& m : messages) {
int32_t lead = m.role == "system" ? im_sys_ : m.role == "user" ? im_user_ : im_asst_;
HK_CHECK(lead >= 0, "this tokenizer has no control token for role \"%s\"", m.role.c_str());
out.push_back(lead);
text(m.role, out);
out.push_back(im_mid_);
text(m.content, out);
out.push_back(im_end_);
}
out.push_back(im_asst_);
text("assistant", out);
out.push_back(im_mid_);
break;
case ChatStyle::Raw:
for (const ChatMessage& m : messages) text(m.content, out);
break;
}
return out;
}
std::vector<int32_t> ChatFormat::stop_ids() const {
std::vector<int32_t> v;
if (style_ == ChatStyle::Xtml) v.push_back(end_);
if (style_ == ChatStyle::Im) v.push_back(im_end_);
if (eos_ >= 0) v.push_back(eos_);
return v;
}
ReplyParser::ReplyParser(const Tokenizer& tok, ChatStyle style, bool think)
: tok_(tok), style_(style), channel_(style == ChatStyle::Xtml ? (think ? Thinking : Answer) : Answer) {
open_ = tok.special("<|open|>");
close_ = tok.special("<|close|>");
sep_ = tok.special("<|sep|>");
end_ = tok.special("<|end_of_msg|>");
}
std::string ReplyParser::push(int32_t id, Kind* kind) {
*kind = channel_;
if (style_ != ChatStyle::Xtml) {
if (tok_.is_special(id)) return "";
return utf8_.push(tok_.token(id));
}
if (id == open_ || id == close_) {
in_tag_ = true;
closing_ = id == close_;
tag_.clear();
return utf8_.flush();
}
if (id == sep_ && in_tag_) {
in_tag_ = false;
// tag name = first word of the header
std::string name = tag_.substr(0, tag_.find(' '));
if (!closing_) {
if (name == "think") channel_ = Thinking;
else if (name == "response") channel_ = Answer;
else channel_ = None; // tool calls and anything else: not shown as answer text
} else {
channel_ = None;
}
return "";
}
if (id == end_ || tok_.is_special(id)) return utf8_.flush();
if (in_tag_) {
tag_ += tok_.token(id);
return "";
}
if (channel_ == None) return "";
return utf8_.push(tok_.token(id));
}
std::string ReplyParser::flush() { return utf8_.flush(); }
} // namespace hk
|