各推理框架如何控制 Qwen3 思考行为 — Transformers / SGLang / vLLM / DashScope 配置对照
概览
Qwen3 的思考模式控制在不同层面有不同方法:
| 层面 | 硬开关 | 软开关 |
|---|---|---|
| Tokenizer 模板 | enable_thinking=False | /think /no_think 指令 |
| 推理框架 | API 参数或启动参数 | 同上 |
| 云端 API | 请求参数 | 同上 |
Transformers(纯 Python 调用)
硬开关
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "Qwen/Qwen3-8B"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto"
)
messages = [
{"role": "user", "content": "分析一下这段代码的问题。"},
]
# enable_thinking=True 是默认值,设为 False 强制关闭
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=False
)
inputs = tokenizer(text, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=1024)
response = tokenizer.decode(
outputs[0][inputs["input_ids"].shape[1]:],
skip_special_tokens=True
)
print(response)
软开关(多轮对话中的状态控制)
/think 和 /no_think 是有状态的——模型会记住最近的指令,在多轮对话中持续生效,直到被新指令覆盖。
messages = [
{"role": "user", "content": "/think 证明勾股定理。"},
# 模型会思考后回答
{"role": "assistant", "content": "<think>好的,让我来..."},
{"role": "user", "content": "/no_think 给我一个例子。"},
# 模型直接给答案,不思考
]
SGLang(推理服务)
启动时设置默认模式
python -m sglang.launch_server \
--model-path Qwen/Qwen3-8B \
--thinking-type auto \
--port 30000
--thinking-type 可选值:
auto(默认):由模型自动判断disabled:强制关闭always:强制开启
单次请求控制
from sglang import Engine, ChatCompletionRequest
engine = Engine(model_path="Qwen/Qwen3-8B")
# 强制开启思考
req = ChatCompletionRequest(
model="Qwen/Qwen3-8B",
messages=[{"role": "user", "content": "证明勾股定理。"}],
thinking={"type": "always"},
max_tokens=2048,
)
# 强制关闭思考
req_no_think = ChatCompletionRequest(
model="Qwen/Qwen3-8B",
messages=[{"role": "user", "content": "1 + 1 = ?"}],
thinking={"type": "disabled"},
max_tokens=512,
)
out = engine.chat.completions.create(req)
print(out.choices[0].message.content)
cURL 方式
# 关闭思考
curl http://localhost:30000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen3-8B",
"messages": [{"role": "user", "content": "1 + 1 = ?"}],
"extra_body": {"thinking": {"type": "disabled"}},
"max_tokens": 512
}'
vLLM(推理服务)
vLLM 对 Qwen3 的思考模式支持在较新版本中完善(v0.6+)。
Python API
from vllm import LLM, SamplingParams
llm = LLM(
model="Qwen/Qwen3-8B",
tokenizer_mode="auto",
)
# vLLM 通过 extra_body 传递思考模式控制
sampling_params = SamplingParams(
max_tokens=1024,
extra_body={
"thinking": {"type": "disabled"} # 或 "always" / "auto"
}
)
output = llm.chat(
messages=[{"role": "user", "content": "1 + 1 = ?"}],
sampling_params=sampling_params,
)
print(output[0].outputs[0].text)
启动服务
vllm serve Qwen/Qwen3-8B \
--port 8000 \
--chat-template auto \
--api-key EMPTY
DashScope(阿里云百炼 Python SDK)
开启思考模式
import dashscope
from dashscope import Generation
dashscope.api_key = "YOUR_DASHSCOPE_API_KEY"
# qwen3-max 默认开启思考模式(云端旗舰)
response = Generation.call(
model="qwen3-max",
messages=[
{"role": "user", "content": "证明勾股定理。"}
],
result_format="message",
enable_thinking=True, # 显式开启
)
print(response.output.choices[0].message.content)
# 如果是思考模式,content 会包含 <think>...</think> 段落
关闭思考模式
response = Generation.call(
model="qwen3-plus",
messages=[
{"role": "user", "content": "1 + 1 = ?"}
],
result_format="message",
enable_thinking=False, # 关闭思考
)
OpenAI 兼容接口
from openai import OpenAI
client = OpenAI(
api_key="YOUR_DASHSCOPE_API_KEY",
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
response = client.chat.completions.create(
model="qwen3-max",
messages=[
{"role": "user", "content": "证明勾股定理。"}
],
extra_body={
"enable_thinking": True
}
)
print(response.choices[0].message.content)
Ollama
Ollama 目前对 Qwen3 思考模式的官方支持还在完善中。最可靠的方式是通过 prompt 控制。
# 提示词方式
ollama run qwen3:8b "/no_think 1 + 1 = ?"
ollama run qwen3:8b "/think 证明勾股定理"
在 Modelfile 中预设:
FROM qwen3:8b
SYSTEM You are a helpful assistant. /no_think
然后 ollama create qwen3-fast -f Modelfile,之后运行 ollama run qwen3-fast 就默认不思考了。
LM Studio
LM Studio 在 UI 界面中提供了思考模式的开关:
- 打开 LM Studio
- 加载 Qwen3 模型
- 在推理参数中找到「Thinking Mode」选项
- 选择 Enabled / Disabled / Auto
命令行方式可以通过 lms 的 prompt 传入 /think 或 /no_think。
思考 token 的提取
当思考模式开启时,模型输出中会包含 <think>...</think> 标签包裹的思考链。在 API 返回中,不同框架的处理方式不同:
- Transformers:整个 response 包含思考标签,需要自行解析
- SGLang / vLLM:可以通过参数分离思考 token 和最终答案
- DashScope:思考内容在
reasoning_content字段,最终答案在content字段
# DashScope 的思考内容分离
response = Generation.call(model="qwen3-max", messages=messages)
# response.output.choices[0].message.reasoning_content ← 思考链
# response.output.choices[0].message.content ← 最终答案
相关阅读
- 02-关闭思考模式的方法:三种关闭方法详解
- 04-思考 vs 非思考 性能对比:两种模式的实测效率差异
- 01-DashScope Python SDK:完整的云端 API 接入教程