在 Transformers 本地调用 LongCat-Flash-Thinking-2601 时,应使用仓库提供的 apply_chat_template,显式开启思考并按需传入工具,避免手写特殊 token。
适合的任务:本地 Transformers 推理、多轮深度思考、工具调用和工具结果回填。
不适合的任务:直接套用到未实现该模板的第三方 API;模型卡没有给出通用服务商的字段映射。
适用的模型版本:meituan-longcat/LongCat-Flash-Thinking-2601。
适用的客户端、Agent 或 API:Hugging Face Transformers;模型卡另提到 SGLang/vLLM,但本页代码是 tokenizer/model 本地调用示例。
推荐的推理档位和参数:官方示例使用 enable_thinking=True、add_generation_prompt=True、max_new_tokens=32768;温度未公开固定值,Heavy Thinking 的高温建议只适用于多轨迹探索。
下面保留模型卡的关键调用结构;将 model、tokenizer 替换为本地加载的对象即可。工具声明和消息字段必须保持 OpenAI 风格的 tools、tool_calls、reasoning_content 结构。
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "meituan-longcat/LongCat-Flash-Thinking-2601"
tokenizer = AutoTokenizer.from_pretrained(model_name)
# model = AutoModelForCausalLM.from_pretrained(model_name, ...)
tools = [{
"type": "function",
"function": {
"name": "func_add",
"description": "Calculate the sum of two numbers",
"parameters": {
"type": "object",
"properties": {
"x1": {"type": "number", "description": "The first addend"},
"x2": {"type": "number", "description": "The second addend"}
},
"required": ["x1", "x2"]
}
}
}]
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Please tell me what is 125679 + 234519?"},
{
"role": "assistant",
"reasoning_content": "This calculation requires precision; I will use the func_add tool.",
"tool_calls": [{
"type": "function",
"function": {
"name": "func_add",
"arguments": {"x1": 125679, "x2": 234519}
}
}]
},
{"role": "tool", "name": "func_add", "content": '{"ans": 360198}'}
]
text = tokenizer.apply_chat_template(
messages,
tools=tools,
tokenize=False,
enable_thinking=True,
add_generation_prompt=True,
save_history_reasoning_content=False
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(**model_inputs, max_new_tokens=32768)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
print(tokenizer.decode(output_ids, skip_special_tokens=True).strip("\n"))注册工具 schema,并把工具列表传给 apply_chat_template。
发送用户任务;模型返回 reasoning_content 和 tool_calls 时,按调用参数执行真实工具。
将工具结果作为 role=tool 消息追加,再次调用模板和模型。
默认用 save_history_reasoning_content=False 丢弃历史思考以节省上下文;需要保留历史思考时改为 True,并单独监测上下文长度。
模型卡直接给出 tokenizer.apply_chat_template(messages, tools=tools, tokenize=False, enable_thinking=True, add_generation_prompt=True, save_history_reasoning_content=False)。
官方工具示例使用 func_add,工具结果为 {"ans": 360198},模型生成上限示例为 max_new_tokens=32768。
模型卡说明:工具声明位于会话开头;默认交错思考模式保留最终回答和工具轨迹,丢弃此前思考内容。
这是 2601 权重的本地模板,不等于任何托管 API 的请求格式;API 网关可能改写 reasoning_content 或工具字段。
560B 总参数模型的显存、并行和量化要求很高;该页面的代码片段没有承诺消费级硬件可运行。
max_new_tokens=32768 是官方示例值,不是所有任务的最优值;长思考会显著增加成本和延迟。
模型卡未公开固定 temperature、top-p 或 Heavy Thinking 的轨迹数,不能从该示例推导一组通用参数。
页面明确把思考历史的保留设为可选:需要节省 token 时关闭 save_history_reasoning_content,需要完整复盘时再打开。
LongCat Flash Thinking