HuanCode Docs

OpenClaw 配置

在 OpenClaw 中配置 HuanCode API 接入

OpenClaw 是一个开源的 AI Agent 框架,支持通过 OpenAI 兼容接口调用各种大语言模型。

前提条件

安装 OpenClaw

Python
pip install openclaw
Node.js
npm install openclaw

配置方式

方式一:环境变量

export OPENCLAW_API_KEY="your-huancode-api-key"
export OPENCLAW_BASE_URL="https://api.huancode.com/v1"

写入 shell 配置文件使其永久生效:

~/.zshrc 或 ~/.bashrc
# HuanCode API 配置(OpenClaw)
export OPENCLAW_API_KEY="your-huancode-api-key"
export OPENCLAW_BASE_URL="https://api.huancode.com/v1"

方式二:配置文件

创建或编辑 OpenClaw 配置文件:

~/.openclaw/config.yaml
provider:
  api_key: "your-huancode-api-key"
  base_url: "https://api.huancode.com/v1"
  default_model: "deepseek-chat"

方式三:代码中指定

Python

from openclaw import OpenClaw

client = OpenClaw(
    api_key="your-huancode-api-key",
    base_url="https://api.huancode.com/v1"
)

response = client.run(
    model="deepseek-chat",
    prompt="帮我写一个 Python 排序函数"
)

print(response.output)

Node.js

import { OpenClaw } from 'openclaw';

const client = new OpenClaw({
  apiKey: 'your-huancode-api-key',
  baseURL: 'https://api.huancode.com/v1',
});

const response = await client.run({
  model: 'deepseek-chat',
  prompt: '帮我写一个 TypeScript 排序函数',
});

console.log(response.output);

使用不同模型

OpenClaw 支持在运行时切换 HuanCode 提供的任意模型:

# DeepSeek R1 推理模型 —— 适合复杂逻辑和数学推理
response = client.run(
    model="deepseek-reasoner",
    prompt="证明根号 2 是无理数"
)

# 通义千问 Max —— 综合能力强
response = client.run(
    model="qwen-max",
    prompt="分析这段代码的时间复杂度"
)

# Kimi 长文本 —— 适合处理超长文档
response = client.run(
    model="moonshot-v1-128k",
    prompt="总结以下文档的要点:...",
)

Agent 工作流示例

OpenClaw 支持构建多步骤 Agent 工作流,配合 HuanCode 使用:

from openclaw import OpenClaw, Tool

client = OpenClaw(
    api_key="your-huancode-api-key",
    base_url="https://api.huancode.com/v1"
)

# 定义工具
search_tool = Tool(
    name="web_search",
    description="搜索互联网信息",
    handler=my_search_function
)

# 创建带工具的 Agent
response = client.run(
    model="qwen-max",
    prompt="帮我查找 Python 3.12 的新特性并整理成表格",
    tools=[search_tool],
    max_steps=5
)

print(response.output)

验证配置

# 通过 OpenClaw CLI 测试连通性
openclaw test --base-url https://api.huancode.com/v1 --api-key your-huancode-api-key

或者直接调用 API 验证:

curl https://api.huancode.com/v1/models \
  -H "Authorization: Bearer your-huancode-api-key"

常见问题

提示 "Connection refused"

  • 确认 base_urlhttps://api.huancode.com/v1,注意末尾不要有多余的 /
  • 检查网络连接是否正常

Agent 运行中断

  • 检查余额是否充足,多步骤 Agent 会消耗较多 token
  • 可设置 max_steps 限制最大步数,避免超出预算
  • 推理型模型(如 deepseek-reasoner)token 消耗较高,注意用量

如何选择模型?

场景推荐模型说明
日常编程deepseek-chat性价比高,综合能力强
复杂推理deepseek-reasoner推理增强,适合数学和逻辑
长文档处理moonshot-v1-128k128K 上下文,适合长文本
综合任务qwen-max千问旗舰版,能力全面
快速响应qwen-turbo低延迟,适合高频调用

On this page