PEFT
Safetensors
English
encoder-decoder
mixture-of-experts
ul2
lora
tool-use
function-calling
chat
kaivoss's picture
Upload glm_tools.py with huggingface_hub
b57ace0 verified
Raw History Blame Contribute Delete
849 Bytes
"""Render an OpenAI `tools` list into the system-prompt text GLM-5.3-Flash's own chat template produces.
Training data and the serving shim both use this, so the model sees the same tools block at inference as in training.
"""
import json
from pathlib import Path
import jinja2
HERE = Path(__file__).parent
_glm = None
def tools_block(tools):
global _glm
if _glm is None:
env = jinja2.Environment(trim_blocks=True, lstrip_blocks=True, extensions=["jinja2.ext.loopcontrols"])
env.filters["tojson"] = lambda v, ensure_ascii=False, indent=None: json.dumps(v, ensure_ascii=ensure_ascii, indent=indent)
_glm = env.from_string((HERE / "glm_tools_template.jinja").read_text(encoding="utf-8"))
parts = _glm.render(messages=[], tools=tools).split("<|system|>")
return parts[2].lstrip("\n")