Files
Strata/serve/toolformat.py
T
Niko1221 7084a49b7b tools/qwen4_intake.py: read GGUF headers and say what a new model needs
One command on a folder, GGUF files or a Hugging Face repo (range reads of the
headers only). Per set of shards it reports the architecture and every geometry
key against qwen4exp's compiled shape and qwen35moe's limits, the tensor layout
against the family_layout.hpp tables (missing, reshaped, unknown), each quant
type by role against the CUDA / HIP / SYCL / Metal kernel lists, the tokenizer
and chat template (tool-call form, thinking tags), MTP, picture reader, split
and sizes, and a case A / B / C verdict with a DRAFT catalog entry and setup
config. serve/toolformat.py holds the tool-call form detector.

The tables are ports of the C++ and kernel sources; the tests read those files
and fail when a table drifts. Fixtures are real headers recorded from Hugging
Face (Flash-Next sets, Qwen3.6, DeepSeek, picture readers).
2026-11-06 17:22:07 +01:00

43 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Which tool-call spelling a chat template documents, read from the template's text.
No imports from the rest of the server: tools/qwen4_intake.py and serve/frontend.py share this one function, so the report
that says "this model writes XML calls" and the parser the server picks cannot disagree.
xml `<tool_call>` around `<function=NAME>` and `<parameter=NAME>` blocks (Qwen3.6, Qwen3.8-Flash-Next: every template Strata ships)
json `<tool_call>` around a JSON object `{"name": ..., "arguments": {...}}` (Qwen3, Hermes)
dsml DeepSeek-V4's `<|DSML|...>` blocks
none the template has no tool section
other the template documents tools in a form this module does not know
"""
from __future__ import annotations
import re
_DSML = "|DSML|" # the full-width bars DeepSeek's template writes
_XML_OPEN = re.compile(r"<function=[^>]*>")
def tool_format(source: str) -> str:
"""The tool-call form `source` (a chat template's Jinja text) tells the model to write."""
if not isinstance(source, str) or not source:
return "none"
if _DSML in source:
return "dsml"
has_call = "<tool_call>" in source
if has_call and _XML_OPEN.search(source) and "<parameter=" in source:
return "xml"
if has_call and ("tools" in source) and ('"name"' in source or "'name'" in source or "| tojson" in source or "|tojson" in source):
return "json"
if "tools" not in source and not has_call:
return "none"
return "other"
def thinking_tags(source: str) -> dict:
"""The reasoning tags the template mentions: whether it writes `<think>` itself (into the generation prompt) and takes an
`enable_thinking` switch."""
src = source if isinstance(source, str) else ""
return {"has_think_tags": "<think>" in src and "</think>" in src,
"enable_thinking_switch": "enable_thinking" in src,
"generation_prompt_opens_think": bool(re.search(r"add_generation_prompt.*?<think>", src, re.S))}