E5DC Render templates in immutable sandbox by CISC · Pull Request #1441 · abetlen/llama-cpp-python · GitHub
[go: up one dir, main page]

Skip to content
Merged
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: 6 additions & 5 deletions llama_cpp/llama_chat_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,7 @@ def hf_tokenizer_config_to_chat_formatter(
assert isinstance(tokenizer_config["eos_token"], str)
eos_token = tokenizer_config["eos_token"]

env = jinja2.Environment(
loader=jinja2.BaseLoader(),
env = ImmutableSandboxedEnvironment(
trim_blocks=True,
lstrip_blocks=True,
).from_string(chat_template)
Expand Down Expand Up @@ -2603,7 +2602,10 @@ def __call__(
messages = [llama_types.ChatCompletionRequestSystemMessage(role="system", content=self.DEFAULT_SYSTEM_MESSAGE)] + messages

image_urls = self.get_image_urls(messages)
template = jinja2.Template(self.CHAT_FORMAT)
template = ImmutableSandboxedEnvironment(
trim_blocks=True,
lstrip_blocks=True,
).from_string(self.CHAT_FORMAT)
text = template.render(
messages=messages,
add_generation_prompt=True,
Expand Down Expand Up @@ -3242,8 +3244,7 @@ def chatml_function_calling(
"{% endfor %}"
"{% if add_generation_prompt %}<|im_start|>assistant\n{% endif %}"
)
template_renderer = jinja2.Environment(
loader=jinja2.BaseLoader(),
template_renderer = ImmutableSandboxedEnvironment(
autoescape=jinja2.select_autoescape(["html", "xml"]),
undefined=jinja2.StrictUndefined,
).from_string(function_calling_template)
Expand Down
0