hkimi / src /tokenizer /chat.cpp
Ozomozyan's picture
hkimi 0.1.0
68888be verified
Raw History Blame Contribute Delete
5.69 kB
#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 += "&amp;";
else if (c == '"') o += "&quot;";
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