免费获取学习方案
ARTICLE DETAIL

资讯详情

深耕编程基础知识与建站技术分享的一线实战洞察。

基于 vLLM 部署 Hunyuan-A13B-Instruct:OpenAI 兼容服务、思考模式切换与工具调用实战

基于 vLLM 部署 Hunyuan-A13B-Instruct:OpenAI 兼容服务、思考模式切换与工具调用实战 基于 vLLM 部署 Hunyuan-A13B-InstructOpenAI 兼容服务、思考模式切换与工具调用实战【免费下载链接】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 vllm0.10.0 pip install openai1.90.0 pip install modelscope1.25.0其中vllm0.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请修改我, revisionmaster) 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-parserHunyuan-A13B 对应值为hunyuan_a13b。作为对照同一模型在 SGLang 路线中见 SGLang 部署文档使用了--reasoning-parser qwen3解析think.../think思考内容并支持/think前缀强制开启思考、/no_think强制关闭思考说明该模型的思考控制机制在不同推理框架下均得到支持。vLLM ServingPython 命令行启动服务CUDA_VISIBLE_DEVICES0,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_utilizationvLLM 目标可用显存比例0~1OOM 可尝试调低如 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 $b9$ 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_keyopenai_api_key, base_urlopenai_api_base, ) for i in range(len(prompts)): response client.chat.completions.create( modelHunyuan-A13B-Instruct, messages [{role: user, content: prompts[i]}], temperature0, max_tokens8000, extra_body{ chat_template_kwargs: {enable_thinking: True}, # 默认开启思考设置为False则关闭 } ) print(f问题 {i1}: {prompts[i]}) # print(response) print(fHunyuan-A13B-Instruct思考 {i1}: {response.choices[0].message.reasoning_content}) print(fHunyuan-A13B-Instruct回复 {i1}: {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 797_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[i1:]) # 测试示例 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_DEVICES0,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允许模型在对话过程中自动决定是否调用工具。天气查询工具调用代码在一般测试代码的基础上加上工具调用我们模拟了一个查询天气的函数用来返回模拟结果实际使用中需要外接真实的查询 APIfrom 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 $b9$ 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_keyopenai_api_key, base_urlopenai_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( modelHunyuan-A13B-Instruct, messages [{role: user, content: prompts[i]}], temperature0, max_tokens8000, extra_body{ chat_template_kwargs: {enable_thinking: True}, # 默认开启思考设置为False则关闭 } ) print(f问题 {i1}: {prompts[i]}) # print(response) print(fHunyuan-A13B-Instruct思考 {i1}: {response.choices[0].message.reasoning_content}) print(fHunyuan-A13B-Instruct回复 {i1}: {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( modelHunyuan-A13B-Instruct, messagestool_messages, temperature0, max_tokens7500, toolsweather_tools, tool_choiceauto, 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( locationfunction_args.get(location, 未知城市), datefunction_args.get(date, 今天), unitfunction_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( modelHunyuan-A13B-Instruct, messagestool_messages, temperature0, max_tokens8000, extra_body{ chat_template_kwargs: {enable_thinking: True}, } ) print(问题 4: 使用工具调用天气查询) print(fHunyuan-A13B-Instruct工具调用思考: {first_tool_response.choices[0].message.reasoning_content}) print(fHunyuan-A13B-Instruct工具调用回复: {first_tool_response.choices[0].message.content}) print(fHunyuan-A13B-Instruct思考 4: {final_tool_response.choices[0].message.reasoning_content}) print(fHunyuan-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_choiceauto模型在思考后决定调用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),仅供参考
返回列表