如何配置 OpenClaw 使用自定义模型

前言

网上撸了很多免费的大模型,比如元宝、阿里云、飞书、天翼云等等,由于都是限量供应,比如元宝的和阿里云的 Api 每天会提供一定的额度,所以不得不在多个模型中来回切换。
有些厂商的定版制openclaw 除了飞书外,其他的比如阿里云的 JVS Claw在模型配置面板中只支持部分知名模型供应商,无法随意配置自定义模型,而元宝压根就不支持第三方模型的配置,所以如果我们想要自定义,那就只能通过指令配置的形式了。
指令的执行需要依靠终端窗口,但是没关系,只能你能和龙虾对上话,哪怕厂商不暴露终端给你也一样能配置,让龙虾帮你配就好了, 这还不好简单😉

那么接下来,就跟大家分享一下如何配置自定义模型

第一步:增加自定义模型配置

首先,您需要在 OpenClaw 的配置中注册您的模型提供商和模型信息。

1. 清理旧配置(如存在)

如果之前配置过同名模型,建议先清理旧配置以确保环境干净。

1
openclaw config unset models.providers.<your_provider_name>

提示:请将上述命令中的 <your_provider_name> 替换为您的自定义标识,例如 yuanbao

2. 激活模型配置模式

确保 OpenClaw 的模型模式设置为 merge,以允许合并新的模型配置。

1
openclaw config set models.mode merge

3. 初始化模型提供商配置结构

确保 models.providers 配置项存在且为一个空对象,为后续注入配置做好准备。

1
openclaw config set models.providers '{}'

4. 注入自定义模型配置

使用 --json 参数,以正确的 JSON 格式注入您的模型详细信息。这是最关键的一步。

1
2
3
4
5
6
7
8
9
10
11
openclaw config set 'models.providers.<your_provider_name>' --json '{
"baseUrl": "这里填你模型的 baseUrl",
"apiKey": "这里填你模型的apiKey",
"api": "openai-completions",
"models": [
{
"id": "这里填模型id",
"name": "这里填模型名称"
}
]
}'

重要参数说明:

  • baseUrl:模型服务实际的 API 端点地址。
  • apiKey: 模型 API 密钥。
  • api:指定适配的 API 协议,openai-completions 表示兼容 OpenAI 的补全接口。
  • models.id:模型在服务端的唯一标识符。
  • models.name:为您模型设置的展示名称。

执行效果:成功执行后,您的配置文件openclaw.json 中会新增类似以下的结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"models": {
"providers": {
"<your_provider_name>": {
"baseUrl": "<your_model_api_base_url>",
"apiKey": "<your_api_key>",
"api": "openai-completions",
"models": [
{
"id": "<your_model_id>",
"name": "<your_model_display_name>"
}
]
}
}
}
}

第二步:设置默认模型

注册模型后,需要将其指定为 OpenClaw 智能体(Agents)使用的默认主模型。

在容器内执行以下命令,将您的模型设置为全局默认模型:

1
openclaw config set agents.defaults.model.primary <your_provider_name>/<your_model_id>

关键点解析:

  • 配置路径agents.defaults.model.primary 是 OpenClaw 框架中用于设置主模型的标准配置路径。
  • 格式规范:值必须采用 provider/modelId 的格式。

执行效果:此命令将在配置文件中新增如下结构,使系统默认调用您指定的模型:

1
2
3
4
5
6
7
8
9
{
"agents": {
"defaults": {
"model": {
"primary": "<your_provider_name>/<your_model_id>"
}
}
}
}

第三步:重启网关并验证

配置修改完成后,需要重启相关服务以使新配置生效。

1. 重启 OpenClaw 网关

执行以下命令重启网关服务,加载最新的模型配置。

1
openclaw gateway restart

2. 验证配置状态

重启完成后,使用以下命令查看当前系统中已就绪且生效的默认模型,确认配置是否成功。

1
openclaw models status

当命令输出中显示您配置的模型(如 <your_provider_name>/<your_model_id>)为可用状态时,即表示自定义模型配置并设置默认成功。

总结

总共分为 注册模型、设为默认、重启生效 三个步骤,简单即可完成 OpenClaw 对自定义模型的集成,赶紧去尝试一下吧😁

分部配置方案

有时候我们可能只需要修改某个模型的密钥或者名称,那么可以使用单独的指令会更加方便

具体模板可以参考如下:

第一步:清理配置

1
openclaw config unset models.模型别名

第二步:设置运行模式

1
openclaw config set models.mode "merge"

第三步:配置基础信息

1
2
3
4
5
6
7
8
# 设置 API 端点地址
openclaw config set models.providers.模型别名.baseUrl "API基础URL"

# 设置 API 密钥
openclaw config set models.providers.模型别名.apiKey "API密钥"

# 设置 API 类型
openclaw config set models.providers.模型别名.api "API类型"

第四步:配置模型列表

1
2
3
4
5
6
7
8
9
10
# 1. 先清空 models 配置
openclaw config unset models.providers.模型别名.models

# 2. 添加第一个模型
openclaw config set models.providers.模型别名.models[0].id "第一个模型ID"
openclaw config set models.providers.模型别名.models[0].name "第一个模型显示名称"

# 3. 如需添加更多模型(可选)
openclaw config set models.providers.模型别名.models[1].id "第二个模型ID"
openclaw config set models.providers.模型别名.models[1].name "第二个模型显示名称"

元宝配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 1. 清理配置
openclaw config unset models.yuanbao

# 2. 设置运行模式
openclaw config set models.mode "merge"

# 3. 配置基础信息
openclaw config set models.providers.yuanbao.baseUrl "https://bot.yuanbao.tencent.com/api/bot"
openclaw config set models.providers.yuanbao.apiKey "675628accfc4963a4a9f9c42fe98014cd29d6522717f5a79976165630c55d1bd2a8cfe4bcf821451c0455ed302eeb8d97d5fb52e8c0dd3ba07519b08d90c17845c710b4ffda5fbe7e4383d51fb64"
openclaw config set models.providers.yuanbao.api "openai-completions"

# 4. 配置模型
openclaw config unset models.providers.yuanbao.models
openclaw config set models.providers.yuanbao.models[0].id "openclaw_yuanbao_robot_model"
openclaw config set models.providers.yuanbao.models[0].name "腾讯元宝"

多模型配置示例

1
2
3
4
5
6
7
8
9
10
# 清空后重新设置多个模型
openclaw config unset models.providers.yuanbao.models

# 添加第一个模型
openclaw config set models.providers.yuanbao.models[0].id "openclaw_yuanbao_robot_model"
openclaw config set models.providers.yuanbao.models[0].name "腾讯元宝"

# 添加第二个模型
openclaw config set models.providers.yuanbao.models[1].id "yuanbao-pro"
openclaw config set models.providers.yuanbao.models[1].name "元宝Pro版"

本文为作者原创 转载时请注明出处 谢谢

乱码三千 – 点滴积累 ,欢迎来到乱码三千技术博客站

0%