K2-Horizon-3.7B-bf16 工具调用指南:JSON/XML/Markdown三种function call格式对比
K2-Horizon-3.7B-bf16 工具调用指南:JSON/XML/Markdown三种function call格式对比
K2-Horizon-3.7B-bf16 是 IFM K2-Horizon 3.7B 模型的 MLX bf16 转换版,专为 Apple 芯片(M 系列)本地推理设计,支持 512K 上下文,并原生内置 function call(工具调用)能力。本文带你完整对比它的三种工具描述格式——JSON、XML、Markdown 的渲染效果、解析难度与适用场景,帮你在 mlx-lm 中一次性配好工具调用。
一、先认识 K2-Horizon-3.7B-bf16 🍎
这是一份为 MLX 生态准备的稠密(dense)模型:
| 项目 | 参数 |
|---|---|
| 架构 | K2HorizonForCausalLM,36 层 decoder-only |
| 上下文窗口 | 512K(524,288 tokens) |
| 精度 | bf16 未量化,磁盘约 10.1 GB |
| 显存占用 | 短提示词峰值约 10.2 GB,适合 24 GB 内存的 Mac |
| 生成速度 | M4 Max 128GB 上约 50.3 tok/s |
| 思考标签 | 回答前会先输出 `<ifm |
模型定义与架构参数可见 config.json,MLX 实现代码在 k2_horizon.py,模型背景与基准测试详见 README.md。
💡 一个细节:模型回答前会先"思考"。调用工具时请给
max_tokens留出思考空间的余量。
二、K2-Horizon 的 function call 是怎么工作的
与很多"只支持 JSON tools"的模型不同,K2-Horizon 的聊天模板把工具调用拆成了两个独立的开关(逻辑集中在 chat_template.jinja):
tool_presentation_format(默认markdown):决定工具列表以什么格式展示给模型,支持json/xml/markdown三种。tool_call_format(默认xml):决定模型以什么格式输出调用,支持json/xml/xml_typed三种。
也就是说,"工具怎么描述"和"调用怎么输出"可以分别选择,组合自由度比单格式模型更高。
所有工具都会包在 <ifm|tools> … </ifm|tools> 块里注入 system 消息;模型发起调用时则统一输出 <ifm|tool_calls> … </ifm|tool_calls> 块。这种自定义标签体系的好处是不依赖特定框架,解析器只需做一次标签切分 + 内容反序列化。
⚠️ 两个常见报错,模板里写得很清楚:
- 传了
tool_presentation/tool_calling_format/tool_format等"别名参数"会直接抛错——请统一使用上面两个参数名; - 工具的
parameters必须是 dict(已解析的 JSON Schema),不能是 JSON 字符串。
三、三种工具描述格式对比
用一个 get_weather(city, unit) 工具为例,看三种 tool_presentation_format 分别渲染成什么样:
3.1 JSON 格式:最机器友好
整个工具列表直接序列化为原始 JSON,塞进 <ifm|tools> 块:
{"name": "get_weather", "description": "查询指定城市天气",
"parameters": {"type": "object",
"properties": {"city": {"type": "string", "description": "城市名"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}},
"required": ["city"]}}
- ✅ 优点:零格式转换,任何解析器(
json.loads)都能直接处理;对模型来说"所见即所得",适合工具 schema 非常复杂(含$defs、oneOf等)的场景——模板会把这类"渲染器无法漂亮展示"的工具自动回退为 JSON。 - ❌ 缺点:token 开销最大,
"type": "object"之类的样板文字反复出现。
3.2 XML 格式:结构化的折中
每个工具渲染为带属性的 <function> 标签,参数是 <param> 子标签:
<function name="get_weather">
<description>查询指定城市天气</description>
<parameters>
<param name="city" type="string" required=true>城市名</param>
<param name="unit" type="string" enum="celsius"|"fahrenheit"/>
</parameters>
</function>
- ✅ 优点:属性式写法紧凑,
required=true、enum=一目了然;对长 schema 的嵌套结构(items、oneOf、returns)有完整的标签化表达。 - ❌ 缺点:标签嵌套较深,比 Markdown 略难人工审阅。
3.3 Markdown 格式:人类最易读(默认)
工具被渲染成标准 Markdown 列表,参数名用行内代码标注:
## get_weather
查询指定城市天气
**Parameters**
- `city` *(string, required)* - 城市名
- `unit` *(string)*
- Allowed values: `celsius`, `fahrenheit`
- ✅ 优点:可读性最佳,方便人工检查;token 消耗低,是日常使用的默认值。
- ❌ 缺点:纯文本表达力有限——遇到
patternProperties、深层嵌套结构等复杂 schema 时,模板会自动把整组工具降级为 JSON 渲染,保持格式统一。
四、调用输出格式对比:json / xml / xml_typed
tool_call_format 控制模型"怎么喊出这次调用",三种形式都包裹在 <ifm|tool_call> 里:
json —— 单行 JSON 对象:
<ifm|tool_call>{"name": "get_weather", "arguments": {"city": "Beijing", "unit": "celsius"}}</ifm|tool_call>
xml(默认)—— 函数名 + 键值标签对:
<ifm|tool_call>get_weather
<ifm|arg_key>city</ifm|arg_key>
<ifm|arg_value>Beijing</ifm|arg_value>
</ifm|tool_call>
xml_typed —— 在 xml 基础上额外输出 <ifm|arg_type> 标签,显式声明每个参数的类型,校验更严格。
小提示:xml 系列格式中,字符串/标量参数写纯文本即可,数组和对象参数写 JSON 字面量。
五、一张表看懂三种格式怎么选
| 维度 | JSON | XML | Markdown |
|---|---|---|---|
| 参数名 | tool_presentation_format |
同左 | 同左 |
| 可读性 | 一般 | 中 | 高 |
| Token 开销 | 高 | 中 | 低 |
| 复杂 schema 表现 | 完整(原生) | 完整(标签化) | 复杂结构自动回退 JSON |
| 解析难度 | json.loads 即可 |
标签切分 + 属性解析 | 需 Markdown 解析 |
| 推荐场景 | 复杂/程序化 schema | 平衡型,结构化校验 | 日常对话、人工审阅 |
| 对应调用格式搭配 | tool_call_format="json" |
tool_call_format="xml"(默认) |
可搭配 xml 或 xml_typed |
经验法则:默认配置(Markdown 展示 + XML 调用)就是官方推荐组合,绝大多数场景无需改动;只有当你的工具 schema 大量使用 $defs、oneOf 或需要严格类型校验时,才考虑切换到 JSON 展示 + xml_typed 调用。
六、动手实践:在 mlx-lm 里跑通工具调用
1. 安装 mlx-lm
pip install -U mlx-lm
2. 获取模型(克隆到本地以便 --trust-remote-code 加载仓库内模型代码)
git clone https://gitcode.com/hf_mirrors/mlx-community/K2-Horizon-3.7B-bf16
3. 用 Python API 指定工具格式
from mlx_lm import load, generate
model, tokenizer = load("本地路径/K2-Horizon-3.7B-bf16", trust_remote_code=True)
messages = [{"role": "user", "content": "北京现在多少度?"}]
tools = [{"function": {"name": "get_weather",
"description": "查询指定城市天气",
"parameters": {"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]}}}]
prompt = tokenizer.apply_chat_template(
messages, tools=tools,
tool_presentation_format="xml", # json / xml / markdown
tool_call_format="xml", # json / xml / xml_typed
add_generation_prompt=True)
print(generate(model, tokenizer, prompt, max_tokens=2048))
命令行方式同理:mlx_lm.generate --model <本地路径> --trust-remote-code --prompt "…"。
七、常见问题 FAQ ❓
Q1:为什么加载时必须加 --trust-remote-code?
该仓库附带了 MLX 模型代码 k2_horizon.py(由 config.json 中的 model_file 字段引用),因为 mlx-lm 官方尚未内置 k2_horizon 架构,需要允许加载仓库内的模型文件。
Q2:mlx_lm.server 能解析 K2 的工具调用吗?
目前还不能。mlx-lm 尚未识别 <ifm|think> 标签和 K2 的工具调用格式,server 返回时思考文本会混在 content 字段里,工具调用块也需要你自己解析 <ifm|tool_calls> 标签。详见 README.md 的 Notes 部分。
Q3:报 "Unsupported argument: tool_presentation" 是为什么?
模板只认 tool_presentation_format 和 tool_call_format 两个参数名,其余别名(tool_presentation、tool_calling_format、tool_format)都会被显式拒绝,见 chat_template.jinja 开头几行的校验逻辑。
Q4:工具没有 parameters 会怎样?
模板会强制报错:每个工具都必须提供含 type、properties、required 的 parameters dict,把 arguments 误写成参数名也会被提示改名。
八、总结
K2-Horizon-3.7B-bf16 把"工具描述格式"与"调用输出格式"解耦,是三格式 function call 支持中灵活度较高的实现:
- 日常使用:保持默认(Markdown 展示 + XML 调用);
- 复杂 schema:切
tool_presentation_format="json"保证表达完整; - 严格校验场景:用
tool_call_format="xml_typed"拿到显式类型标签。
配合 512K 长上下文,K2-Horizon 适合做本地 Agent 的"大脑"——希望这篇 function call 格式对比能帮你快速选对配置,跑通第一个工具调用。🚀