基于 vLLM 部署 Hunyuan-A13B-Instruct:OpenAI 兼容服务、思考模式切换与工具调用实战
【免费下载链接】self-llm《开源大模型食用指南》针对中国宝宝量身打造的基于Linux环境快速微调(全参数/Lora)、部署国内外开源大模型(LLM)/多模态大模型(MLLM)教程项目地址: https://gitcode.com/GitHub_Trending/se/self-llm
本篇技术指南以 models/Hunyuan-A13B-Instruct/02-Hunyuan-A13B-Instruct-vLLM.md 为核心,讲解如何用 vLLM 将腾讯混元 Hunyuan-A13B-Instruct 大模型部署为兼容 OpenAI 接口的服务,并完成思考(推理)模式切换、日常问答/数学推理/代码写作能力实测与工具调用全流程。读者按文操作后,可独立完成从环境准备、模型下载到服务启动、客户端调用与健康检查的完整闭环,并掌握--enable-reasoning、--tool-call-parser等关键参数的用法。
vLLM 简介
vLLM是一个高性能的大语言模型推理与服务框架,具备以下特点:
- 高效的 KV 缓存与内存管理:基于
PagedAttention显著降低显存浪费,提升长文本与高并发场景下的吞吐。 - 兼容 OpenAI 接口:可直接以
OpenAI API形式对外提供completions与chat completions能力,便于与现有生态集成。 - 多 GPU 并行与易扩展:支持 Tensor Parallel 等策略,参数简单、易于横向扩展吞吐与上下文长度上限。
- 生态良好:与
HuggingFace/ModelScope模型仓库无缝衔接,支持多种推理优化与特性(如推理/思考内容解析、工具调用)。
在本仓库中,Hunyuan-A13B-Instruct 的部署路线包括 vLLM(本文)、SGLang 与 EvalScope 并发测试,其中 vLLM 路线以 OpenAI 兼容接口和丰富的推理特性著称,适合快速接入各类应用。
环境准备
基础环境
本文的基础环境如下:
---------------- ubuntu 22.04 python 3.12 cuda 12.8 pytorch 2.8.0 ----------------提示:请确保本机 NVIDIA 驱动、CUDA 与 PyTorch CUDA 编译版本匹配,可用
nvidia-smi与python -c "import torch; print(torch.version.cuda, torch.cuda.is_available())"进行快速自检。
vLLM 环境配置
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple pip install vllm==0.10.0 pip install openai==1.90.0 pip install modelscope==1.25.0其中vllm==0.10.0为推理服务框架,openai客户端用于调用服务,modelscope用于从魔搭社区下载模型。
模型下载
使用 ModelScope 的snapshot_download下载模型,新建model_download.py文件并输入以下内容:
# model_download.py # 注意修改cache_dir为保存的路径 from modelscope import snapshot_download model_dir = snapshot_download('Tencent-Hunyuan/Hunyuan-A13B-Instruct', cache_dir='请修改我!!!', revision='master') print(f"模型下载完成,保存路径为:{model_dir}")注意:请将
cache_dir修改为实际的模型下载保存路径,终端执行python model_download.py后耐心等待下载完成。
Hunyuan-A13B-Instruct 思考推理模式切换
Hunyuan-A13B-Instruct默认使用慢思考(即推理模式)。
这一设计与模型本身的架构直接相关。根据本仓库的模型架构解析文档,Hunyuan-A13B 采用大规模稀疏专家(MoE)架构:包含1 个共享专家 + 64 个细粒度非共享专家,训练阶段每次前向传播激活 8 个专家,并采用 SwiGLU 激活函数与 Grouped-Query Attention;在推理层面采用双模式推理链框架——快思考(Fast Thinking)模拟人类直觉推理、快速生成结果,慢思考(Slow Thinking)模拟人类深度推理、适用于需要多步逻辑推断的复杂任务。因此模型默认开启慢思考,以保证复杂任务的推理质量。
推理模式可以通过两种方式关闭:
- 请求参数方式:在请求中设置
extra_body参数:{"chat_template_kwargs": {"enable_thinking": false}} - 提示词方式:在提示词前添加
/no_think前缀
另:若希望在服务端返回
reasoning_content字段用于展示推理过程,请在服务启动时开启--enable-reasoning并指定正确的--reasoning-parser(Hunyuan-A13B 对应值为hunyuan_a13b)。
作为对照,同一模型在 SGLang 路线中(见 SGLang 部署文档)使用了--reasoning-parser qwen3解析<think>...</think>思考内容,并支持/think前缀强制开启思考、/no_think强制关闭思考,说明该模型的思考控制机制在不同推理框架下均得到支持。
vLLM Serving
Python 命令行启动服务
CUDA_VISIBLE_DEVICES=0,1,2,3 python -m vllm.entrypoints.openai.api_server \ --model /请修改我!!!/Tencent-Hunyuan/Hunyuan-A13B-Instruct \ --served-model-name Hunyuan-A13B-Instruct \ --max-model-len 8192 \ --tensor-parallel-size 4 \ --port 8085 \ --trust_remote_code \ --gpu_memory_utilization 0.9 \ --enable-reasoning \ --reasoning-parser hunyuan_a13b成功启动后,你将看到Application startup complete的输出:
显存预算建议:官方推荐 4 * H20。
我们通过上述 vLLM 启动的服务兼容 OpenAI 接口,因此可以通过 Python 的 OpenAI 库进行调用。下面展示日常问答、数学推理、代码写作和工具调用的实际例子来测试Hunyuan-A13B-Instruct的能力。
关键参数说明
| 参数 | 说明 | 注意事项 |
|---|---|---|
--tensor-parallel-size | 张量并行划分数 | 等于所用 GPU 数时较常见;多卡可提升吞吐和可用上下文长度上限 |
--max-model-len | 单请求最大上下文长度(输入+输出) | 越大显存占用越高,易触发 OOM;可按显存情况下调,如 4096 |
--gpu_memory_utilization | vLLM 目标可用显存比例(0~1) | OOM 可尝试调低,如 0.8/0.7 |
--served-model-name | 对外暴露的模型名 | 客户端需用同名model调用 |
--port/--host | 服务监听端口/地址 | 云主机需放通端口安全组 |
--trust_remote_code | 允许加载仓库中的自定义代码 | 必需,否则部分模型无法正确初始化 |
--enable-reasoning+--reasoning-parser hunyuan_a13b | 开启推理内容解析 | 返回reasoning_content字段,便于展示"思考"过程 |
其中--trust_remote_code对 Hunyuan-A13B-Instruct 尤其重要:模型仓库携带自定义的 modeling 代码(如hunyuan.py等),需要该参数才能正确初始化。这也是 LoRA 微调文档 中专门要求修改模型目录下hunyuan.py文件的原因——该模型对远程代码的依赖贯穿推理与微调全流程。
健康检查
服务启动后,可用curl快速确认服务状态与模型列表:
curl http://127.0.0.1:8085/v1/models示例测试
from openai import OpenAI # 通过 Python 的 OpenAI 客户端库进行调用。下面展示日常问答,数学推理,代码写作和工具调用的实际例子来测试 `Hunyuan-A13B-Instruct` 的能力 openai_api_key = "EMPTY" openai_api_base = "http://127.0.0.1:8085/v1" # 使用正确的端口 prompt_daily_chat = "你好,你是谁" prompt_math_reasoning = "Find the sum of all integer bases $b>9$ for which $17_{b}$ is a divisor of $97_{b}$." # 题目来自AIME2025,答案为70 prompt_coding = "写一个python程序,实现快速排序" prompts = [prompt_daily_chat, prompt_math_reasoning, prompt_coding] client = OpenAI( api_key=openai_api_key, base_url=openai_api_base, ) for i in range(len(prompts)): response = client.chat.completions.create( model="Hunyuan-A13B-Instruct", messages = [{"role": "user", "content": prompts[i]}], temperature=0, max_tokens=8000, extra_body={ "chat_template_kwargs": {"enable_thinking": True}, # 默认开启思考,设置为False则关闭 } ) print(f"问题 {i+1}: {prompts[i]}") # print(response) print(f"Hunyuan-A13B-Instruct思考 {i+1}: {response.choices[0].message.reasoning_content}") print(f"Hunyuan-A13B-Instruct回复 {i+1}: {response.choices[0].message.content}") print("-"*100)若拿不到
reasoning_content字段,请确认服务端已添加--enable-reasoning --reasoning-parser hunyuan_a13b。
测试结果
问题 1(日常问答):你好,你是谁
模型先进行内部思考(梳理身份设定、功能清单与语气),随后给出回复:
你好,我是腾讯元宝,你可以叫我元宝,英文名是Tencent Yuanbao。我是由腾讯开发的人工智能助手呢。 我可有不少功能哦:1. 回答问题……2. 解决问题……3. 学习新知识……4. 创造内容……5. 闲聊……我还支持灵活切换多种顶尖大模型,现在用的是Hunyuan-T1模型。而且你可以通过文字、图片、文件等多种方式跟我交流哦。
问题 2(数学推理,AIME2025 题目)
模型将17_b与97_b转换为十进制:17_b = b + 7,97_b = 9b + 7;由9b + 7 = 9(b + 7) - 56可知b + 7必须是 56 的约数,结合b > 9得候选约数 28 与 56,对应基数b = 21与b = 49,最终给出:
[ 21 + 49 = \boxed{70} ]
推理过程完整、严谨,最终答案与题目给出的标准答案一致。
问题 3(代码写作):写一个 python 程序,实现快速排序
模型不仅给出代码,还附带原理讲解、逐行注释、测试用例与复杂度说明:
def quick_sort(arr): # 基本情况:如果数组长度小于等于1,直接返回 if len(arr) <= 1: return arr # 选择最后一个元素作为基准值 pivot = arr[-1] # i 指向当前小于pivot的元素的末尾位置 i = 0 # 遍历除pivot外的所有元素 for j in range(len(arr) - 1): # 如果当前元素小于等于pivot,交换到i的位置 if arr[j] <= pivot: arr[i], arr[j] = arr[j], arr[i] i += 1 # i右移,标记下一个小于pivot的位置 # 将pivot放到正确的位置(i的位置) arr[i], arr[-1] = arr[-1], arr[i] # 递归排序左边和右边的子数组,并合并结果 return quick_sort(arr[:i]) + [arr[i]] + quick_sort(arr[i+1:]) # 测试示例 if __name__ == "__main__": example_arr = [3, 6, 8, 10, 1, 2, 1] sorted_arr = quick_sort(example_arr) print("排序后的数组:", sorted_arr)测试输出:排序后的数组: [1, 1, 2, 3, 6, 8, 10]。模型同时指出:该实现采用 Lomuto 分区方案,平均时间复杂度 (O(n\log n)),最坏情况(数组已有序)退化为 (O(n^2)),可通过随机选择基准值优化。
分析
这三个问题Hunyuan-A13B-Instruct都回答得不错:自我认知清晰,AIME2025 题目的推理和最终答案均正确,代码有清晰的注释、解释和测试用例,体现出慢思考模式下较强的推理与生成能力。
工具调用
工具调用是大语言模型的一项至关重要的能力,Hunyuan-A13B-Instruct也支持了工具调用。
注意:官方 README 中存在两处未更新的错误,tool-call-parser参数在vLLM的实现中值应为hunyuan_a13b,另外Reasoning Parser也已经集成到 vLLM 中(即前文的--reasoning-parser hunyuan_a13b),无需再单独引入。
启动带工具解析的服务
在部署时,为了开启工具调用,需要在原启动命令的基础上加入两个参数,最终的vLLM启动命令为:
CUDA_VISIBLE_DEVICES=0,1,2,3 python -m vllm.entrypoints.openai.api_server \ --model /请修改我!!!/Tencent-Hunyuan/Hunyuan-A13B-Instruct \ --served-model-name Hunyuan-A13B-Instruct \ --max-model-len 8192 \ --tensor-parallel-size 4 \ --port 8085 \ --trust_remote_code \ --gpu_memory_utilization 0.9 \ --tool-call-parser hunyuan_a13b \ --enable-auto-tool-choice \ --enable-reasoning \ --reasoning-parser hunyuan_a13b其中--tool-call-parser hunyuan_a13b指定混元专用的工具调用解析器,--enable-auto-tool-choice允许模型在对话过程中自动决定是否调用工具。
天气查询工具调用代码
在一般测试代码的基础上加上工具调用,我们模拟了一个查询天气的函数用来返回模拟结果(实际使用中需要外接真实的查询 API):
from openai import OpenAI import json # 通过 Python 的 OpenAI 客户端库进行调用。下面展示日常问答,数学推理,代码写作和工具调用的实际例子来测试 `Hunyuan-A13B-Instruct` 的能力 openai_api_key = "EMPTY" openai_api_base = "http://127.0.0.1:8085/v1" # 使用正确的端口 prompt_daily_chat = "你好,你是谁" prompt_math_reasoning = "Find the sum of all integer bases $b>9$ for which $17_{b}$ is a divisor of $97_{b}$." # 题目来自AIME2025,答案为70 prompt_coding = "写一个python程序,实现快速排序" prompts = [prompt_daily_chat, prompt_math_reasoning, prompt_coding] client = OpenAI( api_key=openai_api_key, base_url=openai_api_base, ) def get_weather(location: str, date: str = "今天", unit: str = "celsius") -> str: """示例工具:查询天气(示例中返回模拟结果)。 Args: location: 城市名 date: 日期(如"今天"/"明天"或 YYYY-MM-DD) unit: 温度单位(celsius/fahrenheit) Returns: 一个简要的天气描述字符串 """ normalized_unit = "°C" if unit == "celsius" else "°F" # 这里返回一个模拟结果,真实场景可替换为外部 API 调用 return f"{location}{date}多云,气温 28{normalized_unit},湿度 70%,东北风 3 级。" for i in range(len(prompts)): response = client.chat.completions.create( model="Hunyuan-A13B-Instruct", messages = [{"role": "user", "content": prompts[i]}], temperature=0, max_tokens=8000, extra_body={ "chat_template_kwargs": {"enable_thinking": True}, # 默认开启思考,设置为False则关闭 } ) print(f"问题 {i+1}: {prompts[i]}") # print(response) print(f"Hunyuan-A13B-Instruct思考 {i+1}: {response.choices[0].message.reasoning_content}") print(f"Hunyuan-A13B-Instruct回复 {i+1}: {response.choices[0].message.content}") print("-"*100) # ============== 工具调用示例(OpenAI 格式):weather 查询 ============== tool_messages = [ {"role": "system", "content": "你可以调用工具来获取实时天气。"}, {"role": "user", "content": "帮我查一下深圳今天的天气,用摄氏度。"}, ] weather_tools = [ { "type": "function", "function": { "name": "get_weather", "description": "根据地点与日期查询天气,返回简要的天气描述。", "parameters": { "type": "object", "properties": { "location": {"type": "string", "description": "城市名,如北京、深圳"}, "date": {"type": "string", "description": "日期,YYYY-MM-DD 或 今天/明天", "default": "今天"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"], "default": "celsius"} }, "required": ["location"] } } } ] first_tool_response = client.chat.completions.create( model="Hunyuan-A13B-Instruct", messages=tool_messages, temperature=0, max_tokens=7500, tools=weather_tools, tool_choice="auto", extra_body={ "chat_template_kwargs": {"enable_thinking": True}, } ) assistant_msg = first_tool_response.choices[0].message # 将包含 tool_calls 的 assistant 消息加入到对话历史 tool_messages.append({ "role": "assistant", "content": assistant_msg.content or "", "tool_calls": assistant_msg.tool_calls, }) tool_calls = assistant_msg.tool_calls or [] for tool_call in tool_calls: function_name = tool_call.function.name try: function_args = json.loads(tool_call.function.arguments or "{}") except json.JSONDecodeError: function_args = {} if function_name == "get_weather": tool_result = get_weather( location=function_args.get("location", "未知城市"), date=function_args.get("date", "今天"), unit=function_args.get("unit", "celsius"), ) else: tool_result = f"不支持的工具: {function_name}" tool_messages.append({ "role": "tool", "tool_call_id": tool_call.id, "content": tool_result, }) final_tool_response = client.chat.completions.create( model="Hunyuan-A13B-Instruct", messages=tool_messages, temperature=0, max_tokens=8000, extra_body={ "chat_template_kwargs": {"enable_thinking": True}, } ) print("问题 4: 使用工具调用(天气查询)") print(f"Hunyuan-A13B-Instruct工具调用思考: {first_tool_response.choices[0].message.reasoning_content}") print(f"Hunyuan-A13B-Instruct工具调用回复: {first_tool_response.choices[0].message.content}") print(f"Hunyuan-A13B-Instruct思考 4: {final_tool_response.choices[0].message.reasoning_content}") print(f"Hunyuan-A13B-Instruct回复 4: {final_tool_response.choices[0].message.content}") print("-"*100)运行结果与流程说明
输出结果为:
问题 4: 使用工具调用(天气查询) Hunyuan-A13B-Instruct工具调用思考: 好的,用户让我帮忙查深圳今天的天气,并且要用摄氏度。……所有必要参数都已提供,没有缺失。因此,应该调用get_weather函数,传入location为深圳,date为今天,unit为摄氏度。 Hunyuan-A13B-Instruct工具调用回复: None Hunyuan-A13B-Instruct思考 4: 好的,用户让我查深圳今天的天气,用摄氏度。我之前已经调用了天气API,得到了回复:深圳今天多云,气温28°C,湿度70%,东北风3级。现在需要把这些信息整理成用户友好的回答。 Hunyuan-A13B-Instruct回复 4: 深圳今天多云,气温28°C,湿度70%,东北风3级。希望这些信息对你有帮助哦。整个调用链路体现了标准 OpenAI 工具调用范式:
- 首轮请求携带
tools与tool_choice="auto",模型在思考后决定调用get_weather,返回的 assistant 消息中content为空、tool_calls携带函数名与参数; - 将含
tool_calls的 assistant 消息追加进历史,按tool_call_id回填role: "tool"的执行结果; - 二次请求让模型基于工具结果组织最终回复。
在工具调用中,Hunyuan-A13B-Instruct也表现得很好,能够适时调用工具并最终返回准确的天气查询信息。
常见问题与调试建议
- 拿不到
reasoning_content字段:请确认服务端启动命令已添加--enable-reasoning --reasoning-parser hunyuan_a13b。 - 显存不足(OOM):可下调
--max-model-len(如 4096)或降低--gpu_memory_utilization(如 0.8/0.7),并按--tensor-parallel-size增加参与推理的 GPU 数量。 - 服务不可达:确认
--port已正确开放(云主机需放通安全组),并用curl http://127.0.0.1:8085/v1/models做健康检查。 - 工具调用不生效:确认启动命令包含
--tool-call-parser hunyuan_a13b --enable-auto-tool-choice两个参数,且客户端传入了标准tools/tool_choice字段。
小结
本文基于 vLLM 完成了 Hunyuan-A13B-Instruct 的完整部署链路:从环境与依赖准备、ModelScope 模型下载,到带思考解析的 OpenAI 兼容服务启动,再到日常问答、AIME 数学推理、代码生成与工具调用的多维度实测。结合本仓库同目录下的模型架构解析可以理解其默认慢思考的行为来源,SGLang 部署文档则提供了同一模型的另一套高性能推理方案,读者可按需选择;若需进一步压测并发性能,可参考 EvalScope 并发测试文档。
【免费下载链接】self-llm《开源大模型食用指南》针对中国宝宝量身打造的基于Linux环境快速微调(全参数/Lora)、部署国内外开源大模型(LLM)/多模态大模型(MLLM)教程项目地址: https://gitcode.com/GitHub_Trending/se/self-llm
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考