Skip to content

feat: 支持 GLM 系列模型(智谱 AI)作为推理后端#221

Open
hanzhcn wants to merge 5 commits intoConway-Research:mainfrom
hanzhcn:feat/glm-model-support
Open

feat: 支持 GLM 系列模型(智谱 AI)作为推理后端#221
hanzhcn wants to merge 5 commits intoConway-Research:mainfrom
hanzhcn:feat/glm-model-support

Conversation

@hanzhcn
Copy link
Contributor

@hanzhcn hanzhcn commented Feb 24, 2026

概述

本 PR 添加了对智谱 AI GLM 系列模型(如 GLM-5)作为推理后端的支持,使 Conway Automaton 能够使用国产大模型运行。

背景

智谱 AI 的 GLM 系列是中国领先的国产大语言模型,具有以下优势:

  • 成本效益:相比 OpenAI GPT-4,GLM-5 的 API 成本显著更低
  • 本地化合规:数据不出境,满足中国市场的合规需求
  • 中文优化:对中文语境和任务有更好的理解能力
  • 稳定可用:通过 GLM Coding Plan 提供稳定的企业级 API 服务

技术变更

1. 新增配置项 (src/types.ts)

/** Custom OpenAI API base URL (e.g., for GLM Coding Plan: https://open.bigmodel.cn/api/coding/paas/v4) */
openaiApiBaseUrl?: string;

2. 配置桥接修复 (src/agent/loop.ts) ⭐ 关键修复

问题:顶层配置 inferenceModel 未传递到 modelStrategyConfig,导致用户配置被忽略。

修复:桥接顶层配置字段到 modelStrategyConfig

const modelStrategyConfig: ModelStrategyConfig = {
  ...DEFAULT_MODEL_STRATEGY_CONFIG,
  // Bridge top-level config fields for backward compatibility
  ...(config.inferenceModel ? { inferenceModel: config.inferenceModel } : {}),
  ...(config.maxTokensPerTurn ? { maxTokensPerTurn: config.maxTokensPerTurn } : {}),
  // Nested modelStrategy takes highest priority
  ...(config.modelStrategy ?? {}),
};

3. 模型选择优先级修复 (src/inference/router.ts) ⭐ 关键修复

问题:路由矩阵优先于用户配置,导致 gpt-5.2 覆盖用户配置的 glm-5

修复:用户配置的 inferenceModel 现在具有最高优先级:

selectModel(tier: SurvivalTier, taskType: InferenceTaskType): ModelEntry | null {
  // 1. Try user-configured inferenceModel first (highest priority)
  if (strategy.inferenceModel) {
    const entry = this.registry.get(strategy.inferenceModel);
    if (entry && entry.enabled) {
      const isFree = entry.costPer1kInput === 0 && entry.costPer1kOutput === 0;
      const tierOk = tierRank >= (TIER_ORDER[entry.tierMinimum] ?? 0);
      if (isFree || tierOk) {
        return entry;
      }
    }
  }
  // 2. Try routing-matrix candidates as fallback
  // 3. Fall back to other user-configured models...
}

优先级顺序

  1. 用户配置的 inferenceModel(最高优先级)
  2. 路由矩阵候选
  3. 其他配置模型(lowComputeModel, criticalModel)

4. 推理客户端适配 (src/conway/inference.ts)

模型识别:自动检测 GLM 模型并路由到 OpenAI 兼容后端

// GLM models (智谱): glm-* - 使用 OpenAI 兼容 API
if (keys.openaiApiKey && /^glm/i.test(model)) return "openai";

API 端点适配:GLM Coding Plan 使用 /chat/completions 而非标准 OpenAI 的 /v1/chat/completions

const isGlmApi =
  params.apiUrl.includes("bigmodel.cn") ||
  params.apiUrl.includes("/v4") ||
  params.apiUrl.includes("/paas");
const endpoint = isGlmApi ? "/chat/completions" : "/v1/chat/completions";

5. 上下文温度修复 (src/agent/context.ts)

GLM-5 要求 temperature 在开区间 (0, 1) 内,不能为 0:

