Refactor llm_chat_template_from_str to avoid throwing exceptions

This commit is contained in:
AnonN10 2025-11-03 18:46:02 +07:00 committed by GitHub
parent 070ff4d535
commit cc9732e2af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 5 deletions

View File

@ -76,14 +76,15 @@ static const std::map<std::string, llm_chat_template> LLM_CHAT_TEMPLATES = {
};
llm_chat_template llm_chat_template_from_str(const std::string & name) {
return LLM_CHAT_TEMPLATES.at(name);
if (auto it = LLM_CHAT_TEMPLATES.find(name); it != LLM_CHAT_TEMPLATES.end()) {
return it->second;
}
return LLM_CHAT_TEMPLATE_UNKNOWN;
}
llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
try {
return llm_chat_template_from_str(tmpl);
} catch (const std::out_of_range &) {
// ignore
if (auto t = llm_chat_template_from_str(tmpl); t != LLM_CHAT_TEMPLATE_UNKNOWN) {
return t;
}
auto tmpl_contains = [&tmpl](const char * haystack) -> bool {