ember.cpp 0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
Loading...
Searching...
No Matches
microtorch::chat::Template Struct Reference

#include <chat_template.h>

Collaboration diagram for microtorch::chat::Template:
Collaboration graph

Public Member Functions

std::string id () const
 
json to_json () const
 

Static Public Member Functions

template<class J >
static Template from_json (const J &r)
 

Public Attributes

std::string name
 
json version
 
std::string bos
 
std::string content = "raw"
 
std::string system_mode = "none"
 
std::string system_join
 
std::string system_open
 
std::string system_close
 
std::string user_open
 
std::string user_close
 
std::string assistant_open
 
std::string assistant_close
 
std::string generation_prompt
 
bool has_nothink = false
 
std::string generation_prompt_nothink
 
std::vector< std::string > stop_eos
 
std::vector< std::string > stop_turn
 
std::string past_assistant = "keep"
 
bool has_default_system = false
 
std::string default_system
 
std::vector< std::string > reserved
 
std::vector< std::string > markers
 
std::vector< std::string > match
 
std::vector< std::string > architectures
 
bool has_tool = false
 
bool has_tools = false
 
std::string tool_open
 
std::string tool_close
 
std::string tool_call_open
 
std::string tool_call_join
 
std::string tools_join
 
std::string tools_sep
 

Detailed Description

Definition at line 89 of file chat_template.h.

Member Function Documentation

◆ from_json()

template<class J >
static Template microtorch::chat::Template::from_json ( const J &  r)
inlinestatic

Definition at line 141 of file chat_template.h.

141 {
142 static const char* known[] = {"name", "version", "bos", "content", "system", "user", "assistant",
143 "generation_prompt", "generation_prompt_nothink", "stop", "past_assistant",
144 "default_system", "reserved", "markers", "match", "architectures",
145 "tool", "tool_call", "tools"};
146 if (!r.is_object()) throw Error("chat template: the record is not a JSON object");
147 for (auto it = r.begin(); it != r.end(); ++it) {
148 const std::string k = it.key();
149 bool ok = !k.empty() && k[0] == '_';
150 for (const char* q : known) ok = ok || k == q;
151 if (!ok) throw Error("chat template field '" + k + "' is not implemented");
152 }
153 for (const char* k : {"name", "version", "user", "assistant", "generation_prompt", "stop"})
154 if (!r.contains(k)) throw Error(std::string("chat template has no '") + k + "'");
155 Template t;
156 t.name = r["name"].template get<std::string>();
157 if (!r["version"].is_string() && !r["version"].is_number_integer())
158 throw Error("chat template version must be an integer or a string");
159 t.version = json::parse(r["version"].dump());
160 t.bos = r.value("bos", std::string());
161 t.content = r.value("content", std::string("raw"));
162 if (t.content != "raw" && t.content != "trim")
163 throw Error("chat template content '" + t.content + "' is not implemented (raw, trim)");
164 if (r.contains("system")) {
165 const auto& s = r["system"];
166 t.system_mode = s.value("mode", std::string("none"));
167 if (t.system_mode != "fold" && t.system_mode != "turn" && t.system_mode != "none")
168 throw Error("chat template system mode '" + t.system_mode + "' is not implemented (fold, turn, none)");
169 t.system_join = s.value("join", std::string());
170 t.system_open = s.value("open", std::string());
171 t.system_close = s.value("close", std::string());
172 }
173 auto side = [&](const char* k, std::string& open, std::string& close) {
174 const auto& s = r[k];
175 for (auto it = s.begin(); it != s.end(); ++it)
176 if (it.key() != "open" && it.key() != "close")
177 throw Error(std::string("chat template ") + k + " takes open and close only");
178 open = s.value("open", std::string());
179 close = s.value("close", std::string());
180 };
181 side("user", t.user_open, t.user_close);
182 side("assistant", t.assistant_open, t.assistant_close);
183 auto pair = [&](const char* k, const char* a, const char* b, std::string& x, std::string& y) {
184 const auto& s = r[k];
185 if (!s.is_object()) throw Error(std::string("chat template ") + k + " is an object");
186 for (auto it = s.begin(); it != s.end(); ++it)
187 if (it.key() != a && it.key() != b)
188 throw Error(std::string("chat template ") + k + " takes " + a + " and " + b + " only");
189 x = s.value(a, std::string());
190 y = s.value(b, std::string());
191 };
192 if (r.contains("tool") != r.contains("tool_call")) throw Error("chat template tool and tool_call come together");
193 if (r.contains("tools") && !r.contains("tool")) throw Error("chat template tools needs tool and tool_call");
194 if (r.contains("tool")) {
195 if (t.system_mode != "turn") throw Error("chat template tool turns need system mode turn");
196 t.has_tool = true;
197 pair("tool", "open", "close", t.tool_open, t.tool_close);
198 pair("tool_call", "open", "join", t.tool_call_open, t.tool_call_join);
199 }
200 if (r.contains("tools")) {
201 t.has_tools = true;
202 pair("tools", "join", "sep", t.tools_join, t.tools_sep);
203 }
204 t.generation_prompt = r["generation_prompt"].template get<std::string>();
205 if (r.contains("generation_prompt_nothink")) {
206 t.has_nothink = true;
207 t.generation_prompt_nothink = r["generation_prompt_nothink"].template get<std::string>();
208 }
209 auto strs = [&](const J& a) {
210 std::vector<std::string> v;
211 for (const auto& x : a) v.push_back(x.template get<std::string>());
212 return v;
213 };
214 t.stop_eos = strs(r["stop"].value("eos", J::array()));
215 t.stop_turn = strs(r["stop"].value("turn", J::array()));
216 t.past_assistant = r.value("past_assistant", std::string("keep"));
217 if (t.past_assistant != "keep" && t.past_assistant != "strip_think")
218 throw Error("chat template past_assistant '" + t.past_assistant + "' is not implemented");
219 if (r.contains("default_system")) {
220 t.has_default_system = true;
221 t.default_system = r["default_system"].template get<std::string>();
222 }
223 if (r.contains("reserved")) t.reserved = strs(r["reserved"]);
224 if (r.contains("markers")) t.markers = strs(r["markers"]);
225 if (r.contains("match")) t.match = strs(r["match"]);
226 if (r.contains("architectures")) t.architectures = strs(r["architectures"]);
227 return t;
228 }
float value
std::vector< float > v
std::vector< E > a
size_t j
Template get(const std::string &id)

