Skip to content

Commit a671d18

Browse files
committed
Switch to j2 sandbox
1 parent b579c62 commit a671d18

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/guidellm/backend/openai.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import httpx
1010
import jinja2
11+
from jinja2.sandbox import ImmutableSandboxedEnvironment
1112
from loguru import logger
1213
from PIL import Image
1314

@@ -136,7 +137,17 @@ def __getstate__(self) -> object:
136137

137138
@cached_property
138139
def request_template(self) -> jinja2.Template:
139-
j2_env = jinja2.Environment(loader=jinja2.BaseLoader(), autoescape=True)
140+
# Thanks to HuggingFace Tokenizers for this implementation
141+
def tojson(x, ensure_ascii=False):
142+
# We override the built-in tojson filter because Jinja's
143+
# default filter escapes HTML characters
144+
return json.dumps(x, ensure_ascii=ensure_ascii)
145+
146+
j2_env = ImmutableSandboxedEnvironment(trim_blocks=True, lstrip_blocks=True)
147+
148+
# Define custom filter functions
149+
j2_env.filters["tojson"] = tojson
150+
140151
return j2_env.from_string(self._request_template_str)
141152

142153
@property

0 commit comments

Comments
 (0)