diff --git a/src/llm/language_model/continuous_batching/servable_initializer.cpp b/src/llm/language_model/continuous_batching/servable_initializer.cpp index 27f4f51aee..c510b2b91c 100644 --- a/src/llm/language_model/continuous_batching/servable_initializer.cpp +++ b/src/llm/language_model/continuous_batching/servable_initializer.cpp @@ -210,6 +210,21 @@ Status ContinuousBatchingServableInitializer::initialize(std::shared_ptrschedulerConfig, properties->device, properties->pluginConfig, properties->tokenizerPluginConfig); properties->tokenizer = properties->pipeline->get_tokenizer(); + + // Override chat template from chat_template.jinja file if present in model directory + std::filesystem::path chatTemplateJinjaPath = std::filesystem::path(parsedModelsPath) / "chat_template.jinja"; + if (std::filesystem::exists(chatTemplateJinjaPath)) { + std::ifstream chatTemplateFile(chatTemplateJinjaPath); + if (chatTemplateFile.is_open()) { + std::string chatTemplateContent((std::istreambuf_iterator(chatTemplateFile)), + std::istreambuf_iterator()); + if (!chatTemplateContent.empty()) { + properties->tokenizer.set_chat_template(chatTemplateContent); + } + } else { + SPDLOG_LOGGER_WARN(llm_calculator_logger, "Failed to open chat template file: {}", chatTemplateJinjaPath.string()); + } + } } catch (const std::exception& e) { SPDLOG_ERROR("Error during llm node initialization for models_path: {} exception: {}", parsedModelsPath, e.what()); return StatusCode::LLM_NODE_RESOURCE_STATE_INITIALIZATION_FAILED; diff --git a/src/llm/language_model/legacy/servable_initializer.cpp b/src/llm/language_model/legacy/servable_initializer.cpp index 4ee7d4820a..856a2b8902 100644 --- a/src/llm/language_model/legacy/servable_initializer.cpp +++ b/src/llm/language_model/legacy/servable_initializer.cpp @@ -13,6 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. //***************************************************************************** +#include #include #include #include @@ -100,6 +101,22 @@ Status LegacyServableInitializer::initialize(std::shared_ptr& ser try { properties->pipeline = std::make_shared(parsedModelsPath, properties->device, properties->pluginConfig); properties->tokenizer = properties->pipeline->get_tokenizer(); + + // Override chat template from chat_template.jinja file if present in model directory + std::filesystem::path chatTemplateJinjaPath = std::filesystem::path(parsedModelsPath) / "chat_template.jinja"; + if (std::filesystem::exists(chatTemplateJinjaPath)) { + std::ifstream chatTemplateFile(chatTemplateJinjaPath); + if (chatTemplateFile.is_open()) { + std::string chatTemplateContent((std::istreambuf_iterator(chatTemplateFile)), + std::istreambuf_iterator()); + if (!chatTemplateContent.empty()) { + properties->tokenizer.set_chat_template(chatTemplateContent); + SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Loaded custom chat template from: {}", chatTemplateJinjaPath.string()); + } + } else { + SPDLOG_LOGGER_WARN(llm_calculator_logger, "Failed to open chat template file: {}", chatTemplateJinjaPath.string()); + } + } } catch (const std::exception& e) { SPDLOG_ERROR("Error during llm node initialization for models_path: {} exception: {}", parsedModelsPath, e.what()); return StatusCode::LLM_NODE_RESOURCE_STATE_INITIALIZATION_FAILED; diff --git a/src/llm/visual_language_model/legacy/servable_initializer.cpp b/src/llm/visual_language_model/legacy/servable_initializer.cpp index ec8bfd327a..6ed34cc7d0 100644 --- a/src/llm/visual_language_model/legacy/servable_initializer.cpp +++ b/src/llm/visual_language_model/legacy/servable_initializer.cpp @@ -13,6 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. //***************************************************************************** +#include #include #include #include @@ -84,6 +85,22 @@ Status VisualLanguageModelLegacyServableInitializer::initialize(std::shared_ptr< try { properties->pipeline = std::make_shared(parsedModelsPath, properties->device, properties->pluginConfig); properties->tokenizer = properties->pipeline->get_tokenizer(); + + // Override chat template from chat_template.jinja file if present in model directory + std::filesystem::path chatTemplateJinjaPath = std::filesystem::path(parsedModelsPath) / "chat_template.jinja"; + if (std::filesystem::exists(chatTemplateJinjaPath)) { + std::ifstream chatTemplateFile(chatTemplateJinjaPath); + if (chatTemplateFile.is_open()) { + std::string chatTemplateContent((std::istreambuf_iterator(chatTemplateFile)), + std::istreambuf_iterator()); + if (!chatTemplateContent.empty()) { + properties->tokenizer.set_chat_template(chatTemplateContent); + SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Loaded custom chat template from: {}", chatTemplateJinjaPath.string()); + } + } else { + SPDLOG_LOGGER_WARN(llm_calculator_logger, "Failed to open chat template file: {}", chatTemplateJinjaPath.string()); + } + } } catch (const std::exception& e) { SPDLOG_ERROR("Error during llm node initialization for models_path: {} exception: {}", parsedModelsPath, e.what()); return StatusCode::LLM_NODE_RESOURCE_STATE_INITIALIZATION_FAILED;