Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 626c753

Browse files
authored
Merge pull request #398 from janhq/397-grammar-file-not-found-llamacppcc201
bug: correct way of handle grammar file
2 parents 5ce716b + ef16ba7 commit 626c753

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

controllers/llamaCPP.cc

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,19 @@ void llamaCPP::chatCompletion(
186186
(*jsonBody).get("frequency_penalty", 0).asFloat();
187187
data["presence_penalty"] = (*jsonBody).get("presence_penalty", 0).asFloat();
188188
const Json::Value &messages = (*jsonBody)["messages"];
189-
std::string grammar_file = (*jsonBody).get("grammar_file", "").asString();
190-
std::ifstream file(grammar_file);
191-
if (!file) {
192-
LOG_ERROR << "Grammar file not found";
193-
} else {
194-
std::stringstream grammarBuf;
195-
grammarBuf << file.rdbuf();
196-
data["grammar"] = grammarBuf.str();
197-
}
189+
190+
if (!(*jsonBody)["grammar_file"].isNull()) {
191+
std::string grammar_file = (*jsonBody)["grammar_file"].asString();
192+
std::ifstream file(grammar_file);
193+
if (!file) {
194+
LOG_ERROR << "Grammar file not found";
195+
} else {
196+
std::stringstream grammarBuf;
197+
grammarBuf << file.rdbuf();
198+
data["grammar"] = grammarBuf.str();
199+
}
200+
};
201+
198202
if (!llama.multimodal) {
199203

200204
for (const auto &message : messages) {

0 commit comments

Comments
 (0)