开发工具

使用OpenCode Ollama Qwen离线AI编码代理

How to set up and run an offline coding agent using OpenCode, Ollama and Qwen.

ガイド一覧 · 本文は主に簡体字中国語です。国際向けの要点は English summary をご利用ください。

使用OpenCode Ollama Qwen离线AI编码代理

摘要:如何使用OpenCode、Ollama和Qwen搭建完全离线的AI编码代理,支持在网络中断时让Cursor项目继续编码、修复bug和运行测试。本文适合Cursor Pro或Windsurf用户追求隐私编程场景,也可直接对比在线Claude Code订阅和Cursor Pro订阅的价值。

slug:ch-offline-coding-with-open-code-ollama-qwen

分类:开发工具

OpenCode + Ollama + Qwen的离线AI编码代理,让你的Cursor项目在飞行、火车或无网环境下依然能继续工作。它用本地模型处理读文件、编辑代码和运行Shell命令,无需任何在线API,完全隐私。

如果你是远程工程师、注重数据安全,或者经常在网络信号差的地方办公,这套方案特别实用。相比依赖在线模型的Claude Code订阅或Cursor Pro订阅,它在低带宽或断网时也能保持高效率,是值得尝试的替代方案。

核心优势对比

方案 网络要求 隐私安全 每日费用(约) 适合场景
OpenCode Ollama Qwen 无(本地) 最高 0元 飞行/火车/办公室断网
Cursor Pro 需要网络 中等 ~20美元/月 日常主力 + 云模型增强
Claude Code订阅 需要网络 中等 ~20美元/月 复杂任务但依赖云

数据参考(2026-10-27)来自OpenCode官方文档与Qwen模型卡。实际花费以当前硬件为准。

为什么选择Qwen3.5 9B作为本地模型

Qwen3.5系列在LiveCodeBench和BFCL工具调用基准上表现突出,适合需要编辑和运行命令的OpenCode代理。我们选用9B版本,下载约6.6–7.6 GB,运行内存需求12–16 GB起。

Qwen3.5模型对比(基准数据)

模型 LiveCodeBench v6 BFCL-V4 下载体积(GB)
Qwen3.5 4B 55.8 50.3 3.3–4.0
Qwen3.5 9B 65.6 66.1 6.6–7.6
Qwen3.5 27B 高于9B 高于9B 17–20

安装时优先用9B,能平衡速度与能力。更多官方模型列表见Ollama仓库。

安装与配置步骤

1. 在联网状态下,用Homebrew安装工具:brew install ollama node python git。

2. 创建项目目录并克隆示例仓库(含expense_total.py和测试代码)。

3. 安装OpenCode v1.19.4:npm install --prefix .tools --save-exact opencode-ai@1.19.4。

4. 启动Ollama服务(Terminal B窗口):OLLAMA_HOST=127.0.0.1:11434 OLLAMA_NO_CLOUD=1 OLLAMA_CONTEXT_LENGTH=65536 ollama serve。

5. 下载模型:ollama pull qwen3.5:9b。

6. 配置OpenCode(opencode.json已内置本地路径):指定Qwen3.5 9B为主要和辅助模型,启用权限确认,禁用web工具。

OpenCode配置关键点(opencode.json示例):

  • model和small_model指向
  • permission模式要求手动确认编辑与Shell命令
  • webSearch与fetch工具已关闭

离线运行实战:修复Expense Calculator示例

准备好项目后,在网络中断前运行初始程序和测试(3个用例失败)。启动OpenCode(--pure模式)输入提示:“Fix the bug in expense_total.py so the existing tests pass. Don't change the tests or install anything. Run the tests and explain your fix.”

代理会提出一行修改(Decimal处理),请求权限确认并自动运行unittest。5个测试全部通过后退出,再手动验证程序返回25.50。整个过程全程本地,无一次网络请求。

可继续尝试CSV销售报表和日志分析器示例,保持相同配置。

性能与边界

本地Qwen3.5 9B在OpenCode任务上完成率约50%(云端基准参考),速度取决于硬件(Apple M系列建议16 GB+内存)。复杂项目可能需要分步调试。

延伸阅读

风险与边界

本地部署依赖机器硬件与显存,复杂任务可能需多次人工确认。模型能力有限,无法替代专业在线API(如Claude 3.5或Cursor Pro云服务)的深度分析。实际结果以OpenCode官方文档和Qwen模型卡当日数据为准。以下非法律意见:本文仅供学习参考,不构成任何商业或技术支持。使用前请备份项目,测试环境以官方安装为准。

English summary

This guide explains how to set up a fully offline AI coding agent using OpenCode, Ollama, and the Qwen3.5 9B model. Perfect for Cursor Pro or Windsurf users who want privacy-focused coding during network outages or travel. The agent can read files, propose edits, and run tests locally with permission prompts. Key setup involves installing tools, starting Ollama with no-cloud mode, downloading the 9B model, and configuring OpenCode to use the local endpoint. A complete walkthrough fixes a buggy expense calculator, achieving full test passes offline. Benchmarks show solid tool-calling performance, though results vary by hardware. Compare with online options like Cursor Pro subscription or Claude Code—offline wins on cost and privacy but needs sufficient RAM (12-16 GB recommended). Ideal for engineers prioritizing security or remote work. Ready-to-use examples included in the companion repo.

(正文字数约2150字,去除空白后约1980字)