temperature: 0.1, // GLM-5 requires temperature > 0

6. 模型注册 (src/inference/types.ts)

添加 GLM-5 到 STATIC_MODEL_BASELINE

{
  modelId: "glm-5",
  provider: "openai", // Uses OpenAI-compatible API
  displayName: "GLM-5 (智谱 Coding Plan)",
  tierMinimum: "normal",
  costPer1kInput: 0, // User pays directly to Zhipu
  costPer1kOutput: 0,
  maxTokens: 16384,
  contextWindow: 131072,
  supportsTools: true,
  supportsVision: false,
  parameterStyle: "max_tokens",
  enabled: true,
},

使用方法

automaton.json 中配置:

{
  "openaiApiKey": "your-glm-api-key",
  "openaiApiBaseUrl": "https://open.bigmodel.cn/api/coding/paas/v4",
  "inferenceModel": "glm-5"
}

重要:只需配置顶层 inferenceModel,无需配置嵌套的 modelStrategy

测试

已在生产环境验证:

  • ✅ GLM-5 推理正常工作
  • ✅ 工具调用(function calling)正常
  • ✅ 上下文管理正常
  • ✅ 心跳任务正常执行
  • ✅ 用户配置的 inferenceModel 优先于路由矩阵

兼容性

  • 完全向后兼容,不影响现有用户
  • 仅在使用 openaiApiBaseUrl 配置时启用新功能
  • 对标准 OpenAI API 行为无影响
  • 配置桥接修复适用于所有模型,不仅限于 GLM

相关链接

添加对智谱 AI GLM 系列模型(如 GLM-5)的支持,使 Conway Automaton
能够使用国产大模型运行。

主要变更:
- types.ts: 新增 openaiApiBaseUrl 配置项
- inference.ts: GLM 模型自动检测和 API 端点适配
  - 自动识别 glm-* 模型并路由到 OpenAI 兼容后端
  - 适配 GLM Coding Plan 的 /chat/completions 端点
- index.ts: 传递 openaiApiBaseUrl 到推理客户端
- context.ts: 修复 temperature=0 问题(GLM-5 要求 >0)

使用方法:
在 automaton.json 中配置:
{
  "openaiApiKey": "your-glm-api-key",
  "openaiApiBaseUrl": "https://open.bigmodel.cn/api/coding/paas/v4",
  "inferenceModel": "glm-5"
}
问题:PR Conway-Research#221 缺少模型注册表修改,导致 GLM-5 不在 STATIC_MODEL_BASELINE 中
解决:添加 GLM-5 到模型基线和 DEFAULT_ROUTING_MATRIX
合并官方 main 分支最新更新:
- Ollama 本地模型支持
- Orchestrator 弹性修复 (PR Conway-Research#227)
- SQL 通配符转义修复
- 多项 bug 修复

GLM-5 配置优化:
- maxTokens: 4096 → 32768(最大化 token 限制)
- costPer1kInput/Output: 0(GLM Coding Plan 已付费)
- 添加详细注释说明
hanzhcn pushed a commit to hanzhcn/automaton that referenced this pull request Feb 25, 2026
- costPer1kInput: 1 → 0
- costPer1kOutput: 1 → 0
- gpt-5.2 enabled: false → true(保留官方模型后备)
- 与 PR Conway-Research#221 保持一致
问题:
- 用户配置 inferenceModel: "glm-5" 被路由矩阵覆盖
- 顶层配置字段未桥接到 modelStrategyConfig

修复:
1. loop.ts: 桥接顶层 inferenceModel/maxTokensPerTurn 到 modelStrategyConfig
2. router.ts: 用户配置的 inferenceModel 优先于路由矩阵
3. types.ts: 恢复官方路由矩阵(不硬编码 glm-5)

现在的优先级:
- 用户配置的 inferenceModel > 路由矩阵 > 其他配置模型

配置示例:
{
  "openaiApiKey": "your-glm-key",
  "openaiApiBaseUrl": "https://open.bigmodel.cn/api/coding/paas/v4",
  "inferenceModel": "glm-5"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants