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 或阿里云技术支持。

相关推荐
feasibility.1 天前
AI 编程助手进阶指南:从 Claude Code 到 OpenCode 的工程化经验总结
人工智能·经验分享·设计模式·自动化·agi·skills·opencode
molaifeng3 天前
统一调度:用 EasyCLI + CLIProxyAPI 打造你的私人“AI 路由器”
人工智能·codex·opencode·easycli·cliproxyapi
feasibility.4 天前
在OpenCode使用skills搭建基于LLM的dify工作流
人工智能·低代码·docker·ollama·skills·opencode·智能体/工作流
横木沉5 天前
Opencode启动时内置Bun段错误的解决笔记
人工智能·笔记·bun·vibecoding·opencode
猿小猴子7 天前
主流 AI IDE 之一的 OpenCode 介绍
ide·人工智能·ai·opencode
Stuomasi_xiaoxin8 天前
Windows 安装 OpenCode 完全指南:从 npm 踩坑到 Chocolatey 完美部署
python·ai编程·opencode
yuanlulu8 天前
Agent_Skills_完全教程「AI生成」
人工智能·大语言模型·agent·智能体·skill·claude code·opencode
若梦几何2628 天前
opencode升级
opencode
liangshanbo12159 天前
oh-my-opencode (oMo) 全程配置与实战手册
opencode·oh-my-opencode