Skip to content
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
10 changes: 10 additions & 0 deletions llama_cpp/llama_chat_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)

import jinja2
from jinja2.ext import Extension
from jinja2.sandbox import ImmutableSandboxedEnvironment

import numpy as np
Expand Down Expand Up @@ -190,6 +191,14 @@ def __call__(
**kwargs: Any,
) -> ChatFormatterResponse: ...

class GenerationTagIgnore(Extension):
"""Ignores the generation and endgeneration tags in Jinja templates."""

tags = {"generation", "endgeneration"}

def parse(self, parser):
parser.stream.skip(1)
return nodes.Const("")

class Jinja2ChatFormatter(ChatFormatter):
def __init__(
Expand All @@ -213,6 +222,7 @@ def __init__(
loader=jinja2.BaseLoader(),
trim_blocks=True,
lstrip_blocks=True,
extensions=[GenerationTagIgnore]
).from_string(self.template)

@staticmethod
Expand Down