From c22ab032ecd644106b38383790a0cc87eb9c2c43 Mon Sep 17 00:00:00 2001 From: hfshfg <38004547@qq.com> Date: Thu, 2 Apr 2026 20:28:45 +0800 Subject: [PATCH] fix: switch to ZhipuAI GLM-4-Plus API --- .github/scripts/generate-daily.js | 27 ++++++++++++++------------- .github/workflows/daily-content.yml | 2 +- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/.github/scripts/generate-daily.js b/.github/scripts/generate-daily.js index e9fd2a8..0693edf 100644 --- a/.github/scripts/generate-daily.js +++ b/.github/scripts/generate-daily.js @@ -1,7 +1,7 @@ #!/usr/bin/env node /** * 每日内容生成脚本 - * 调用 Claude API 生成每日小技巧和每日最佳案例 + * 调用智谱 GLM API 生成每日小技巧和每日最佳案例 * 写入 website/daily/data.json */ @@ -56,23 +56,24 @@ const USER_PROMPT = `请生成今天(${TODAY})的内容,包含两部分: } }`; -function callClaude(apiKey) { +function callGLM(apiKey) { return new Promise((resolve, reject) => { const body = JSON.stringify({ - model: 'claude-sonnet-4-6', + model: 'glm-4-plus', max_tokens: 1024, - system: SYSTEM_PROMPT, - messages: [{ role: 'user', content: USER_PROMPT }] + messages: [ + { role: 'system', content: SYSTEM_PROMPT }, + { role: 'user', content: USER_PROMPT } + ] }); const options = { - hostname: 'api.anthropic.com', - path: '/v1/messages', + hostname: 'open.bigmodel.cn', + path: '/api/paas/v4/chat/completions', method: 'POST', headers: { 'Content-Type': 'application/json', - 'x-api-key': apiKey, - 'anthropic-version': '2023-06-01', + 'Authorization': `Bearer ${apiKey}`, 'Content-Length': Buffer.byteLength(body) } }; @@ -87,7 +88,7 @@ function callClaude(apiKey) { } try { const parsed = JSON.parse(data); - const text = parsed.content[0].text.trim(); + const text = parsed.choices[0].message.content.trim(); // 提取 JSON(防止模型输出额外文字) const jsonMatch = text.match(/\{[\s\S]*\}/); if (!jsonMatch) throw new Error('No JSON found in response'); @@ -105,9 +106,9 @@ function callClaude(apiKey) { } async function main() { - const apiKey = process.env.ANTHROPIC_API_KEY; + const apiKey = process.env.ZHIPU_API_KEY; if (!apiKey) { - console.error('ANTHROPIC_API_KEY not set'); + console.error('ZHIPU_API_KEY not set'); process.exit(1); } @@ -115,7 +116,7 @@ async function main() { let content; try { - content = await callClaude(apiKey); + content = await callGLM(apiKey); console.log('API call successful'); } catch (err) { console.error('API call failed:', err.message); diff --git a/.github/workflows/daily-content.yml b/.github/workflows/daily-content.yml index 432c4c4..80a4c1a 100644 --- a/.github/workflows/daily-content.yml +++ b/.github/workflows/daily-content.yml @@ -22,7 +22,7 @@ jobs: - name: Generate daily content env: - ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} + ZHIPU_API_KEY: ${{ secrets.ZHIPU_API_KEY }} run: node .github/scripts/generate-daily.js - name: Commit and push changes