@@ -187,16 +187,8 @@ void llamaCPP::chatCompletion(
187187 data[" presence_penalty" ] = (*jsonBody).get (" presence_penalty" , 0 ).asFloat ();
188188 const Json::Value &messages = (*jsonBody)[" messages" ];
189189
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- }
190+ if (!grammar_file_content.empty ()) {
191+ data[" grammar" ] = grammar_file_content;
200192 };
201193
202194 if (!llama.multimodal ) {
@@ -514,6 +506,19 @@ bool llamaCPP::loadModelImpl(const Json::Value &jsonBody) {
514506 if (!jsonBody[" mlock" ].isNull ()) {
515507 params.use_mlock = jsonBody[" mlock" ].asBool ();
516508 }
509+
510+ if (!jsonBody[" grammar_file" ].isNull ()) {
511+ std::string grammar_file = jsonBody[" grammar_file" ].asString ();
512+ std::ifstream file (grammar_file);
513+ if (!file) {
514+ LOG_ERROR << " Grammar file not found" ;
515+ } else {
516+ std::stringstream grammarBuf;
517+ grammarBuf << file.rdbuf ();
518+ grammar_file_content = grammarBuf.str ();
519+ }
520+ };
521+
517522 params.model = jsonBody[" llama_model_path" ].asString ();
518523 params.n_gpu_layers = jsonBody.get (" ngl" , 100 ).asInt ();
519524 params.n_ctx = jsonBody.get (" ctx_len" , 2048 ).asInt ();
0 commit comments