OpenCode接入千问模型指南

OpenCode 接入千问(Qwen)大模型完整指南

本文档详细介绍如何在 OpenCode 中配置和使用阿里云百炼(DashScope)提供的千问大模型。

目录

  1. 前置条件
  2. [获取 API Key](#获取 API Key)
  3. [配置 OpenCode](#配置 OpenCode)
  4. 切换模型
  5. 支持的模型
  6. 常见问题

前置条件

  • 已安装 OpenCode
  • 拥有阿里云账号
  • 已开通阿里云百炼(DashScope)服务

获取 API Key

步骤 1:登录阿里云百炼控制台

访问 阿里云百炼控制台

步骤 2:创建 API Key

  1. 在控制台左侧菜单找到 API Key 管理
  2. 点击 创建 API Key
  3. 填写名称(如:opencode-qwen
  4. 复制生成的 API Key(格式:sk-xxxxx

步骤 3:记录 API Key

将 API Key 保存到安全的地方,例如:

复制代码
sk-e277c7fd3ff54d79a2ffe68363b3c1XY

⚠️ 重要提示:API Key 是访问模型的凭证,请勿泄露给他人。


配置 OpenCode

方式一:API Key 写入配置文件(推荐新手)

步骤 1:创建/编辑配置文件

打开或创建配置文件:

  • Windows : %USERPROFILE%\.config\opencode\opencode.json
  • macOS/Linux : ~/.config/opencode/opencode.json
步骤 2:粘贴配置内容
json 复制代码
{
  "$schema": "https://opencode.ai/config.json",
  "model": "aliyun/qwen-max",
  "provider": {
    "aliyun": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "aliyun",
      "options": {
        "baseURL": "https://dashscope.aliyuncs.com/compatible-mode/v1",
        "apiKey": "sk-e277c7fd3ff54d79a2ffe68363b3c1XY"
      },
      "models": {
        "qwen-max": {
          "name": "qwen-max",
          "cost": {
            "input": 10,
            "output": 40,
            "cache_read": 10,
            "cache_write": 40
          },
          "limit": {
            "context": 262144,
            "output": 65536
          }
        },
        "qwen-plus": {
          "name": "qwen-plus",
          "cost": {
            "input": 2,
            "output": 8,
            "cache_read": 2,
            "cache_write": 8
          },
          "limit": {
            "context": 131072,
            "output": 32768
          }
        },
        "qwen-turbo": {
          "name": "qwen-turbo",
          "cost": {
            "input": 0.5,
            "output": 2,
            "cache_read": 0.5,
            "cache_write": 2
          },
          "limit": {
            "context": 131072,
            "output": 8192
          }
        }
      }
    }
  }
}

⚠️ 请将 apiKey 替换为您自己的 API Key

步骤 3:保存文件

确保文件保存为 opencode.json,格式为 UTF-8。


方式二:使用环境变量(更安全)

步骤 1:设置环境变量

Windows (PowerShell):

powershell 复制代码
[Environment]::SetEnvironmentVariable("DASHSCOPE_API_KEY", "sk-e277c7fd3ff54d79a2ffe68363b3c133", "User")

macOS/Linux (Bash):

bash 复制代码
export DASHSCOPE_API_KEY="sk-e277c7fd3ff54d79a2ffe68363b3c133"
# 添加到 ~/.bashrc 或 ~/.zshrc 使其永久生效
echo 'export DASHSCOPE_API_KEY="sk-e277c7fd3ff54d79a2ffe68363b3c133"' >> ~/.bashrc
步骤 2:修改配置文件

将配置文件中的 apiKey 改为:

json 复制代码
"apiKey": "{env:DASHSCOPE_API_KEY}"

切换模型

方法 1:使用 /models 命令(推荐)

启动 OpenCode:

bash 复制代码
opencode

在 OpenCode 提示符下输入:

复制代码
/models

会显示可用模型列表:

  • aliyun/qwen-max - 千问 Max(最强版本)
  • aliyun/qwen-plus - 千问 Plus(平衡版本)
  • aliyun/qwen-turbo - 千问 Turbo(快速经济版)

使用方向键选择,按回车确认。

方法 2:启动时指定

bash 复制代码
opencode --model aliyun/qwen-max

方法 3:修改默认模型

编辑 opencode.json,修改:

json 复制代码
"model": "aliyun/qwen-plus"

支持的模型

模型名称 上下文长度 输出长度 特点 适用场景
qwen-max 262K 65K 最强性能,支持复杂推理 代码生成、复杂分析
qwen-plus 131K 32K 性能与成本平衡 日常编程、文档编写
qwen-turbo 131K 8K 响应快速,成本低 简单查询、快速响应

其他可用模型

  • qwen-max-latest - 始终使用最新版本
  • qwen-plus-latest - Plus 版本最新版
  • qwen-coder-plus - 专为代码优化的版本

常见问题

Q1: 提示 "Invalid API Key"

解决

  1. 检查 API Key 是否正确复制(包含 sk- 前缀)
  2. 确认已开通百炼服务
  3. 检查 API Key 是否过期或被删除

Q2: 连接超时或失败

解决

  • 国内用户:使用 https://dashscope.aliyuncs.com/compatible-mode/v1
  • 海外用户:使用 https://dashscope-intl.aliyuncs.com/compatible-mode/v1

Q3: 模型列表中没有千问模型

解决

  1. 确认配置文件路径正确
  2. 检查 JSON 格式是否正确(可用在线 JSON 校验工具)
  3. 重启 OpenCode

Q4: 如何查看当前使用的模型?

解决

在 OpenCode 中输入:

复制代码
/models

当前启用的模型会有标记。

Q5: 成本如何计算?

千问模型的计费基于 token 数量:

  • Input: 输入的提示词 token 数
  • Output: 模型生成的 token 数
  • Cache Read/Write: 缓存读取和写入的 token 数

具体价格请参考 阿里云百炼定价页面


最佳实践

  1. 根据场景选择模型

    • 复杂任务 → qwen-max
    • 日常开发 → qwen-plus
    • 简单查询 → qwen-turbo
  2. 使用环境变量存储 API Key

    • 避免将 API Key 提交到 Git
    • 便于在不同环境切换
  3. 定期更新模型

    • 使用 -latest 后缀自动获取最新版本
    • 或关注阿里云更新公告
  4. 监控用量

    • 在阿里云控制台查看 API 调用统计
    • 设置预算告警避免超额

参考链接


总结

配置流程:

  1. ✅ 获取阿里云百炼 API Key
  2. ✅ 创建/编辑 ~/.config/opencode/opencode.json
  3. ✅ 配置 provider 和模型参数
  4. ✅ 启动 OpenCode 并切换模型
  5. ✅ 开始使用千问大模型!

如有问题,请参考 OpenCode GitHub Issues 或阿里云技术支持。

相关推荐
weixin_427179283 天前
openCode使用
agent·opencode
小草cys5 天前
opencode添加skills(windows下)
skills·opencode
鸿乃江边鸟5 天前
用 oh-my-opencode 写一个word转pdf skill
pdf·大模型·opencode
青啊青斯10 天前
OpenCode环境搭建及常用操作
opencode
Hehuyi_In10 天前
opencode + skills 漏洞复盘分析试用
ai·skills·opencode
hallo12811 天前
opencode执行/start-work出现错误:Model not found: opencode/glm-4.7-free.
opencode
夜魔00911 天前
OpenCode、OpenClaw、Claude Code skill tool失败问题解决
ripgrep·fetch·error·skill·rg·opencode
feasibility.12 天前
AI自动化的陷阱:2026年开年ai爆发潮下的冷思考
人工智能·经验分享·自动化·程序员创富·vibe coding·opencode·openclaw
谷新龙00114 天前
opencode入门
opencode
烁烁闪闪烁烁15 天前
【weelinking系列Claude教程】 04 - Claude Code 安装与配置
人工智能·chatgpt·ai编程·claude·cursor·claude code·opencode