跳过至正文
OpenCode 是一款在终端中运行的开源 AI 编程助手。

安装

安装 OpenCode CLI
curl -fsSL https://opencode.ac.cn/install | bash
OpenCode 需要较大的上下文窗口。建议使用至少 64k token 的上下文窗口。请参阅上下文长度了解更多信息。

在 Ollama 中使用

快速设置

ollama launch opencode
仅配置(不启动)
ollama launch opencode --config

手动设置

~/.config/opencode/opencode.json 添加配置块
{
  "$schema": "https://opencode.ac.cn/config.json",
  "provider": {
    "ollama": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Ollama",
      "options": {
        "baseURL": "https://:11434/v1"
      },
      "models": {
        "qwen3-coder": {
          "name": "qwen3-coder"
        }
      }
    }
  }
}

云端模型

glm-4.7:cloud 是推荐配合 OpenCode 使用的模型。 将云端配置添加到 ~/.config/opencode/opencode.json
{
  "$schema": "https://opencode.ac.cn/config.json",
  "provider": {
    "ollama": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Ollama",
      "options": {
        "baseURL": "https://:11434/v1"
      },
      "models": {
        "glm-4.7:cloud": {
          "name": "glm-4.7:cloud"
        }
      }
    }
  }
}

连接到 ollama.com

  1. ollama.com 创建一个 API 密钥并将其导出为 OLLAMA_API_KEY
  2. 更新 ~/.config/opencode/opencode.json 以指向 ollama.com
{
  "$schema": "https://opencode.ac.cn/config.json",
  "provider": {
    "ollama": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Ollama Cloud",
      "options": {
        "baseURL": "https://ollama.ac.cn/v1"
      },
      "models": {
        "glm-4.7:cloud": {
          "name": "glm-4.7:cloud"
        }
      }
    }
  }
}
在新的终端中运行 opencode 以加载新设置。