perf : 预编译 Jinja2 Template,避免每次 render 重新构建

This commit is contained in:
ViperEkura 2026-05-31 14:50:16 +08:00
parent dbe5891201
commit 14f83cbdac
1 changed files with 17 additions and 20 deletions

View File

@ -1,13 +1,10 @@
from dataclasses import dataclass
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
from jinja2 import Template from jinja2 import Template
# Message type for chat messages
type MessageType = Dict[str, Any] type MessageType = Dict[str, Any]
@dataclass
class ChatTemplate: class ChatTemplate:
"""A chat template with Jinja2 rendering support. """A chat template with Jinja2 rendering support.
@ -15,23 +12,24 @@ class ChatTemplate:
name: Unique identifier for the template. name: Unique identifier for the template.
template_str: Jinja2 template string. template_str: Jinja2 template string.
description: Optional description. description: Optional description.
default_variables: Optional dictionary of default variable values default_variables: Optional dictionary of default variable values.
that will be passed to the template if not overridden during rendering.
special_tokens: Optional dictionary mapping token names to their string values. special_tokens: Optional dictionary mapping token names to their string values.
These tokens are automatically added to the template variables.
""" """
name: str def __init__(
template_str: str self,
description: str = "" name: str = "",
default_variables: Dict[str, Any] = None template_str: str = "",
special_tokens: Dict[str, str] = None description: str = "",
default_variables: Optional[Dict[str, Any]] = None,
def __post_init__(self): special_tokens: Optional[Dict[str, str]] = None,
if self.default_variables is None: ):
self.default_variables = {} self.name = name
if self.special_tokens is None: self.template_str = template_str
self.special_tokens = {} self.description = description
self.default_variables = default_variables or {}
self.special_tokens = special_tokens or {}
self._compiled : Template = Template(template_str)
@classmethod @classmethod
def from_string( def from_string(
@ -43,7 +41,7 @@ class ChatTemplate:
) -> "ChatTemplate": ) -> "ChatTemplate":
"""Create a ChatTemplate instance directly from a template string.""" """Create a ChatTemplate instance directly from a template string."""
return cls( return cls(
name="", # empty name for adhoc templates name="",
template_str=template_str, template_str=template_str,
description=description, description=description,
default_variables=default_variables, default_variables=default_variables,
@ -73,5 +71,4 @@ class ChatTemplate:
if system_prompt is not None: if system_prompt is not None:
variables["system_prompt"] = system_prompt variables["system_prompt"] = system_prompt
jinja_template = Template(self.template_str) return self._compiled.render(**variables)
return jinja_template.render(**variables)