Compare commits

...

2 Commits

2 changed files with 69 additions and 15 deletions

View File

@@ -5,7 +5,6 @@ const CONFIG = {
apiUrl: 'https://open.bigmodel.cn/api/paas/v4/chat/completions',
apiKey: '2259e33a1357460abe17919aaf81e73d.K44a8LPQTmFM5PKm',
model: 'glm-4.5-air',
thinkingModel: 'glm-z1-flash', // 智谱思考模型
maxTokens: 2048
};
@@ -418,8 +417,19 @@ async function streamGenerate(userMsgIndex) {
contentEl.innerHTML = '<span class="streaming-cursor">▌</span>';
try {
// 根据开关选择模型
const model = enableThinking ? CONFIG.thinkingModel : CONFIG.model;
// 构建请求体 - 统一使用 glm-4.5-air通过 thinking 参数控制
const requestBody = {
model: CONFIG.model,
messages: currentConversation.messages.slice(0, aiMessageIndex).map(m => ({
role: m.role,
content: m.content
})),
max_tokens: CONFIG.maxTokens,
stream: true,
thinking: {
type: enableThinking ? 'enabled' : 'disabled'
}
};
const response = await fetch(CONFIG.apiUrl, {
method: 'POST',
@@ -427,15 +437,7 @@ async function streamGenerate(userMsgIndex) {
'Content-Type': 'application/json',
'Authorization': `Bearer ${CONFIG.apiKey}`
},
body: JSON.stringify({
model: model,
messages: currentConversation.messages.slice(0, aiMessageIndex).map(m => ({
role: m.role,
content: m.content
})),
max_tokens: CONFIG.maxTokens,
stream: true
})
body: JSON.stringify(requestBody)
});
if (!response.ok) {
@@ -512,6 +514,58 @@ async function streamGenerate(userMsgIndex) {
currentConversation.updatedAt = Date.now();
saveConversations();
renderMessages();
// 自动总结标题第一次对话和每隔5次对话
const totalMessages = currentConversation.messages.length;
if (totalMessages === 1 || totalMessages % 5 === 0) {
await generateConversationTitle();
}
}
}
// 生成对话标题
async function generateConversationTitle() {
if (!currentConversation) return;
// 构建对话摘要
const conversationText = currentConversation.messages.map(m =>
`${m.role === 'user' ? '用户' : 'AI'}: ${m.content.slice(0, 200)}`
).join('\n');
const titlePrompt = `请用不超过10个字总结以下对话的主题只输出标题不要其他内容
${conversationText}`;
try {
const response = await fetch(CONFIG.apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${CONFIG.apiKey}`
},
body: JSON.stringify({
model: CONFIG.model,
messages: [{ role: 'user', content: titlePrompt }],
max_tokens: 50
})
});
if (response.ok) {
const data = await response.json();
const newTitle = data.choices?.[0]?.message?.content?.trim();
if (newTitle && newTitle.length > 0 && newTitle.length <= 15) {
currentConversation.title = newTitle;
currentConversation.updatedAt = Date.now();
saveConversations();
// 更新页面标题显示
const titleEl = document.querySelector('.header h1');
if (titleEl) {
titleEl.textContent = newTitle;
}
}
}
} catch (error) {
console.error('生成标题失败:', error);
}
}

View File

@@ -8,12 +8,12 @@
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
<title>AI助手</title>
<link rel="stylesheet" href="style.css?v=2.2.2">
<link rel="stylesheet" href="style.css?v=2.3.0">
<link rel="manifest" href="manifest.json">
</head>
<body>
<div id="app"></div>
<script src="marked.min.js?v=2.2.2"></script>
<script src="app.js?v=2.2.2"></script>
<script src="marked.min.js?v=2.3.0"></script>
<script src="app.js?v=2.3.0"></script>
</body>
</html>