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