Skip to content

Commit

Permalink
Merge pull request #281 from mefich/main
Browse files Browse the repository at this point in the history
Add strftime_now to Jinja2 to use with Granite3 models
  • Loading branch information
kingbri1 authored Feb 14, 2025
2 parents 2e49147 + 7f6294a commit ba9fae8
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions common/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from jinja2.sandbox import ImmutableSandboxedEnvironment
from loguru import logger
from packaging import version
from datetime import datetime


from common.utils import unwrap

Expand Down Expand Up @@ -95,10 +97,16 @@ async def render(self, template_vars: dict):
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)
Expand Down

0 comments on commit ba9fae8

Please sign in to comment.