Skip to content

Commit cc9732e

Browse files
authored
Refactor llm_chat_template_from_str to avoid throwing exceptions
1 parent 070ff4d commit cc9732e

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/llama-chat.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ static const std::map<std::string, llm_chat_template> LLM_CHAT_TEMPLATES = {
7676
};
7777

7878
llm_chat_template llm_chat_template_from_str(const std::string & name) {
79-
return LLM_CHAT_TEMPLATES.at(name);
79+
if (auto it = LLM_CHAT_TEMPLATES.find(name); it != LLM_CHAT_TEMPLATES.end()) {
80+
return it->second;
81+
}
82+
return LLM_CHAT_TEMPLATE_UNKNOWN;
8083
}
8184

8285
llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
83-
try {
84-
return llm_chat_template_from_str(tmpl);
85-
} catch (const std::out_of_range &) {
86-
// ignore
86+
if (auto t = llm_chat_template_from_str(tmpl); t != LLM_CHAT_TEMPLATE_UNKNOWN) {
87+
return t;
8788
}
8889

8990
auto tmpl_contains = [&tmpl](const char * haystack) -> bool {

0 commit comments

Comments
 (0)