References a, j, k, name, v, and value.

◆ id()

std::string microtorch::chat::Template::id ( ) const
inline

Definition at line 108 of file chat_template.h.

108{ return name + "/" + (version.is_string() ? version.get<std::string>() : version.dump()); }

References name, and version.

Referenced by microtorch::chat::get(), and tinyllama::TinyLlamaSession::TinyLlamaSession().

◆ to_json()

json microtorch::chat::Template::to_json ( ) const
inline

Definition at line 111 of file chat_template.h.

111 {
112 json r = {{"name", name}, {"version", version}, {"bos", bos}, {"content", content}};
113 json s = {{"mode", system_mode}};
114 if (system_mode == "fold") s["join"] = system_join;
115 if (system_mode == "turn") {
116 s["open"] = system_open;
117 s["close"] = system_close;
118 }
119 r["system"] = s;
120 r["user"] = {{"open", user_open}, {"close", user_close}};
121 r["assistant"] = {{"open", assistant_open}, {"close", assistant_close}};
122 if (has_tool) {
123 r["tool"] = {{"open", tool_open}, {"close", tool_close}};
124 r["tool_call"] = {{"open", tool_call_open}, {"join", tool_call_join}};
125 }
126 if (has_tools) r["tools"] = {{"join", tools_join}, {"sep", tools_sep}};
127 r["generation_prompt"] = generation_prompt;
128 r["stop"] = {{"eos", stop_eos}, {"turn", stop_turn}};
129 r["past_assistant"] = past_assistant;
130 r["reserved"] = reserved;
131 if (!markers.empty()) r["markers"] = markers;
132 if (has_default_system) r["default_system"] = default_system;
133 if (has_nothink) r["generation_prompt_nothink"] = generation_prompt_nothink;
134 if (!match.empty()) r["match"] = match;
135 if (!architectures.empty()) r["architectures"] = architectures;
136 return r;
137 }
nlohmann::json json
Definition server.cpp:68
std::vector< std::string > stop_eos
std::vector< std::string > reserved
std::string generation_prompt_nothink
std::vector< std::string > architectures
std::vector< std::string > match
std::vector< std::string > markers
std::vector< std::string > stop_turn

References architectures, assistant_close, assistant_open, bos, content, default_system, generation_prompt, generation_prompt_nothink, has_default_system, has_nothink, has_tool, has_tools, j, markers, match, name, past_assistant, reserved, stop_eos, stop_turn, system_close, system_join, system_mode, system_open, tool_call_join, tool_call_open, tool_close, tool_open, tools_join, tools_sep, user_close, user_open, and version.

Member Data Documentation

◆ architectures

std::vector<std::string> microtorch::chat::Template::architectures

Definition at line 102 of file chat_template.h.

Referenced by to_json().

◆ assistant_close

std::string microtorch::chat::Template::assistant_close

Definition at line 94 of file chat_template.h.

