注释title生成
This commit is contained in:
59
server2.js
59
server2.js
@@ -1031,49 +1031,56 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
|
|||||||
// res.write(
|
// res.write(
|
||||||
// `data: ${JSON.stringify({ type: "llm", content: delta})}\n\n`
|
// `data: ${JSON.stringify({ type: "llm", content: delta})}\n\n`
|
||||||
// );
|
// );
|
||||||
if (delta) {
|
|
||||||
for (const ch of delta) {
|
if (delta)
|
||||||
res.write(`data: ${JSON.stringify({ type: "llm", content: ch })}\n\n`);
|
{
|
||||||
await new Promise(r => setTimeout(r, 0)); // 控制渲染节奏
|
for (const ch of delta)
|
||||||
}
|
{
|
||||||
|
res.write(`data: ${JSON.stringify({ type: "llm", content: ch })}\n\n`);
|
||||||
|
await new Promise(r => setTimeout(r, 0)); // 控制渲染节奏
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 保存 LLM 输出到数据库
|
// 保存 LLM 输出到数据库
|
||||||
|
/* 加标题需要异步,且在会话开始时就进行 */
|
||||||
|
// try
|
||||||
|
// {
|
||||||
|
// const titlePrompt = `请根据以下对话内容生成一个简短的标题(不超过15字):
|
||||||
|
// 用户问题: ${userPrompt}
|
||||||
|
// AI回答: ${llmText}`;
|
||||||
|
|
||||||
// try {
|
// const titleResp = await client1.chat.completions.create({
|
||||||
// const titlePrompt = `请根据以下对话内容生成一个简短的标题(不超过15字):
|
// model: "gpt-5-mini",
|
||||||
// 用户问题: ${userPrompt}
|
// messages: [
|
||||||
// AI回答: ${llmText}`;
|
// { role: "system", content: "你是标题生成助手,只返回标题,不要解释。" },
|
||||||
|
// { role: "user", content: titlePrompt }
|
||||||
// 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() || "未命名会话";
|
// const title = titleResp.choices[0]?.message?.content?.trim() || "未命名会话";
|
||||||
// res.write(
|
// res.write(
|
||||||
// `data: ${JSON.stringify({ type: "title", content: title })}\n\n`
|
// `data: ${JSON.stringify({ type: "title", content: title })}\n\n`
|
||||||
// );
|
// );
|
||||||
|
|
||||||
// console.log("生成的标题:", title);
|
// 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]
|
|
||||||
// );
|
|
||||||
|
|
||||||
|
|
||||||
|
// // 保存到数据库(建议你给 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) {
|
// catch (err)
|
||||||
|
// {
|
||||||
// console.error("生成标题失败:", err.message);
|
// console.error("生成标题失败:", err.message);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
await pool.query(
|
||||||
|
"INSERT INTO ai_chat_messages (session_id, user_id, role, content) VALUES (?, ?, 'assistant', ?)",
|
||||||
|
[sessId, userId || null, llmText]
|
||||||
|
);
|
||||||
|
|
||||||
res.write("data: [DONE]\n\n");
|
res.write("data: [DONE]\n\n");
|
||||||
res.end();
|
res.end();
|
||||||
|
|||||||
Reference in New Issue
Block a user