优化生成标题

This commit is contained in:
2025-11-24 21:40:06 +08:00
parent 7296b7881e
commit a3cb65306a
2 changed files with 76 additions and 42 deletions

View File

@@ -861,8 +861,10 @@ const stopFlags = new Map();
// ====================== API ======================
app.post("/analyze", upload.single("file"), async (req, res) => {
try
{
const { requestId } = req.body;
{
const { requestId } = req.body;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.setHeader("Transfer-Encoding", "chunked");
@@ -889,7 +891,23 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
console.log("缺少必要参数");
return res.status(400).json({ error: "缺少必要参数" });
}
const { userPrompt, imageBase64, userId, sessionId } = req.body;
const { userPrompt, imageBase64, userId, sessionId } = req.body;
const [rows] = await pool.query(
`
SELECT EXISTS(
SELECT 1
FROM ai_chat_messages
WHERE session_id = ?
AND user_id = ?
AND title IS NOT NULL
AND title != ''
) AS hasTitle
`,
[sessionId, userId]
);
const existingTitle = rows[0].hasTitle === 1; // true / false
console.log(existingTitle, rows?.[0], 9999, rows)
// 如果前端没有传 sessionId,生成一个
const sessId = sessionId || Date.now().toString();
@@ -1081,43 +1099,51 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
// 保存 LLM 输出到数据库
/* 加标题需要异步,且在会话开始时就进行 */
// try
// {
// const titlePrompt = `请根据以下对话内容生成一个简短的标题(不超过15字):
// 用户问题: ${userPrompt}
// AI回答: ${llmText}`;
// const titleResp = await client1.chat.completions.create({
// model: "gpt-5-mini",
// messages: [
// { role: "system", content: "你是标题生成助手,只返回标题,不要解释。" },
// { role: "user", content: titlePrompt }
// ]
// });
// const title = titleResp.choices[0]?.message?.content?.trim() || "未命名会话";
// res.write(
// `data: ${JSON.stringify({ type: "title", content: title })}\n\n`
// );
// console.log("生成的标题:", title);
// // 保存到数据库(建议你给 ai_chat_messages 表新增一列 title,或者单独建一张会话表)
// // await pool.query(
// // "INSERT INTO ai_chat_messages (session_id, user_id, role, content,title) VALUES (?, ?, 'assistant', ?,?)",
// // [sessId, userId || null, llmText,title]
// // );
// }
// catch (err)
// {
// console.error("生成标题失败:", err.message);
// }
await pool.query(
/* 加标题需要异步,且在会话开始时就进行 */
if (existingTitle) {
console.log("uidh")
await pool.query(
"INSERT INTO ai_chat_messages (session_id, user_id, role, content) VALUES (?, ?, 'assistant', ?)",
[sessId, userId || null, llmText]
);
} else {
console.log("uidh1111")
try
{
const titlePrompt = `请根据以下对话内容生成一个简短的标题(不超过15字):
用户问题: ${userPrompt}
AI回答: ${llmText}`;
const titleResp = await client1.chat.completions.create({
model: "gpt-5-mini",
messages: [
{ role: "system", content: "你是标题生成助手,只返回标题,不要解释。" },
{ role: "user", content: titlePrompt }
]
});
const title = titleResp.choices[0]?.message?.content?.trim() || "未命名会话";
res.write(
`data: ${JSON.stringify({ type: "title", content: title })}\n\n`
);
console.log("生成的标题:", title);
// 保存到数据库(建议你给 ai_chat_messages 表新增一列 title,或者单独建一张会话表)
await pool.query(
"INSERT INTO ai_chat_messages (session_id, user_id, role, content,title) VALUES (?, ?, 'assistant', ?,?)",
[sessId, userId || null, llmText,title]
);
}
catch (err)
{
console.error("生成标题失败:", err.message);
}
}
res.write("data: [DONE]\n\n");
res.end();