Referenced by to_json(), and ember::ChatFormat::v1_markers().

◆ assistant_open

std::string microtorch::chat::Template::assistant_open

Definition at line 94 of file chat_template.h.

Referenced by to_json(), and ember::ChatFormat::v1_markers().

◆ bos

std::string microtorch::chat::Template::bos

Definition at line 92 of file chat_template.h.

Referenced by to_json(), and ember::ChatFormat::v1_markers().

◆ content

std::string microtorch::chat::Template::content = "raw"

Definition at line 92 of file chat_template.h.

Referenced by to_json(), and ember::ChatFormat::v1_markers().

◆ default_system

std::string microtorch::chat::Template::default_system

Definition at line 101 of file chat_template.h.

Referenced by to_json().

◆ generation_prompt

std::string microtorch::chat::Template::generation_prompt

Definition at line 95 of file chat_template.h.

Referenced by to_json(), and ember::ChatFormat::v1_markers().

◆ generation_prompt_nothink

std::string microtorch::chat::Template::generation_prompt_nothink

Definition at line 97 of file chat_template.h.

Referenced by to_json().

◆ has_default_system

bool microtorch::chat::Template::has_default_system = false

Definition at line 100 of file chat_template.h.

Referenced by to_json().

◆ has_nothink

bool microtorch::chat::Template::has_nothink = false

Definition at line 96 of file chat_template.h.

Referenced by to_json().

◆ has_tool

bool microtorch::chat::Template::has_tool = false

Definition at line 105 of file chat_template.h.

Referenced by main(), and to_json().

◆ has_tools

bool microtorch::chat::Template::has_tools = false

Definition at line 105 of file chat_template.h.

Referenced by to_json().

◆ markers

std::vector<std::string> microtorch::chat::Template::markers

Definition at line 102 of file chat_template.h.

Referenced by to_json().

◆ match

std::vector<std::string> microtorch::chat::Template::match

Definition at line 102 of file chat_template.h.

Referenced by to_json().

◆ name

◆ past_assistant

std::string microtorch::chat::Template::past_assistant = "keep"

Definition at line 99 of file chat_template.h.

Referenced by to_json().

◆ reserved

std::vector<std::string> microtorch::chat::Template::reserved

Definition at line 102 of file chat_template.h.

Referenced by to_json().

◆ stop_eos

std::vector<std::string> microtorch::chat::Template::stop_eos

Definition at line 98 of file chat_template.h.

Referenced by to_json(), and ember::ChatFormat::v1_markers().

◆ stop_turn

std::vector<std::string> microtorch::chat::Template::stop_turn

Definition at line 98 of file chat_template.h.

Referenced by to_json(), and ember::ChatFormat::v1_markers().

◆ system_close

std::string microtorch::chat::Template::system_close

Definition at line 93 of file chat_template.h.

Referenced by to_json().

◆ system_join

std::string microtorch::chat::Template::system_join

Definition at line 93 of file chat_template.h.

Referenced by to_json().

◆ system_mode

std::string microtorch::chat::Template::system_mode = "none"

Definition at line 93 of file chat_template.h.

Referenced by to_json(), and ember::ChatFormat::v1_markers().

◆ system_open

std::string microtorch::chat::Template::system_open

Definition at line 93 of file chat_template.h.

Referenced by to_json().

◆ tool_call_join

std::string microtorch::chat::Template::tool_call_join

Definition at line 106 of file chat_template.h.

Referenced by to_json().

◆ tool_call_open

std::string microtorch::chat::Template::tool_call_open

Definition at line 106 of file chat_template.h.

Referenced by to_json().

◆ tool_close

std::string microtorch::chat::Template::tool_close

Definition at line 106 of file chat_template.h.

Referenced by to_json().

◆ tool_open

std::string microtorch::chat::Template::tool_open

Definition at line 106 of file chat_template.h.

Referenced by to_json().

◆ tools_join

std::string microtorch::chat::Template::tools_join

Definition at line 106 of file chat_template.h.

Referenced by to_json().

◆ tools_sep

std::string microtorch::chat::Template::tools_sep

Definition at line 106 of file chat_template.h.

Referenced by to_json().

◆ user_close

std::string microtorch::chat::Template::user_close

Definition at line 94 of file chat_template.h.

Referenced by to_json(), and ember::ChatFormat::v1_markers().

◆ user_open

std::string microtorch::chat::Template::user_open

Definition at line 94 of file chat_template.h.

Referenced by to_json(), and ember::ChatFormat::v1_markers().

◆ version

json microtorch::chat::Template::version

Definition at line 91 of file chat_template.h.

Referenced by id(), to_json(), and ember::ChatFormat::v1_markers().


The documentation for this struct was generated from the following file: