From 3b482d80e440b6ffbe6a66cc68145f82560073f0 Mon Sep 17 00:00:00 2001 From: mefich Date: Thu, 13 Feb 2025 18:08:24 +0200 Subject: [PATCH 1/2] Add strftime_now to Jijnja2 to use with Granite3 models Granite3 default template uses strftime_now function. Currently Jinja2 raises an exception because strftime_now is undefined and /v1/chat/completions endpoint doesn't work with these models when a template from the model metadata is used. --- common/templating.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/common/templating.py b/common/templating.py index 1200e5c..2134942 100644 --- a/common/templating.py +++ b/common/templating.py @@ -10,6 +10,8 @@ from jinja2.ext import loopcontrols from jinja2.sandbox import ImmutableSandboxedEnvironment from loguru import logger from packaging import version +from datetime import datetime + from common.utils import unwrap @@ -95,10 +97,16 @@ class PromptTemplate: def compile(self, template_str: str): """Compiles and stores a jinja2 template""" + # Some models require strftime_now, e.g. Granite3 + def strftime_now(format): + current_time = datetime.now() + return current_time.strftime(format) + # Exception handler def raise_exception(message): raise TemplateError(message) + self.environment.globals["strftime_now"] = strftime_now self.environment.globals["raise_exception"] = raise_exception return self.environment.from_string(template_str) From 7f6294a96d242a19fb2d2029e843927b6b9abf94 Mon Sep 17 00:00:00 2001 From: kingbri <8082010+kingbri1@users.noreply.github.com> Date: Thu, 13 Feb 2025 22:42:59 -0500 Subject: [PATCH 2/2] Tree: Format Signed-off-by: kingbri <8082010+kingbri1@users.noreply.github.com> --- common/templating.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/templating.py b/common/templating.py index 2134942..8ea4fd1 100644 --- a/common/templating.py +++ b/common/templating.py @@ -97,7 +97,7 @@ class PromptTemplate: def compile(self, template_str: str): """Compiles and stores a jinja2 template""" - # Some models require strftime_now, e.g. Granite3 + # Some models require strftime_now, e.g. Granite3 def strftime_now(format): current_time = datetime.now() return current_time.strftime(format)