一文搞定Qwen3.5微调全流程从SFT、RLHF、多模态到Agent微调覆盖全场景数据格式深度解析20 Agent模板并附开箱即用训练代码助你快速上手实战。一、概述为什么需要这篇文章在微调 Qwen3.5 模型时你是否遇到过这些困惑❓ 数据格式五花八门不知道该用哪种❓ Agent 微调到底是什么如何让模型学会调用工具❓ 20 种 Agent Template 该如何选择❓ 多模态、RLHF、工具调用的数据该怎么准备本文基于 ms-swift阿里巴巴 ModelScope 官方微调框架为你提供✅4 种自动转换数据格式- messages/sharegpt/alpaca/query-response 一键适配✅全场景数据格式- 预训练、SFT、DPO/KTO/GRPO、多模态、Agent 全覆盖✅Agent 微调深度解析- 原理、数据格式、20 Template 对比、实战代码✅开箱即用的训练脚本- 复制粘贴即可运行适合人群AI 研究者、算法工程师、大模型应用开发者二、ms-swift 标准数据集格式ms-swift 的标准数据集格式可接受的 keys 包括Key说明是否必需messages对话消息列表必需rejected_response用于 DPO 等 RLHF 训练可选label用于 KTO 训练和分类模型训练可选images多模态图片路径/URL可选videos多模态视频路径/URL可选audios多模态音频路径/URL可选toolsAgent 任务的工具定义可选objectsgrounding 任务可选2.1 四种可自动转换的数据格式ms-swift 的AutoPreprocessor支持将以下四种格式自动转换为标准格式1) messages 格式标准格式{messages: [{role: system, content: system}, {role: user, content: query1}, {role: assistant, content: response1}, {role: user, content: query2}, {role: assistant, content: response2}]}2) sharegpt 格式{system: system, conversation: [{human: query1, assistant: response1}, {human: query2, assistant: response2}]}3) query-response 格式{system: system, query: query2, response: response2, history: [[query1, response1]]}自动字段映射system:system,system_promptquery:query,prompt,input,instruction,question,problemresponse:response,answer,output,targets,target,answer_key,answers,solution,text,completion,content4) alpaca 格式{system: system, instruction: query-inst, input: query-input, output: response}注意instruction和input字段将组合成 query 字段。若两者都不为空query f{instruction}\n{input}三、各场景数据格式详解3.1 预训练数据格式{messages: [{role: assistant, content: I love music}]}{messages: [{role: assistant, content: 教练我要打篮球}]}{messages: [{role: assistant, content: 西红柿鸡蛋盖饭和地三鲜盖饭哪个更权威}]}3.2 监督微调SFT数据格式{messages: [{role: system, content: 你是个有用无害的助手}, {role: user, content: 告诉我明天的天气}, {role: assistant, content: 明天天气晴朗}]}{messages: [{role: system, content: 你是个有用无害的数学计算器}, {role: user, content: 11等于几}, {role: assistant, content: 等于2}, {role: user, content: 再加1呢}, {role: assistant, content: 等于3}]}控制损失计算可通过loss字段控制对应回复是否计算损失{messages: [{role: user, content: 你好}, {role: assistant, content: 你好有什么可以帮助你的吗, loss: false}, {role: user, content: 11等于几}, {role: assistant, content: 等于2, loss: true}]}3.3 RLHF 数据格式DPO/ORPO/CPO/SimPO/RM{messages: [{role: system, content: 你是个有用无害的助手}, {role: user, content: 告诉我明天的天气}, {role: assistant, content: 明天天气晴朗}], rejected_response: 我不知道}KTO{messages: [{role: user, content: 告诉我明天的天气}, {role: assistant, content: 我不知道}], label: false}{messages: [{role: user, content: 11等于几}, {role: assistant, content: 等于2}], label: true}PPO/GRPO{messages: [{role: system, content: 你是个有用无害的助手}, {role: user, content: 告诉我明天的天气}]}{messages: [{role: user, content: 11等于几}, {role: assistant, content: 等于2}, {role: user, content: 再加1呢}]}3.4 多模态数据格式{messages: [{role: user, content: 浙江的省会在哪}, {role: assistant, content: 浙江的省会在杭州。}]}{messages: [{role: user, content: imageimage两张图片有什么区别}, {role: assistant, content: 前一张是小猫后一张是小狗}], images: [/xxx/x.jpg, /xxx/x.png]}{messages: [{role: user, content: audio语音说了什么}, {role: assistant, content: 今天天气真好呀}], audios: [/xxx/x.mp3]}{messages: [{role: user, content: image图片中是什么video视频中是什么}, {role: assistant, content: 图片中是一个大象视频中是一只小狗在草地上奔跑}], images: [/xxx/x.jpg], videos: [/xxx/x.mp4]}特殊标签说明image- 图片插入位置video- 视频插入位置audio- 音频插入位置四、Agent 微调详解4.1 什么是 Agent 微调Agent 微调是让大语言模型具备工具调用能力的训练过程。通过 Agent 微调模型可以理解工具描述解析 API 的功能、参数和使用方法决策工具调用判断何时需要调用工具选择合适的工具生成调用参数根据用户需求生成正确的工具调用参数整合工具结果将工具返回的结果整合到最终回答中4.2 Agent 微调解决的核心问题问题类型描述Agent 微调的解决方案能力边界LLM 知识有截止日期无法获取实时信息通过调用外部 API 获取实时数据计算精度LLM 数学计算能力有限调用计算器等专业工具专业任务无法执行代码、操作文件等调用代码执行器、文件系统等工具多模态交互需要与外部系统交互调用各类 API 完成复杂任务任务分解复杂任务需要多步骤完成支持多轮工具调用和结果整合4.3 Agent 数据集格式ms-swift 使用agent-template实现了 Agent 数据格式与模型的解耦基于统一的数据集格式可以灵活切换不同模型进行训练。纯文本 Agent 数据样本{ tools: [{\type\: \function\, \function\: {\name\: \realtime_aqi\, \description\: \天气预报。获取实时空气质量。当前空气质量PM2.5PM10信息\, \parameters\: {\type\: \object\, \properties\: {\city\: {\type\: \string\, \description\: \城市名例如上海\}}, \required\: [\city\]}}}], messages: [ {role: user, content: 北京和上海今天的天气情况}, {role: tool_call, content: {\name\: \realtime_aqi\, \arguments\: {\city\: \北京\}}}, {role: tool_call, content: {\name\: \realtime_aqi\, \arguments\: {\city\: \上海\}}}, {role: tool_response, content: {\city\: \北京\, \aqi\: \10\, \unit\: \celsius\}}, {role: tool_response, content: {\city\: \上海\, \aqi\: \72\, \unit\: \fahrenheit\}}, {role: assistant, content: 根据天气预报工具北京今天的空气质量指数为10属于良好水平上海今天的空气质量指数为72属于轻度污染水平。} ]}多模态 Agent 数据样本{ tools: [{\type\: \function\, \function\: {\name\: \click\, \description\: \点击屏幕中的某个位置\, \parameters\: {\type\: \object\, \properties\: {\x\: {\type\: \integer\, \description\: \横坐标\}, \y\: {\type\: \integer\, \description\: \纵坐标\}}, \required\: [\x\, \y\]}}}], messages: [ {role: user, content: image现在几点了}, {role: assistant, content: think\n我可以通过打开日历App来获取当前时间。\n/think\n}, {role: tool_call, content: {\name\: \click\, \arguments\: {\x\: 105, \y\: 132}}}, {role: tool_response, content: {\images\: \image\, \status\: \success\}}, {role: assistant, content: 成功打开日历App现在的时间为中午11点} ], images: [desktop.png, calendar.png]}4.4 Agent 数据格式关键要素字段说明toolsJSON 字符串包含工具列表定义role: tool_call模型发起的工具调用content 为 JSON 字符串role: tool_response工具返回的结果也可写成role: toolrole: assistant模型的文本回复重要特性并行工具调用支持连续多个tool_call如上例中同时查询北京和上海混合输出支持assistant和tool_call混合出现多模态支持image标签数量应与images长度相同4.5 tools 字段格式tools [{ type: function, function: { name: get_current_weather, description: Get the current weather in a given location, parameters: { type: object, properties: { location: { type: string, description: The city and state, e.g. San Francisco, CA }, unit: { type: string, enum: [celsius, fahrenheit] } }, required: [location] } }}]五、Agent Template 详解ms-swift 支持多种 Agent Template实现了数据格式与模型的解耦5.1 支持的 Agent TemplateTemplate 名称适用场景特点hermes通用 Agent 训练使用tool_callXML 标签react_enReAct 格式英文Action/Action Input/Observation 格式react_zhReAct 格式中文中文版 ReActqwen_enQwen 官方格式英文使用 ✿FUNCTION✿ 等特殊标记qwen_zhQwen 官方格式中文中文版 Qwen 格式qwen3_coderQwen3-Coder 专用使用function...格式qwen3_5Qwen3.5 专用基于 qwen3_coder 优化glm4GLM4 系列GLM4 官方格式llama3/llama4Llama 系列Llama 官方格式deepseek_v3_1DeepSeek V3.1DeepSeek 官方格式5.2 Hermes 格式示例推荐使用agent_templatehermes时数据会被编码为[INPUT_IDS] |im_start|systemYou are Qwen, created by Alibaba Cloud. You are a helpful assistant.# ToolsYou may call one or more functions to assist with the user query.You are provided with function signatures within tools/tools XML tags:tools{type: function, function: {name: realtime_aqi, description: 天气预报。获取实时空气质量, parameters: {...}}}/toolsFor each function call, return a json object with function name and arguments within tool_call/tool_call XML tags:tool_call{name: function-name, arguments: args-json-object}/tool_call|im_end||im_start|user北京和上海今天的天气情况|im_end||im_start|assistanttool_call{name: realtime_aqi, arguments: {city: 北京}}/tool_calltool_call{name: realtime_aqi, arguments: {city: 上海}}/tool_call|im_end||im_start|usertool_response{city: 北京, aqi: 10, unit: celsius}/tool_responsetool_response{city: 上海, aqi: 72, unit: fahrenheit}/tool_response|im_end||im_start|assistant根据天气预报工具北京今天的空气质量指数为10属于良好水平上海今天的空气质量指数为72属于轻度污染水平。|im_end|5.3 ReAct 格式示例使用agent_templatereact_en时[INPUT_IDS] |im_start|systemAnswer the following questions as best you can. You have access to the following tools:realtime_aqi: Call this tool to interact with the realtime_aqi API. What is the realtime_aqi API useful for? 天气预报。获取实时空气质量 Parameters: {...} Format the arguments as a JSON object.Use the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [realtime_aqi]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can be repeated zero or more times)Thought: I now know the final answerFinal Answer: the final answer to the original input questionBegin!|im_end||im_start|user北京和上海今天的天气情况|im_end||im_start|assistantAction: realtime_aqiAction Input: {city: 北京}Action: realtime_aqiAction Input: {city: 上海}Observation:{city: 北京, aqi: 10, unit: celsius}Observation:{city: 上海, aqi: 72, unit: fahrenheit}根据天气预报工具北京今天的空气质量指数为10属于良好水平上海今天的空气质量指数为72属于轻度污染水平。|im_end|5.4 Qwen3.5 专用格式Qwen3.5 使用qwen3_5agent template格式如下# ToolsYou have access to the following functions:tools{type: function, function: {name: get_weather, ...}}/toolsIf you choose to call a function ONLY reply in the following format with NO suffix:tool_callfunctionexample_function_nameparameterexample_parameter_1value_1/parameterparameterexample_parameter_2This is the value for the second parameterthat can spanmultiple lines/parameter/function/tool_callIMPORTANTReminder:- Function calls MUST follow the specified format- Required parameters MUST be specified- You may provide optional reasoning BEFORE the function call, but NOT after/IMPORTANT六、损失权重控制loss_scaleAgent 训练中可以使用loss_scale参数调节不同部分的损失权重6.1 ReAct 格式的 loss_scale使用--loss_scale react字段损失权重Action:及后续内容2Action Input:及后续内容2Thought:及后续内容1Final Answer:及后续内容1Observation:本身2Observation:后的工具结果0不计算损失6.2 忽略空思维块使用--loss_scale ignore_empty_think可忽略think\n\n/think\n\n的损失计算这在训练推理模型时非常有用。七、Qwen3.5 Agent 训练实战7.1 训练命令示例CUDA_VISIBLE_DEVICES0 \swift sft \ --model Qwen/Qwen3.5-4B \ --tuner_type lora \ --dataset AI-ModelScope/function-calling-chatml \ --agent_template qwen3_5 \ --load_from_cache_file true \ --split_dataset_ratio 0.01 \ --add_non_thinking_prefix true \ --loss_scale ignore_empty_think \ --torch_dtype bfloat16 \ --num_train_epochs 2 \ --per_device_train_batch_size 4 \ --learning_rate 1e-4 \ --lora_rank 8 \ --lora_alpha 32 \ --target_modules all-linear \ --max_length 8192 \ --output_dir output/Qwen3.5-4B-Agent7.2 关键参数说明参数说明--agent_template qwen3_5使用 Qwen3.5 专用 Agent 模板--loss_scale ignore_empty_think忽略空思维块的损失--add_non_thinking_prefix true添加非思考前缀--dataset可使用AI-ModelScope/function-calling-chatml等 Agent 数据集八、常用 Agent 数据集ms-swift 内置支持多个 Agent 相关数据集数据集说明AI-ModelScope/function-calling-chatml函数调用数据集AI-ModelScope/ms_agent_for_agentfabricAgentFabric 数据集damo/MSAgent-BenchMSAgent 基准数据集iic/ms_agentMS Agent 数据集iic/MSAgent-ProMS Agent Pro 数据集iic/MSAgent-MultiRole多角色 Agent 数据集swift/ToolBench工具调用基准数据集LLM-Research/xlam-function-calling-60k60K 函数调用数据集huangjintao/AgentInstruct_copyAgent 指令数据集九、总结9.1 数据格式选择建议场景推荐格式通用 SFTmessages 格式Agent 训练带 tools 的 messages 格式 qwen3_5/hermes template多模态训练messages 格式 images/videos/audios 字段RLHF 训练messages 格式 rejected_response/label 字段9.2 Agent 微调核心要点数据格式统一使用 ms-swift 标准 Agent 格式通过agent_template自动转换模板解耦同一数据集可切换不同agent_template适配不同模型损失控制使用loss_scale控制不同部分的训练权重并行调用支持多工具并行调用训练多模态支持支持图片、视频、音频等多模态 Agent 场景2026年AI行业最大的机会毫无疑问就在应用层字节跳动已有7个团队全速布局Agent大模型岗位暴增69%年薪破百万腾讯、京东、百度开放招聘技术岗80%与AI相关……如今超过60%的企业都在推进AI产品落地而真正能交付项目的大模型应用开发工程师****却极度稀缺落地AI应用绝对不是写几个prompt调几个API就能搞定的企业真正需要的是能搞定这三项核心能力的人✅RAG融入外部信息修正模型输出给模型装靠谱大脑✅Agent智能体让AI自主干活通过工具调用Tools环境交互多步推理完成复杂任务。比如做智能客服等等……✅微调针对特定任务优化让模型适配业务目前脉脉上有超过1000家企业发布大模型相关岗位人工智能岗平均月薪7.8w实习生日薪高达4000远超其他行业收入水平技术的稀缺性才是你「值钱」的关键具备AI能力的程序员比传统开发高出不止一截有的人早就转行AI方向拿到百万年薪AI浪潮正在重构程序员的核心竞争力现在入场仍是最佳时机我把大模型的学习全流程已经整理好了抓住AI时代风口轻松解锁职业新可能希望大家都能把握机遇实现薪资/职业跃迁这份完整版的大模型 AI 学习资料已经上传CSDN朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】⭐️从大模型微调到AI Agent智能体搭建剖析AI技术的应用场景用实战经验落地AI技术。从GPT到最火的开源模型让你从容面对AI技术革新大模型微调掌握主流大模型如DeepSeek、Qwen等的微调技术针对特定场景优化模型性能。学习如何利用领域数据如制造、医药、金融等进行模型定制提升任务准确性和效率。RAG应用开发深入理解检索增强生成Retrieval-Augmented Generation, RAG技术构建高效的知识检索与生成系统。应用于垂类场景如法律文档分析、医疗诊断辅助、金融报告生成等实现精准信息提取与内容生成。AI Agent智能体搭建学习如何设计和开发AI Agent实现多任务协同、自主决策和复杂问题解决。构建垂类场景下的智能助手如制造业中的设备故障诊断Agent、金融领域的投资分析Agent等。如果你也有以下诉求快速链接产品/业务团队参与前沿项目构建技术壁垒从竞争者中脱颖而出避开35岁裁员危险期顺利拿下高薪岗迭代技术水平延长未来20年的新职业发展……那这节课你一定要来听因为留给普通程序员的时间真的不多了立即扫码即可免费预约「AI技术原理 实战应用 职业发展」「大模型应用开发实战公开课」还有靠谱的内推机会直聘权益完课后赠送大模型应用案例集、AI商业落地白皮书
Qwen3.5微调全攻略:SFT、RLHF、多模态到Agent,20+模板+实战代码,速成大模型高手!
发布时间:2026/5/15 19:41:28
一文搞定Qwen3.5微调全流程从SFT、RLHF、多模态到Agent微调覆盖全场景数据格式深度解析20 Agent模板并附开箱即用训练代码助你快速上手实战。一、概述为什么需要这篇文章在微调 Qwen3.5 模型时你是否遇到过这些困惑❓ 数据格式五花八门不知道该用哪种❓ Agent 微调到底是什么如何让模型学会调用工具❓ 20 种 Agent Template 该如何选择❓ 多模态、RLHF、工具调用的数据该怎么准备本文基于 ms-swift阿里巴巴 ModelScope 官方微调框架为你提供✅4 种自动转换数据格式- messages/sharegpt/alpaca/query-response 一键适配✅全场景数据格式- 预训练、SFT、DPO/KTO/GRPO、多模态、Agent 全覆盖✅Agent 微调深度解析- 原理、数据格式、20 Template 对比、实战代码✅开箱即用的训练脚本- 复制粘贴即可运行适合人群AI 研究者、算法工程师、大模型应用开发者二、ms-swift 标准数据集格式ms-swift 的标准数据集格式可接受的 keys 包括Key说明是否必需messages对话消息列表必需rejected_response用于 DPO 等 RLHF 训练可选label用于 KTO 训练和分类模型训练可选images多模态图片路径/URL可选videos多模态视频路径/URL可选audios多模态音频路径/URL可选toolsAgent 任务的工具定义可选objectsgrounding 任务可选2.1 四种可自动转换的数据格式ms-swift 的AutoPreprocessor支持将以下四种格式自动转换为标准格式1) messages 格式标准格式{messages: [{role: system, content: system}, {role: user, content: query1}, {role: assistant, content: response1}, {role: user, content: query2}, {role: assistant, content: response2}]}2) sharegpt 格式{system: system, conversation: [{human: query1, assistant: response1}, {human: query2, assistant: response2}]}3) query-response 格式{system: system, query: query2, response: response2, history: [[query1, response1]]}自动字段映射system:system,system_promptquery:query,prompt,input,instruction,question,problemresponse:response,answer,output,targets,target,answer_key,answers,solution,text,completion,content4) alpaca 格式{system: system, instruction: query-inst, input: query-input, output: response}注意instruction和input字段将组合成 query 字段。若两者都不为空query f{instruction}\n{input}三、各场景数据格式详解3.1 预训练数据格式{messages: [{role: assistant, content: I love music}]}{messages: [{role: assistant, content: 教练我要打篮球}]}{messages: [{role: assistant, content: 西红柿鸡蛋盖饭和地三鲜盖饭哪个更权威}]}3.2 监督微调SFT数据格式{messages: [{role: system, content: 你是个有用无害的助手}, {role: user, content: 告诉我明天的天气}, {role: assistant, content: 明天天气晴朗}]}{messages: [{role: system, content: 你是个有用无害的数学计算器}, {role: user, content: 11等于几}, {role: assistant, content: 等于2}, {role: user, content: 再加1呢}, {role: assistant, content: 等于3}]}控制损失计算可通过loss字段控制对应回复是否计算损失{messages: [{role: user, content: 你好}, {role: assistant, content: 你好有什么可以帮助你的吗, loss: false}, {role: user, content: 11等于几}, {role: assistant, content: 等于2, loss: true}]}3.3 RLHF 数据格式DPO/ORPO/CPO/SimPO/RM{messages: [{role: system, content: 你是个有用无害的助手}, {role: user, content: 告诉我明天的天气}, {role: assistant, content: 明天天气晴朗}], rejected_response: 我不知道}KTO{messages: [{role: user, content: 告诉我明天的天气}, {role: assistant, content: 我不知道}], label: false}{messages: [{role: user, content: 11等于几}, {role: assistant, content: 等于2}], label: true}PPO/GRPO{messages: [{role: system, content: 你是个有用无害的助手}, {role: user, content: 告诉我明天的天气}]}{messages: [{role: user, content: 11等于几}, {role: assistant, content: 等于2}, {role: user, content: 再加1呢}]}3.4 多模态数据格式{messages: [{role: user, content: 浙江的省会在哪}, {role: assistant, content: 浙江的省会在杭州。}]}{messages: [{role: user, content: imageimage两张图片有什么区别}, {role: assistant, content: 前一张是小猫后一张是小狗}], images: [/xxx/x.jpg, /xxx/x.png]}{messages: [{role: user, content: audio语音说了什么}, {role: assistant, content: 今天天气真好呀}], audios: [/xxx/x.mp3]}{messages: [{role: user, content: image图片中是什么video视频中是什么}, {role: assistant, content: 图片中是一个大象视频中是一只小狗在草地上奔跑}], images: [/xxx/x.jpg], videos: [/xxx/x.mp4]}特殊标签说明image- 图片插入位置video- 视频插入位置audio- 音频插入位置四、Agent 微调详解4.1 什么是 Agent 微调Agent 微调是让大语言模型具备工具调用能力的训练过程。通过 Agent 微调模型可以理解工具描述解析 API 的功能、参数和使用方法决策工具调用判断何时需要调用工具选择合适的工具生成调用参数根据用户需求生成正确的工具调用参数整合工具结果将工具返回的结果整合到最终回答中4.2 Agent 微调解决的核心问题问题类型描述Agent 微调的解决方案能力边界LLM 知识有截止日期无法获取实时信息通过调用外部 API 获取实时数据计算精度LLM 数学计算能力有限调用计算器等专业工具专业任务无法执行代码、操作文件等调用代码执行器、文件系统等工具多模态交互需要与外部系统交互调用各类 API 完成复杂任务任务分解复杂任务需要多步骤完成支持多轮工具调用和结果整合4.3 Agent 数据集格式ms-swift 使用agent-template实现了 Agent 数据格式与模型的解耦基于统一的数据集格式可以灵活切换不同模型进行训练。纯文本 Agent 数据样本{ tools: [{\type\: \function\, \function\: {\name\: \realtime_aqi\, \description\: \天气预报。获取实时空气质量。当前空气质量PM2.5PM10信息\, \parameters\: {\type\: \object\, \properties\: {\city\: {\type\: \string\, \description\: \城市名例如上海\}}, \required\: [\city\]}}}], messages: [ {role: user, content: 北京和上海今天的天气情况}, {role: tool_call, content: {\name\: \realtime_aqi\, \arguments\: {\city\: \北京\}}}, {role: tool_call, content: {\name\: \realtime_aqi\, \arguments\: {\city\: \上海\}}}, {role: tool_response, content: {\city\: \北京\, \aqi\: \10\, \unit\: \celsius\}}, {role: tool_response, content: {\city\: \上海\, \aqi\: \72\, \unit\: \fahrenheit\}}, {role: assistant, content: 根据天气预报工具北京今天的空气质量指数为10属于良好水平上海今天的空气质量指数为72属于轻度污染水平。} ]}多模态 Agent 数据样本{ tools: [{\type\: \function\, \function\: {\name\: \click\, \description\: \点击屏幕中的某个位置\, \parameters\: {\type\: \object\, \properties\: {\x\: {\type\: \integer\, \description\: \横坐标\}, \y\: {\type\: \integer\, \description\: \纵坐标\}}, \required\: [\x\, \y\]}}}], messages: [ {role: user, content: image现在几点了}, {role: assistant, content: think\n我可以通过打开日历App来获取当前时间。\n/think\n}, {role: tool_call, content: {\name\: \click\, \arguments\: {\x\: 105, \y\: 132}}}, {role: tool_response, content: {\images\: \image\, \status\: \success\}}, {role: assistant, content: 成功打开日历App现在的时间为中午11点} ], images: [desktop.png, calendar.png]}4.4 Agent 数据格式关键要素字段说明toolsJSON 字符串包含工具列表定义role: tool_call模型发起的工具调用content 为 JSON 字符串role: tool_response工具返回的结果也可写成role: toolrole: assistant模型的文本回复重要特性并行工具调用支持连续多个tool_call如上例中同时查询北京和上海混合输出支持assistant和tool_call混合出现多模态支持image标签数量应与images长度相同4.5 tools 字段格式tools [{ type: function, function: { name: get_current_weather, description: Get the current weather in a given location, parameters: { type: object, properties: { location: { type: string, description: The city and state, e.g. San Francisco, CA }, unit: { type: string, enum: [celsius, fahrenheit] } }, required: [location] } }}]五、Agent Template 详解ms-swift 支持多种 Agent Template实现了数据格式与模型的解耦5.1 支持的 Agent TemplateTemplate 名称适用场景特点hermes通用 Agent 训练使用tool_callXML 标签react_enReAct 格式英文Action/Action Input/Observation 格式react_zhReAct 格式中文中文版 ReActqwen_enQwen 官方格式英文使用 ✿FUNCTION✿ 等特殊标记qwen_zhQwen 官方格式中文中文版 Qwen 格式qwen3_coderQwen3-Coder 专用使用function...格式qwen3_5Qwen3.5 专用基于 qwen3_coder 优化glm4GLM4 系列GLM4 官方格式llama3/llama4Llama 系列Llama 官方格式deepseek_v3_1DeepSeek V3.1DeepSeek 官方格式5.2 Hermes 格式示例推荐使用agent_templatehermes时数据会被编码为[INPUT_IDS] |im_start|systemYou are Qwen, created by Alibaba Cloud. You are a helpful assistant.# ToolsYou may call one or more functions to assist with the user query.You are provided with function signatures within tools/tools XML tags:tools{type: function, function: {name: realtime_aqi, description: 天气预报。获取实时空气质量, parameters: {...}}}/toolsFor each function call, return a json object with function name and arguments within tool_call/tool_call XML tags:tool_call{name: function-name, arguments: args-json-object}/tool_call|im_end||im_start|user北京和上海今天的天气情况|im_end||im_start|assistanttool_call{name: realtime_aqi, arguments: {city: 北京}}/tool_calltool_call{name: realtime_aqi, arguments: {city: 上海}}/tool_call|im_end||im_start|usertool_response{city: 北京, aqi: 10, unit: celsius}/tool_responsetool_response{city: 上海, aqi: 72, unit: fahrenheit}/tool_response|im_end||im_start|assistant根据天气预报工具北京今天的空气质量指数为10属于良好水平上海今天的空气质量指数为72属于轻度污染水平。|im_end|5.3 ReAct 格式示例使用agent_templatereact_en时[INPUT_IDS] |im_start|systemAnswer the following questions as best you can. You have access to the following tools:realtime_aqi: Call this tool to interact with the realtime_aqi API. What is the realtime_aqi API useful for? 天气预报。获取实时空气质量 Parameters: {...} Format the arguments as a JSON object.Use the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [realtime_aqi]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can be repeated zero or more times)Thought: I now know the final answerFinal Answer: the final answer to the original input questionBegin!|im_end||im_start|user北京和上海今天的天气情况|im_end||im_start|assistantAction: realtime_aqiAction Input: {city: 北京}Action: realtime_aqiAction Input: {city: 上海}Observation:{city: 北京, aqi: 10, unit: celsius}Observation:{city: 上海, aqi: 72, unit: fahrenheit}根据天气预报工具北京今天的空气质量指数为10属于良好水平上海今天的空气质量指数为72属于轻度污染水平。|im_end|5.4 Qwen3.5 专用格式Qwen3.5 使用qwen3_5agent template格式如下# ToolsYou have access to the following functions:tools{type: function, function: {name: get_weather, ...}}/toolsIf you choose to call a function ONLY reply in the following format with NO suffix:tool_callfunctionexample_function_nameparameterexample_parameter_1value_1/parameterparameterexample_parameter_2This is the value for the second parameterthat can spanmultiple lines/parameter/function/tool_callIMPORTANTReminder:- Function calls MUST follow the specified format- Required parameters MUST be specified- You may provide optional reasoning BEFORE the function call, but NOT after/IMPORTANT六、损失权重控制loss_scaleAgent 训练中可以使用loss_scale参数调节不同部分的损失权重6.1 ReAct 格式的 loss_scale使用--loss_scale react字段损失权重Action:及后续内容2Action Input:及后续内容2Thought:及后续内容1Final Answer:及后续内容1Observation:本身2Observation:后的工具结果0不计算损失6.2 忽略空思维块使用--loss_scale ignore_empty_think可忽略think\n\n/think\n\n的损失计算这在训练推理模型时非常有用。七、Qwen3.5 Agent 训练实战7.1 训练命令示例CUDA_VISIBLE_DEVICES0 \swift sft \ --model Qwen/Qwen3.5-4B \ --tuner_type lora \ --dataset AI-ModelScope/function-calling-chatml \ --agent_template qwen3_5 \ --load_from_cache_file true \ --split_dataset_ratio 0.01 \ --add_non_thinking_prefix true \ --loss_scale ignore_empty_think \ --torch_dtype bfloat16 \ --num_train_epochs 2 \ --per_device_train_batch_size 4 \ --learning_rate 1e-4 \ --lora_rank 8 \ --lora_alpha 32 \ --target_modules all-linear \ --max_length 8192 \ --output_dir output/Qwen3.5-4B-Agent7.2 关键参数说明参数说明--agent_template qwen3_5使用 Qwen3.5 专用 Agent 模板--loss_scale ignore_empty_think忽略空思维块的损失--add_non_thinking_prefix true添加非思考前缀--dataset可使用AI-ModelScope/function-calling-chatml等 Agent 数据集八、常用 Agent 数据集ms-swift 内置支持多个 Agent 相关数据集数据集说明AI-ModelScope/function-calling-chatml函数调用数据集AI-ModelScope/ms_agent_for_agentfabricAgentFabric 数据集damo/MSAgent-BenchMSAgent 基准数据集iic/ms_agentMS Agent 数据集iic/MSAgent-ProMS Agent Pro 数据集iic/MSAgent-MultiRole多角色 Agent 数据集swift/ToolBench工具调用基准数据集LLM-Research/xlam-function-calling-60k60K 函数调用数据集huangjintao/AgentInstruct_copyAgent 指令数据集九、总结9.1 数据格式选择建议场景推荐格式通用 SFTmessages 格式Agent 训练带 tools 的 messages 格式 qwen3_5/hermes template多模态训练messages 格式 images/videos/audios 字段RLHF 训练messages 格式 rejected_response/label 字段9.2 Agent 微调核心要点数据格式统一使用 ms-swift 标准 Agent 格式通过agent_template自动转换模板解耦同一数据集可切换不同agent_template适配不同模型损失控制使用loss_scale控制不同部分的训练权重并行调用支持多工具并行调用训练多模态支持支持图片、视频、音频等多模态 Agent 场景2026年AI行业最大的机会毫无疑问就在应用层字节跳动已有7个团队全速布局Agent大模型岗位暴增69%年薪破百万腾讯、京东、百度开放招聘技术岗80%与AI相关……如今超过60%的企业都在推进AI产品落地而真正能交付项目的大模型应用开发工程师****却极度稀缺落地AI应用绝对不是写几个prompt调几个API就能搞定的企业真正需要的是能搞定这三项核心能力的人✅RAG融入外部信息修正模型输出给模型装靠谱大脑✅Agent智能体让AI自主干活通过工具调用Tools环境交互多步推理完成复杂任务。比如做智能客服等等……✅微调针对特定任务优化让模型适配业务目前脉脉上有超过1000家企业发布大模型相关岗位人工智能岗平均月薪7.8w实习生日薪高达4000远超其他行业收入水平技术的稀缺性才是你「值钱」的关键具备AI能力的程序员比传统开发高出不止一截有的人早就转行AI方向拿到百万年薪AI浪潮正在重构程序员的核心竞争力现在入场仍是最佳时机我把大模型的学习全流程已经整理好了抓住AI时代风口轻松解锁职业新可能希望大家都能把握机遇实现薪资/职业跃迁这份完整版的大模型 AI 学习资料已经上传CSDN朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】⭐️从大模型微调到AI Agent智能体搭建剖析AI技术的应用场景用实战经验落地AI技术。从GPT到最火的开源模型让你从容面对AI技术革新大模型微调掌握主流大模型如DeepSeek、Qwen等的微调技术针对特定场景优化模型性能。学习如何利用领域数据如制造、医药、金融等进行模型定制提升任务准确性和效率。RAG应用开发深入理解检索增强生成Retrieval-Augmented Generation, RAG技术构建高效的知识检索与生成系统。应用于垂类场景如法律文档分析、医疗诊断辅助、金融报告生成等实现精准信息提取与内容生成。AI Agent智能体搭建学习如何设计和开发AI Agent实现多任务协同、自主决策和复杂问题解决。构建垂类场景下的智能助手如制造业中的设备故障诊断Agent、金融领域的投资分析Agent等。如果你也有以下诉求快速链接产品/业务团队参与前沿项目构建技术壁垒从竞争者中脱颖而出避开35岁裁员危险期顺利拿下高薪岗迭代技术水平延长未来20年的新职业发展……那这节课你一定要来听因为留给普通程序员的时间真的不多了立即扫码即可免费预约「AI技术原理 实战应用 职业发展」「大模型应用开发实战公开课」还有靠谱的内推机会直聘权益完课后赠送大模型应用案例集、AI商业落地白皮书