From 4567309f1ef771e9ca4d5ad283e774b9bfb9a061 Mon Sep 17 00:00:00 2001 From: Hidehiro Anto Date: Tue, 21 Oct 2025 07:02:49 -0700 Subject: [PATCH 1/2] Implement GenerationTagIgnore Jinja2 extension Added a custom Jinja2 extension to ignore generation tags in templates. --- llama_cpp/llama_chat_format.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/llama_cpp/llama_chat_format.py b/llama_cpp/llama_chat_format.py index f738ab9bb..9f83dd154 100644 --- a/llama_cpp/llama_chat_format.py +++ b/llama_cpp/llama_chat_format.py @@ -24,6 +24,7 @@ ) import jinja2 +from jinja2.ext import Extension from jinja2.sandbox import ImmutableSandboxedEnvironment import numpy as np @@ -190,6 +191,14 @@ def __call__( **kwargs: Any, ) -> ChatFormatterResponse: ... +class GenerationTagIgnore(Extension): + """Ignores the generation and endgeneration tags in Jinja templates.""" + + tags = {"generation"} + + def parse(self, parser): + parser.stream.skip(1) + return nodes.Const("") class Jinja2ChatFormatter(ChatFormatter): def __init__( @@ -213,6 +222,7 @@ def __init__( loader=jinja2.BaseLoader(), trim_blocks=True, lstrip_blocks=True, + extensions=[GenerationTagIgnore] ).from_string(self.template) @staticmethod From e1f9c78abfe763980176348dea63c079ca33478e Mon Sep 17 00:00:00 2001 From: Hidehiro Anto Date: Tue, 21 Oct 2025 07:10:16 -0700 Subject: [PATCH 2/2] Add 'endgeneration' tag to GenerationTagIgnore --- llama_cpp/llama_chat_format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llama_cpp/llama_chat_format.py b/llama_cpp/llama_chat_format.py index 9f83dd154..5e43a781f 100644 --- a/llama_cpp/llama_chat_format.py +++ b/llama_cpp/llama_chat_format.py @@ -194,7 +194,7 @@ def __call__( class GenerationTagIgnore(Extension): """Ignores the generation and endgeneration tags in Jinja templates.""" - tags = {"generation"} + tags = {"generation", "endgeneration"} def parse(self, parser): parser.stream.skip(1)