
Qwen3-Coder 开源代码模型实战指南从 Agentic Coding 到本地部署与代码补全【免费下载链接】Qwen3-CoderQwen3-Coder is the code version of Qwen3, the large language model series developed by Qwen team.项目地址: https://gitcode.com/GitHub_Trending/co/Qwen3-Coder导读本文以 Qwen3-Coder 开源仓库根目录 README.md 为主体系统讲解 Qwen3-Coder 模型系列的定位、核心能力、模型选型与下载方式并完整演示基于transformers的对话调用、Fill-in-the-MiddleFIM代码补全等实战用法同时结合仓库内 examples 与 demo 目录中的可运行示例进行源码级印证。读者读完将掌握如何在本地加载 Qwen3-Coder-Next 并完成聊天与代码补全、如何在编码 Agent 平台中发挥其函数调用能力以及如何借助仓库内的评测目录验证模型在多个代码基准上的表现。一、模型系列概览Qwen3-Coder 家族与 Qwen3-Coder-NextQwen3-Coder 是 Qwen 团队面向代码场景发布的大语言模型系列官方将其定位为迄今最具 Agent 能力most agentic的代码模型提供多个规格供不同硬件与场景选择模型类型上下文长度Qwen3-Coder-Nextinstruct256kQwen3-Coder-Next-Basebase256kQwen3-Coder-480B-A35B-Instructinstruct256kQwen3-Coder-30B-A3B-Instructinstruct256kQwen3-Coder-Next-FP8instruct256kQwen3-Coder-Next-GGUFinstruct256kQwen3-Coder-480B-A35B-Instruct-FP8instruct256kQwen3-Coder-30B-A3B-Instruct-FP8instruct256k其中Qwen3-Coder-Next是专门面向编码 Agent 与本地开发场景设计的开源权重模型它基于Qwen3-Next-80B-A3B-Base构建后者采用了混合注意力hybrid attention MoE的新型架构。Qwen3-Coder-Next 在大规模可执行任务合成、环境交互与强化学习上进行了规模化训练从而在显著降低推理成本的同时获得较强的编码与 Agent 能力。从仓库结构看本项目围绕该模型构建了完整的配套生态examples/目录提供对话、流式、FIM、仓库级补全等可直接运行的示例脚本qwencoder-eval/目录沉淀了 base/instruct/reasoning/tool_calling 等多类评测基准的实现与结果finetuning/目录则包含 SFT 与 DPO 的完整训练脚本详见下文第五、七节。二、三大关键特性2.1 效率与性能的平衡Efficiency-Performance Tradeoff官方在开放模型阵营中将 Qwen3-Coder 与 Claude Sonnet 在Agentic Coding、Agentic Browser-Use以及其他基础编码任务上的表现进行对比认为其取得了与之相当的效果。这是一个来自 README 的官方表述读者可将仓库内 qwencoder-eval 目录作为进一步验证其具体评测数据的入口。2.2 面向 Agent 的规模化编码Scaling Agentic CodingQwen3-Coder 支持绝大多数主流编码 Agent 平台包括Qwen Code、CLINE、Claude Code等并为此专门设计了函数调用function calling格式。README 特别强调了一个重要前提Qwen3-Coder 的函数调用依赖 SGLang 与 vLLM 中新的工具解析器tool parser同时官方更新了特殊 token 及其对应的 token id 以与 Qwen3 保持一致因此务必使用新的 tokenizer。这也解释了仓库根目录 requirements.txt 中除torch、transformers、accelerate、safetensors之外还显式列出了vllm的原因——vLLM 正是官方推荐的函数调用推理后端之一。2.3 长上下文能力Long-context Capabilities模型原生支持256Ktoken 的上下文长度并可通过 Yarn 扩展到1Mtoken官方明确将其定位为面向仓库级repository-scale理解能力而优化。在后续章节我们会看到仓库的examples中专门提供了仓库级补全repo-level FIM示例正是对这一能力的直接印证。三、基础信息256K 上下文与 358 种编程语言Qwen3-Coder 的基础能力可归纳为三点长上下文支持 256K token 的上下文理解与生成多语言覆盖支持358 种编程语言完整语言清单在 README.md 中以折叠列表形式给出涵盖 ABAP、C/C/C#、Clojure、Dart、Go、Java、JavaScript、Kotlin、Lua、OCaml、PHP、Python、R、Ruby、Rust、Scala、Solidity、Swift、TypeScript、Vue、Zig 等主流与冷门语言能力保留保留基座模型在数学与通用任务上的优势。四、快速开始用 transformers 与 Qwen3-Coder-Next 聊天[!IMPORTANT]Qwen3-Coder 系列均为 instruct指令微调模型用于对话场景。该模型仅支持非思考non-thinking模式输出中不会生成think/think块因此调用时无需再指定enable_thinkingFalse。4.1 最小可运行示例官方推荐直接用transformers完成对话先用from_pretrained构建 tokenizer 与模型再借助 tokenizer 自带的 chat template通过generate方法生成回复。以下为官方给出的与Qwen3-Coder-Next聊天的完整代码from transformers import AutoModelForCausalLM, AutoTokenizer model_name Qwen/Qwen3-Coder-Next model AutoModelForCausalLM.from_pretrained( model_name, torch_dtypeauto, device_mapauto ) tokenizer AutoTokenizer.from_pretrained(model_name) prompt write a quick sort algorithm. messages [ {role: user, content: prompt} ] text tokenizer.apply_chat_template( messages, tokenizeFalse, add_generation_promptTrue ) model_inputs tokenizer([text], return_tensorspt).to(model.device) generated_ids model.generate( **model_inputs, max_new_tokens65536 ) generated_ids [ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids) ] response tokenizer.batch_decode(generated_ids, skip_special_tokensTrue)[0]各关键参数的作用说明如下apply_chat_template()将messages列表转换为模型可理解的对话格式。add_generation_promptTrue会在输入末尾追加生成提示符|im_start|assistant\n——与 Qwen 系列一贯做法一致Qwen3-Coder 使用ChatML模板max_new_tokens控制回复的最大输出长度官方示例中设为 65536用于支撑长代码生成tokenizer.batch_decode(..., skip_special_tokensTrue)将生成的 token id 解码回文本并去除特殊 tokenmessages结构上述{role: user, content: ...}是格式化对话历史与系统提示的标准写法可按需扩展为多轮对话。其他规格的 instruct 模型如 Qwen3-Coder-30B-A3B-Instruct、Qwen3-Coder-480B-A35B-Instruct可使用完全相同的调用方式。4.2 与仓库示例的对照印证仓库 examples/Qwen2.5-Coder-Instruct.py 给出了同一模式的完整实现它同样使用tokenizer.apply_chat_template(messages, tokenizeFalse, add_generation_promptTrue)格式化输入随后model.generate()生成再通过tokenizer.batch_decode(..., skip_special_tokensTrue)取回文本并给出了系统提示写法You are Qwen, created by Alibaba Cloud. You are a helpful assistant.。该文件中的注释还明确指出现在无需再添加trust_remote_codeTrue直接使用from_pretrained即可。4.3 进阶流式输出Streaming对于需要逐 token 展示输出如构建类 ChatGPT 交互体验的场景仓库 examples/Qwen2.5-Coder-Instruct-stream.py 提供了基于TextIteratorStreamer的流式方案将streamer传入model.generate的generation_kwargs在独立线程中启动生成主线程通过迭代streamer逐段打印结果。核心代码如下streamer TextIteratorStreamer(tokenizer, skip_promptTrue, skip_special_tokensTrue) generation_kwargs dict(inputsmodel_inputs.input_ids, streamerstreamer, max_new_tokens2048) thread Thread(targetmodel.generate, kwargsgeneration_kwargs) thread.start() for new_text in streamer: print(new_text, end)4.4 进阶API 托管式聊天应用如果希望以 Web 聊天界面方式快速体验仓库 demo/chatbot/app.py 提供了一个基于 Gradio 的完整示例通过dashscope的Generation.call以流式方式调用模型并内置温度0.0~1.0默认 0.5、Top-p0.6~1.0默认 0.9、最大长度512~8192默认 2048等采样参数滑块以及 System Prompt 编辑、历史记录清理等功能。该示例展示的是通过 DashScope API 以qwen2.5-coder-32b-instruct为模型名的调用链路可作为自建 ChatBot 服务的参考模板。五、核心编码能力Fill-in-the-Middle 代码补全5.1 FIM 是什么代码插入任务code insertion即fill-in-the-middleFIM要求模型在给定代码上下文的缺口中补全缺失的代码段。README 推荐遵循论文Efficient Training of Language Models to Fill in the Middle中描述的格式约定来构造 prompt。[!IMPORTANT] FIM 能力在Qwen3-Coder 的所有版本中均得到支持下文以 Qwen3-Coder-Next 作为示例说明。5.2 FIM 的 Prompt 结构FIM prompt 必须按照以下结构拼接prompt |fim_prefix| prefix_code |fim_suffix| suffix_code |fim_middle|即以prefix_code缺口前的代码、suffix_code缺口后的代码分别包裹在|fim_prefix|与|fim_suffix|之间模型需要生成的部分从|fim_middle|开始。5.3 完整 FIM 示例官方以下为官方给出的、可直接运行的 FIM 补全示例以快速排序函数为素材from transformers import AutoTokenizer, AutoModelForCausalLM # load model device cuda # the device to load the model onto TOKENIZER AutoTokenizer.from_pretrained(Qwen/Qwen3-Coder-Next) MODEL AutoModelForCausalLM.from_pretrained(Qwen/Qwen3-Coder-Next, device_mapauto).eval() input_text |fim_prefix|def quicksort(arr): if len(arr) 1: return arr pivot arr[len(arr) // 2] |fim_suffix| middle [x for x in arr if x pivot] right [x for x in arr if x pivot] return quicksort(left) middle quicksort(right)|fim_middle| messages [ {role: system, content: You are a code completion assistant.}, {role: user, content: input_text} ] text tokenizer.apply_chat_template( messages, tokenizeFalse, add_generation_promptTrue ) model_inputs TOKENIZER([text], return_tensorspt).to(model.device) # Use max_new_tokens to control the maximum output length. eos_token_ids [151659, 151661, 151662, 151663, 151664, 151643, 151645] generated_ids MODEL.generate(model_inputs.input_ids, max_new_tokens512, do_sampleFalse, eos_token_ideos_token_ids)[0] # The generated_ids include prompt_ids, we only need to decode the tokens after prompt_ids. output_text TOKENIZER.decode(generated_ids[len(model_inputs.input_ids[0]):], skip_special_tokensTrue) print(fPrompt: {input_text}\n\nGenerated text: {output_text})5.4 FIM 关键细节解读eos_token_ids列表[151659, 151661, 151662, 151663, 151664, 151643, 151645]是 FIM 场景下需要显式指定的一组结束 token id用于让模型在补全位置而非对话结束位置及时停止生成。这些 id 与fim_prefix/suffix/middle等特殊 token 对应是保证补全截断位置正确的关键do_sampleFalse关闭采样、使用贪心解码保证补全结果确定可复现截断 prompt 部分generated_ids包含 prompt ids解码时需通过generated_ids[len(model_inputs.input_ids[0]):]仅保留模型新生成的 token。5.5 仓库中的同源示例与仓库级补全repo-level FIM仓库 examples/Qwen2.5-Coder-fim.py 提供与官方示例完全同构的可运行脚本模型名改为 Qwen2.5-Coder-32B可作为脱离对话模板、直接输入 FIM 文本的极简参照。更进一步仓库还给出了**仓库级补全repo-level**的进阶玩法examples/Qwen2.5-Coder-repolevel.py使用|repo_name|library-system声明仓库名再用多个|file_sep|xxx.py分段注入整个仓库的多个文件如library.py、student.py、main.py的现有代码让模型在理解整个项目上下文后继续补写main.py中未完成的函数体examples/Qwen2.5-Coder-repolevel-fim.py将 repo-level 格式与 FIM 结合——在某个文件段内用|fim_prefix|...|fim_suffix|...|fim_middle|指定缺口实现基于多文件仓库上下文的定向补全。这种仓库名 文件分隔符 多文件上下文的组织方式正是 Qwen3-Coder 原生 256K 长上下文在真实工程场景跨文件理解、仓库级代码生成中的应用范例。六、实战用例Agentic Coding 能力展示README 给出了六个真实用例均以一句话自然语言 prompt 驱动编码 Agent 完成端到端任务覆盖网页发布、桌面自动化、游戏开发、创意工具、网站测试与 Web 应用等场景。以下逐一还原其 prompt 原文。6.1 示例发布一个网站OpenClaw 驱动以 OpenClaw 作为 Agent 框架prompt 要求 Agent 自主收集历史信息、撰写网页并用 nginx 完成发布next week we will release new coder model, can you collect the history of qwen coder and write a web page, the release the website with the nginx, you can seach how to do this in alibaba cloud linux first6.2 示例桌面整理Qwen Code 驱动以 Qwen Code 作为 Agentprompt 仅有一句话Please tidy up my desk.6.3 示例僵尸大战植物网页游戏Claude Code 驱动这是 README 中最复杂的用例一段结构化中文需求完整定义了反向塔防玩法、5×9 网格地图、双方单位数值表、战斗逻辑、交互设计与胜负条件。其 prompt 结构极具代表性值得作为用自然语言描述完整游戏需求的模板参考全文较长核心要点如下核心机制玩家扮演僵尸方从右侧部署区向左进攻初始 300 脑子点数僵尸吃掉植物返还 100 点形成经济循环120 秒倒计时内清光植物获胜单位系统僵尸方含普通50 脑/100HP、路障100 脑/200HP、铁桶150 脑/400HP、冲刺80 脑/80HP四类植物方含豌豆射手、双发射手、坚果墙、向日葵四类各有明确的 HP/伤害/攻击间隔数值战斗逻辑50px 碰撞检测触发啃食、30 帧/次咬击、弹道物理、路径 AI 等交互与胜负条件右侧卡片式 UI、资源不足置灰、悬停半透明预览圈、实时三色血条以及plants.length 0 timeLeft 0的胜利判定。6.4 示例声音 ASCII 艺术工具Cline 驱动以 Cline 作为 Agent构建一个带声音反馈的交互式 ASCII 艺术绘图工具要求支持拖拽绘图、字符摆放触发对应音符、多套字符/音阶主题、图案切换器、清空按钮并兼容鼠标与触摸输入Build an interactive ASCII art drawing tool with sound feedback. The application should: 1. Create a canvas where users can draw by clicking and dragging 2. Place different ASCII characters or symbols when the user draws 3. Play corresponding musical notes when each character is placed 4. Include multiple pattern sets with different characters and corresponding note scales 5. Add a pattern switcher button to cycle through different character/sound themes 6. Include a clear button to reset the canvas 7. Support both mouse and touch input for mobile compatibility The application should be creative and fun to use, creating an audio-visual experience where patterns of characters create both visual art and musical patterns. Ensure the musical notes are harmonious when played in sequence.6.5 示例Vibe Checking 网站测试Browser Use Agent 驱动以 Browser Use Agent 为框架对网站进行自动点击探索与缺陷报告Vibe test this website. Click around, try things, report whats broken.6.6 示例跑酷风格粒子系统Qwen Chat Web Dev 驱动以 Qwen Chat 的 WebDev 能力构建一个基于 HTML5 Canvas 的实时粒子系统要求 800-1200 个物理粒子、鼠标吸引力/斥力切换、requestAnimationFrame渲染、FPS 性能监控并交付单 HTML 文件Create an interactive real-time particle system using HTML5 Canvas: Core Features: - Render 800-1200 animated particles with physics-based movement - Mouse cursor exerts attractive/repulsive force on nearby particles - Click to toggle between attraction and repulsion modes - Particles respond with smooth acceleration and velocity calculations Technical Requirements: - Use requestAnimationFrame for optimal performance - Implement force calculation based on distance from cursor - Add visual feedback: particle glow, color variation, and fade effects - Include performance monitoring (FPS counter) Deliverables: - Single HTML file with embedded CSS and JavaScript - Clean, commented code following best practices - Responsive design compatible with modern browsers从上述用例可以归纳出 Qwen3-Coder 作为编码 Agent 的两类典型工作流一句话指令 Agent 自主规划执行如桌面整理、Vibe Checking与结构化需求文档 一步到位的完整交付如游戏、粒子系统。六组演示对应的完整视频与更多素材可在 assets/qwen3-coder-next-demo 目录中查看。七、配套生态评测与微调资源除模型与示例外仓库还沉淀了两类重要资源便于读者进一步深入7.1 评测体系qwencoder-eval 目录按模型能力维度组织了完整的评测实现与结果base基座能力涵盖 ExecRepoBench可执行仓库基准、bigcodebench、cruxeval、evalplusHumanEvalPlus/MbppPlus、fim-bench含 cceval、cclongeval、humaneval_fim、repoeval 等补全专项基准、multiple-eval18 种语言的 MultiPL-Einstruct指令微调能力覆盖 BigCodeBench、CodeArena、McEval、PlotCraft、aider、bird-spider、cruxeval、eval-dev-quality、livecode_bench、multipl_e 等主流基准reasoning推理能力提供 livecode_bench_cot 的评测脚本与结果tool_calling_eval工具调用包含 berkeley-function-call-leaderboardBFCL与 tau-bench 两套 Agent/工具调用评测实现其中 tau-bench 目录还保存了 Qwen3-Coder 在 airline/retail 两个模拟环境上的历史轨迹记录是验证函数调用 多轮工具使用能力的直接参考。这些评测脚本与结果文件为 Qwen3-Coder 的Agentic Coding定位提供了可复现的验证路径。7.2 微调体系finetuning 目录提供了从数据到训练的全链路脚本sft/下包含 train.py、binarize 数据脚本、LoRA 适配器配置adapter_config.json与合并脚本dpo/下包含 train.py 及 zero1/zero2/zero3 三档 DeepSpeed 配置适合有定制需求的开发者在此基础上开展二次训练。八、环境依赖与引用8.1 最小依赖根据仓库根目录 requirements.txt本地运行 Qwen3-Coder 所需的最小依赖为torch、transformers、accelerate、safetensors、vllm。其中transformers负责模型加载与 chat templatevllm用于函数调用场景的高效推理与工具解析参见第二节中的官方说明。8.2 版本与兼容性提醒加载模型时使用新的 tokenizerREADME 明确强调 Qwen3-Coder 更新了特殊 token 及其 id须与 Qwen3 保持一致旧 tokenizer 会导致特殊 token 解析错误instruct 模型仅支持非思考模式无需也不支持enable_thinking参数FIM 能力在所有版本中可用补全时需按 5.2 节的格式构造 prompt 并传入eos_token_ids。8.3 引用若本文档或相关模型对你的工作有帮助可按以下 BibTeX 引用article{Qwen3-Coder-Next, title{Qwen3-Coder-Next Technical Report}, author{Ruisheng Cao and Mouxiang Chen and Jiawei Chen and Zeyu Cui and Yunlong Feng and Binyuan Hui and Yuheng Jing and Kaixin Li and Mingze Li and Junyang Lin and Zeyao Ma and Kashun Shum and Xuwu Wang and Jinxi Wei and Jiaxi Yang and Jiajun Zhang and Lei Zhang and Zongmeng Zhang and Wenting Zhao and Fan Zhou}, journal{arXiv preprint arXiv:2603.00729}, year{2026}, }更详细的性能数据与模型介绍可在 README 中链接的技术博客中查看。结语Qwen3-Coder 系列以高效推理 强 Agent 能力 超长上下文为核心卖点Qwen3-Coder-Next通过混合注意力与 MoE 架构在低成本下支撑 256K 上下文统一的 ChatML 模板与专门设计的函数调用格式使其可以无缝接入 Qwen Code、CLINE、Claude Code 等主流 Agent 平台而 FIM 与仓库级补全能力则覆盖了从单函数到多文件仓库的完整代码生成链路。结合本文给出的可直接运行的代码、仓库内可验证的示例脚本与评测资源读者可以立即开始本地部署、对话测试与 Agent 集成。【免费下载链接】Qwen3-CoderQwen3-Coder is the code version of Qwen3, the large language model series developed by Qwen team.项目地址: https://gitcode.com/GitHub_Trending/co/Qwen3-Coder创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考