Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat template rendering extensions to match transformers #1486

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions llama_cpp/llama_chat_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import string

from contextlib import ExitStack
from datetime import datetime
from typing import (
Any,
Dict,
Expand All @@ -23,6 +24,7 @@
)

import jinja2
import jinja2.ext as jinja2_ext
from jinja2.sandbox import ImmutableSandboxedEnvironment

import numpy as np
Expand Down Expand Up @@ -207,11 +209,14 @@ def __init__(
set(stop_token_ids) if stop_token_ids is not None else None
)

self._environment = ImmutableSandboxedEnvironment(
loader=jinja2.BaseLoader(),
environment = ImmutableSandboxedEnvironment(
trim_blocks=True,
lstrip_blocks=True,
).from_string(self.template)
extensions=[jinja2_ext.loopcontrols],
)
environment.filters["tojson"] = lambda x, indent=None, separators=None, sort_keys=False: json.dumps(x, indent=indent, separators=separators, sort_keys=sort_keys, ensure_ascii=False)
environment.globals["strftime_now"] = lambda format: datetime.now().strftime(format)
self._environment = environment.from_string(self.template)

def __call__(
self,
Expand Down