Unverified Commit 17896f67 authored by 조준래's avatar 조준래 Committed by GitHub
Browse files

Change JSON serialization to custom json.dumps (#31100)



* Change JSON serialization to custom json.dumps to prevent escaping of "<", ">", "&", "'"

* caller has control over the order, remove sort_key=True

* Move tojson into a proper function and expose a couple of other args

---------
Co-authored-by: default avatarjun.4 <jun.4@kakaobrain.com>
Co-authored-by: default avatarMatt <rocketknight1@gmail.com>
parent 1c77b3d9
...@@ -1892,8 +1892,13 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): ...@@ -1892,8 +1892,13 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin):
def raise_exception(message): def raise_exception(message):
raise TemplateError(message) raise TemplateError(message)
def tojson(x, ensure_ascii=False, indent=None, separators=None, sort_keys=False):
# We override the built-in tojson filter because Jinja's default filter escapes HTML characters
# We also expose some options like custom indents and separators
return json.dumps(x, ensure_ascii=ensure_ascii, indent=indent, separators=separators, sort_keys=sort_keys)
jinja_env = ImmutableSandboxedEnvironment(trim_blocks=True, lstrip_blocks=True) jinja_env = ImmutableSandboxedEnvironment(trim_blocks=True, lstrip_blocks=True)
jinja_env.policies["json.dumps_kwargs"]["ensure_ascii"] = False jinja_env.filters["tojson"] = tojson
jinja_env.globals["raise_exception"] = raise_exception jinja_env.globals["raise_exception"] = raise_exception
return jinja_env.from_string(chat_template) return jinja_env.from_string(chat_template)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment