OpenCode 接入千问(Qwen)大模型完整指南
本文档详细介绍如何在 OpenCode 中配置和使用阿里云百炼(DashScope)提供的千问大模型。
目录
前置条件
- 已安装 OpenCode
- 拥有阿里云账号
- 已开通阿里云百炼(DashScope)服务
获取 API Key
步骤 1:登录阿里云百炼控制台
访问 阿里云百炼控制台
步骤 2:创建 API Key
- 在控制台左侧菜单找到 API Key 管理
- 点击 创建 API Key
- 填写名称(如:
opencode-qwen) - 复制生成的 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"
解决:
- 检查 API Key 是否正确复制(包含
sk-前缀) - 确认已开通百炼服务
- 检查 API Key 是否过期或被删除
Q2: 连接超时或失败
解决:
- 国内用户:使用
https://dashscope.aliyuncs.com/compatible-mode/v1 - 海外用户:使用
https://dashscope-intl.aliyuncs.com/compatible-mode/v1
Q3: 模型列表中没有千问模型
解决:
- 确认配置文件路径正确
- 检查 JSON 格式是否正确(可用在线 JSON 校验工具)
- 重启 OpenCode
Q4: 如何查看当前使用的模型?
解决 :
在 OpenCode 中输入:
/models
当前启用的模型会有标记。
Q5: 成本如何计算?
千问模型的计费基于 token 数量:
- Input: 输入的提示词 token 数
- Output: 模型生成的 token 数
- Cache Read/Write: 缓存读取和写入的 token 数
具体价格请参考 阿里云百炼定价页面
最佳实践
-
根据场景选择模型:
- 复杂任务 → qwen-max
- 日常开发 → qwen-plus
- 简单查询 → qwen-turbo
-
使用环境变量存储 API Key:
- 避免将 API Key 提交到 Git
- 便于在不同环境切换
-
定期更新模型:
- 使用
-latest后缀自动获取最新版本 - 或关注阿里云更新公告
- 使用
-
监控用量:
- 在阿里云控制台查看 API 调用统计
- 设置预算告警避免超额
参考链接
总结
配置流程:
- ✅ 获取阿里云百炼 API Key
- ✅ 创建/编辑
~/.config/opencode/opencode.json - ✅ 配置 provider 和模型参数
- ✅ 启动 OpenCode 并切换模型
- ✅ 开始使用千问大模型!
如有问题,请参考 OpenCode GitHub Issues 或阿里云技术支持。