This commit is contained in:
2026-01-25 14:22:37 +08:00
parent 60b1bcf807
commit ab7c8c9276

View File

@@ -1369,276 +1369,455 @@ app.post("/refreshAiChatNum", async (req, res) => {
// } // }
// }); // });
// const app = express();
// const upload = require('multer')({ dest: 'uploads/' });
// const fs = require('fs');
// 替换为你的工具模块
// const { pool, client, client1, hasQuota, getQuota, setQuota, stopFlags } = require('./your-utils');
app.post("/analyze", upload.single("file"), async (req, res) => { app.post("/analyze", upload.single("file"), async (req, res) => {
const { userId } = req.body; const { userId } = req.body;
try { try {
const exists = await hasQuota(userId); const exists = await hasQuota(userId);
if (!exists) { if (!exists) {
const [userRemainChatNum] = await pool.query( const [userRemainChatNum] = await pool.query(
"SELECT ai_chat_num from sys_user where user_id= ?", "SELECT ai_chat_num from sys_user where user_id= ?",
[userId] [userId]
); );
if (userRemainChatNum.length > 0) { if (userRemainChatNum.length > 0) {
const quota = userRemainChatNum[0].ai_chat_num; const quota = userRemainChatNum[0].ai_chat_num;
if (quota <= 0 && quota !== -1) { if (quota <= 0 && quota !== -1) {
return res.status(400).json({ error: "对话次数已用完,请完成账号注册" }); return res.status(400).json({ error: "对话次数已用完,请完成账号注册" });
} }
await setQuota(userId, quota); await setQuota(userId, quota);
} }
} else { } else {
const quota = await getQuota(userId); const quota = await getQuota(userId);
if (quota <= 0 && quota !== -1) { if (quota <= 0 && quota !== -1) {
return res.status(400).json({ error: "对话次数已用完,请完成账号注册" }); return res.status(400).json({ error: "对话次数已用完,请完成账号注册" });
} }
} }
const { requestId } = req.body; const { requestId } = req.body;
// 统一 SSE 响应头 res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.writeHead(200, { res.setHeader("Transfer-Encoding", "chunked");
"Content-Type": "text/event-stream; charset=utf-8", res.setHeader("Cache-Control", "no-cache");
"Cache-Control": "no-cache", res.setHeader("Connection", "keep-alive");
"Connection": "keep-alive", let aborted = false;
"Transfer-Encoding": "chunked" req.on("close", () => {
}); console.log("🔴 前端断开连接,停止整个分析流程");
res.socket.setNoDelay(true); // 禁用缓冲,实时推送 aborted = true;
});
let aborted = false;
req.on("close", () => {
console.log("🔴 前端断开连接,停止整个分析流程");
aborted = true;
});
console.time("analyze"); console.time("analyze");
const logBody = { ...req.body }; const logBody = { ...req.body };
if (req.body?.imageBase64) delete logBody.imageBase64; if (req.body?.imageBase64) {
console.log("analyze: ", logBody); delete logBody.imageBase64;
}
console.log("analyze: ", logBody);
if (!req.body?.userId || !req.body?.sessionId) { // console.log("analyze: ",req.body)
console.log("缺少必要参数"); if (!req.body?.userId || !req.body?.sessionId) {
return res.write(`data: ${JSON.stringify({ type: "error", content: "缺少必要参数" })}\n\ndata: [DONE]\n\n`), res.end(); console.log("缺少必要参数");
} return res.status(400).json({ error: "缺少必要参数" });
const { userPrompt, imageBase64, sessionId } = req.body; }
const [rows] = await pool.query( const { userPrompt, imageBase64, sessionId } = req.body;
`SELECT EXISTS( const [rows] = await pool.query(
SELECT 1 FROM ai_chat_messages `
WHERE session_id = ? AND user_id = ? AND title IS NOT NULL AND title != '' SELECT EXISTS(
) AS hasTitle`, SELECT 1
[sessionId, userId] FROM ai_chat_messages
); WHERE session_id = ?
const existingTitle = rows[0].hasTitle === 1; AND user_id = ?
console.log(existingTitle, rows?.[0], 9999, rows); 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)
const sessId = sessionId || Date.now().toString(); // 如果前端没有传 sessionId,生成一个
const sessId = sessionId || Date.now().toString();
// ------------------ 获取历史对话 ------------------ // ------------------ 获取历史对话(上下文) ------------------
const [historyRows] = await pool.query( const [historyRows] = await pool.query(
"SELECT role, content, image_base64 FROM ai_chat_messages WHERE session_id = ? AND user_id = ? ORDER BY created_at ASC", "SELECT role, content, image_base64 FROM ai_chat_messages WHERE session_id = ? AND user_id = ? ORDER BY created_at ASC",
[sessId, userId || null] [sessId, userId || null]
); );
const historyMessages = historyRows.map(row => ({
role: row.role,
content: [
row.content ? { type: "text", text: row.content } : null,
row.image_base64 ? {
type: "image_url",
image_url: { url: row.image_base64.startsWith("data:") ? row.image_base64 : `data:image/png;base64,${row.image_base64}` }
} : null
].filter(Boolean)
}));
// ------------------ 保存用户提问 ------------------ // 构建历史消息数组
await pool.query( const historyMessages = historyRows.map(row => {
"INSERT INTO ai_chat_messages (session_id, user_id, role, content, image_base64) VALUES (?, ?, 'user', ?, ?)", const message = {
[sessId, userId || null, userPrompt || "", imageBase64 || null] role: row.role,
); content: []
};
// ------------------ 1. 图像分析(GPT 直接返回思考过程 + 结果) ------------------ // 添加文本内容
console.log("====== 开始图像分析... ======"); if (row.content) {
const imageAnalysisMessages = [ message.content.push({ type: "text", text: row.content });
{ }
role: "system",
content: `你是专注于「新能源运维+智能设备」领域的图像分析专家,同时具备行业信息解答能力,严格按以下规则输出,流式返回且格式不可错乱:
1. 思考过程:第一人称推导不需要明确显示是第一人称,先判断用户需求类型(是图像分析还是信息咨询/设备相关问题),再拆解核心诉求,结合行业经验梳理推理逻辑——若为信息咨询,需关联场景(如光伏运维)补充背景;若为图像问题,需按「元素识别→状态判断→需求匹配」推导,全程贴合用户提问重点,不冗余无关信息。
2. 分隔符:必须用「---分析结果---」清晰分割思考过程与最终内容,分隔符单独成行,前后无多余文字。
3. 最终分析结果:结构简洁,先明确核心结论,再补充关键细节(如企业业务、产品特性、场景适配性),结尾预留延伸空间(引导用户补充信息以获取更精准内容),语言专业且落地,符合光伏运维等实际场景需求。
要求:思考过程体现「需求判断→逻辑拆解→信息整合」的完整链路,必须按照思考过程-分析结果-最终分析结果的格式排版。最终结果聚焦用户核心诉求,不偏离主题,适配后续LLM深度分析的衔接。` // 添加图片内容
}, if (row.image_base64) {
...historyMessages, message.content.push({
{ type: "image_url",
role: "user", image_url: {
content: [ url: row.image_base64.startsWith("data:")
{ type: "text", text: userPrompt || "请描述图片内容" }, ? row.image_base64
imageBase64 ? { : `data:image/png;base64,${row.image_base64}`,
type: "image_url", }
image_url: { url: imageBase64.startsWith("data:") ? imageBase64 : `data:image/png;base64,${imageBase64}` } });
} : null }
].filter(Boolean)
}
];
const sceneCompletion = await client.chat.completions.create({ return message;
model: "gpt-4.1", });
stream: true,
messages: imageAnalysisMessages,
// temperature: 0.3 // 降低随机性,保证格式稳定
});
// 流式解析:拆分思考过程和分析结果
let sceneState = "thinking"; // thinking: 思考过程阶段, result: 分析结果阶段
let sceneThinkingText = "", sceneText = "";
const SCENE_SPLITTER = "---分析结果---";
for await (const chunk of sceneCompletion) { // ------------------ 保存用户提问 ------------------
if (stopFlags.get(requestId) || aborted) { await pool.query(
console.log("⛔ 图像分析被中断"); "INSERT INTO ai_chat_messages (session_id, user_id, role, content, image_base64) VALUES (?, ?, 'user', ?, ?)",
return res.end(), stopFlags.delete(requestId); [sessId, userId || null, userPrompt || "", imageBase64 || null]
} );
const delta = chunk.choices[0]?.delta?.content || "";
if (!delta) continue;
// 状态切换:遇到分隔符则切换到结果阶段 // console.log("userPrompt: ",userPrompt);
if (sceneState === "thinking" && delta.includes(SCENE_SPLITTER)) { // console.log("imageBase64: ",imageBase64);
const [thinkingPart, resultPart] = delta.split(SCENE_SPLITTER); // console.log("userId: ",userId);
sceneThinkingText += thinkingPart; // console.log("sessionId: ",sessionId);
// 推送思考过程
res.write(`data: ${JSON.stringify({ type: "scene_thinking", content: thinkingPart })}\n\n`);
sceneState = "result";
// 推送分隔符后的结果片段
if (resultPart) {
sceneText += resultPart;
res.write(`data: ${JSON.stringify({ type: "scene", content: resultPart })}\n\n`);
}
} else if (sceneState === "thinking") {
// 持续推送思考过程
sceneThinkingText += delta;
res.write(`data: ${JSON.stringify({ type: "scene_thinking", content: delta })}\n\n`);
} else {
// 持续推送分析结果
sceneText += delta;
res.write(`data: ${JSON.stringify({ type: "scene", content: delta })}\n\n`);
}
}
console.log("====== 图像分析思考过程 ======\n", sceneThinkingText);
console.log("====== 图像分析结果 ======\n", sceneText);
console.log("====== 图像分析结束 ======");
// ------------------ 2. LLM 深度分析(GPT 直接返回思考过程 + 结果) ------------------ // ------------------ 图像分析 ------------------
console.log("====== 开始深度思考... ======"); // 构建包含上下文的消息数组
const llmMessages = [ const imageAnalysisMessages = [
{ {
role: "system", role: "system",
content: `你是新能源(光伏/风电)场站运维专家,擅长结合智能设备(如割草机器人)给出可落地的标准化方案,严格按以下规则输出,流式返回且格式不可错乱: content: "你是一名图像分析专家,需要结合历史对话内容分析当前图像。"
1. 思考过程:第一人称推导,先拆解用户核心需求(如设备选型、部署落地、运维优化),再搭建分析框架(按「目标确认→资料收集→现场勘察→试点验证→落地推广」逻辑),明确核心评估维度(安全兼容性、自动化可靠性、运维便利性、经济性),说明方案优先级原则(如「可操作性优先、风险可控」),全程紧扣用户提及的设备品牌(如飒踏智能),推导逻辑与后续方案强关联。 },
2. 分隔符:必须用「---方案结果---」清晰分割思考过程与最终内容,分隔符单独成行,前后无多余文字。 ...historyMessages, // 添加历史对话作为上下文
3. 最终运维方案:按「时间节点明确、步骤可落地、指标可量化」原则,拆分各阶段任务(含时间周期、核心动作、输出物),补充标准化SOP、KPI设定、风险防控要点,兼顾安全性(对光伏设备无损伤)、实用性(适配场站场景)、经济性(成本可控),方案结构清晰,可直接用于场站作业指导或采购决策。 {
role: "user",
content: [
{ type: "text", text: userPrompt || "请描述图片内容,并给出分析思考过程" },
imageBase64
? {
type: "image_url",
image_url: {
url: imageBase64.startsWith("data:")
? imageBase64
: `data:image/png;base64,${imageBase64}`,
},
}
: null,
].filter(Boolean),
},
];
要求:思考过程不跳跃,每个分析维度对应方案中的具体环节;最终方案避免空泛,补充可量化指标、资料清单、勘察要点等细节,同时预留灵活调整空间(适配不同场站规模、设备型号)。` const completion = await client.chat.completions.create({
}, model: "gpt-4.1",
...historyMessages, stream: true,
{ role: "user", content: userPrompt || "请基于图片分析提供建议" }, messages: imageAnalysisMessages
{ role: "assistant", content: sceneText } });
];
const llmStream = await client1.chat.completions.create({ console.log("====== 开始图像分析... ======")
model: "gpt-5-mini", let sceneText = "";
stream: true,
messages: llmMessages,
// temperature: 0.4
});
// 流式解析:拆分LLM思考过程和方案结果 /* TLS 加密缓冲,小块数据可能先缓存在服务器端,不立即发送,会导致图像分析模型的结果不能立即返回 */
let llmState = "thinking"; res.writeHead(200, {
let llmThinkingText = "", llmText = ""; "Content-Type": "text/event-stream",
const LLM_SPLITTER = "---方案结果---"; "Cache-Control": "no-cache",
"Connection": "keep-alive",
});
res.socket.setNoDelay(true);
for await (const chunk of llmStream) { for await (const chunk of completion) {
if (stopFlags.get(requestId) || aborted) { if (stopFlags.get(requestId)) {
console.log("⛔ 深度分析被中断"); console.log("⛔ 图像分析被前端中断");
return res.end(), stopFlags.delete(requestId); res.end();
} stopFlags.delete(requestId); // 用完清理
const delta = chunk.choices[0]?.delta?.content || ""; return;
if (!delta) continue; }
const delta = chunk.choices[0]?.delta?.content || "";
sceneText += delta;
if (llmState === "thinking" && delta.includes(LLM_SPLITTER)) { if (delta) {
const [thinkingPart, resultPart] = delta.split(LLM_SPLITTER); res.write(
llmThinkingText += thinkingPart; `data: ${JSON.stringify({ type: "scene", content: delta })}\n\n`
res.write(`data: ${JSON.stringify({ type: "llm_thinking", content: thinkingPart })}\n\n`); );
llmState = "result"; }
if (resultPart) {
llmText += resultPart;
res.write(`data: ${JSON.stringify({ type: "llm", content: resultPart })}\n\n`);
}
} else if (llmState === "thinking") {
llmThinkingText += delta;
res.write(`data: ${JSON.stringify({ type: "llm_thinking", content: delta })}\n\n`);
} else {
llmText += delta;
res.write(`data: ${JSON.stringify({ type: "llm", content: delta })}\n\n`);
}
}
console.log("====== LLM思考过程 ======\n", llmThinkingText);
console.log("====== LLM方案结果 ======\n", llmText);
console.log("====== 深度思考结束 ======");
// ------------------ 保存结果 & 生成标题 ------------------ // if (delta)
if (existingTitle) { // {
await pool.query( // for (const ch of delta)
"INSERT INTO ai_chat_messages (session_id, user_id, role, content) VALUES (?, ?, 'assistant', ?)", // {
[sessId, userId || null, llmText] // res.write(`data: ${JSON.stringify({ type: "scene", content: ch })}\n\n`);
); // await new Promise(r => setTimeout(r, 0)); // 控制渲染节奏
} else { // }
try { // }
const titleResp = await client1.chat.completions.create({ }
model: "gpt-5-mini", console.log(sceneText);
messages: [ console.log("====== 图像分析结束 ======")
{ role: "system", content: "只返回15字以内的标题,无解释" }, // ------------------ ① 思路阶段(模拟 ChatGPT 思考) ------------------
{ role: "user", content: `用户问题: ${userPrompt}\nAI回答: ${llmText}` } const thinkingMessages = [
] {
}); role: "system",
const title = titleResp.choices[0]?.message?.content?.trim() || "未命名会话"; content: `
res.write(`data: ${JSON.stringify({ type: "title", content: title })}\n\n`); 你是一名新能源项目AI分析师。
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(); - 只输出思路,不给结论
if (req.file?.path) fs.unlinkSync(req.file.path); `
console.log("====== 分析完成 ======"); },
console.timeEnd("analyze"); ...historyMessages,
{
role: "user",
content: userPrompt || "请分析图片。"
},
{
role: "assistant",
content: sceneText
}
];
} catch (err) { const thinkingStream = await client1.chat.completions.create({
console.error("❌ 调用失败:", err.response?.data || err.message); model: "gpt-5-mini",
res.write(`data: ${JSON.stringify({ type: "error", content: err.response?.data || err.message })}\n\n`); stream: true,
res.write("data: [DONE]\n\n"); messages: thinkingMessages
res.end(); });
} finally {
let userChatNum = await getQuota(userId); res.write(
if (userChatNum !== -1 && userChatNum !== undefined) { `data: ${JSON.stringify({ type: "phase", content: "thinking" })}\n\n`
userChatNum = Math.max(0, userChatNum - 1); );
await setQuota(userId, userChatNum);
await pool.query("update sys_user set ai_chat_num = ? where user_id= ?", [userChatNum, userId]); let thinkingText = "";
console.log("更新用户对话次数:", userId, userChatNum);
} console.log("====== 开始思路输出 ======");
} for await (const chunk of thinkingStream) {
if (stopFlags.get(requestId)) {
console.log("⛔ 思路阶段被中断");
res.end();
stopFlags.delete(requestId);
return;
}
const delta = chunk.choices[0]?.delta?.content || "";
thinkingText += delta;
if (delta) {
res.write(
`data: ${JSON.stringify({ type: "thinking", content: delta })}\n\n`
);
}
}
console.log(thinkingText);
console.log("====== 思路输出结束 ======");
// 保存图像分析结果到数据库
// await pool.query(
// "INSERT INTO ai_chat_messages (session_id, user_id, role, content) VALUES (?, ?, 'assistant', ?)",
// [sessId, userId || null, sceneText]
// );
// ------------------ 3️⃣ LLM 分析 ------------------
// console.log("LLM 分析 sceneText: ", sceneText)
// const preText = "详细分析这个光伏场站,根据图片给出详细的运维方案"
// + "如果这个光伏场站需要割草怎么做,给出一份详细的计划"
// + "如果这个光伏场站需要用无人机清洗怎么做,给出一份详细的计划"
// + "如果这个光伏场站需要巡检怎么做,给出一份详细的计划"
// 构建包含完整上下文的LLM消息数组
const llmMessages = [
{
role: "system",
content: "你是一名新能源项目的图像分析AI分析师,需要结合历史对话提供专业建议。"
},
...historyMessages, // 添加历史对话作为上下文
{
role: "user",
content: userPrompt || "请基于图片分析提供建议。"
},
{
role: "assistant",
content: sceneText // 添加当前图像分析结果
}
];
// const llmStream = await client1.chat.completions.create({
// model: "gpt-5-mini",
// stream: true,
// messages: llmMessages
// });
// ------------------ ② 结论阶段(最终分析) ------------------
const answerMessages = [
{
role: "system",
content: `
你是一名新能源项目图像分析AI。
请基于以下信息,输出【最终结论】:
要求:
- 内容完整、专业、可执行
- 不重复思路概览
- 可以分段、分点说明
`
},
...historyMessages,
{
role: "user",
content: userPrompt || "请给出最终分析建议。"
},
{
role: "assistant",
content: sceneText + thinkingText
}
];
const answerStream = await client1.chat.completions.create({
model: "gpt-5-mini",
stream: true,
messages: answerMessages
});
res.write(
`data: ${JSON.stringify({ type: "phase", content: "answer" })}\n\n`
);
let llmText = "";
console.log("====== 开始最终分析 ======");
for await (const chunk of answerStream) {
if (stopFlags.get(requestId)) {
console.log("⛔ 最终分析被中断");
res.end();
stopFlags.delete(requestId);
return;
}
const delta = chunk.choices[0]?.delta?.content || "";
llmText += delta;
if (delta) {
res.write(
`data: ${JSON.stringify({ type: "answer", content: delta })}\n\n`
);
}
}
console.log(llmText);
console.log("====== 最终分析结束 ======");
// console.log("====== 开始深度思考... ======")
// let llmText = "";
// for await (const chunk of llmStream) {
// if (stopFlags.get(requestId)) {
// console.log("⛔ 深度分析被前端中断");
// res.end();
// stopFlags.delete(requestId);
// return;
// }
// const delta = chunk.choices[0]?.delta?.content || "";
// llmText += delta;
// if (delta)
// res.write(
// `data: ${JSON.stringify({ type: "llm", content: delta })}\n\n`
// );
// /* delta 很长,这会 人为拉长处理时间。 */
// // if (delta)
// // {
// // for (const ch of delta)
// // {
// // res.write(`data: ${JSON.stringify({ type: "llm", content: ch })}\n\n`);
// // await new Promise(r => setTimeout(r, 0)); // 控制渲染节奏
// // }
// // }
// }
// 保存 LLM 输出到数据库
/* 加标题需要异步,且在会话开始时就进行 */
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();
// 删除上传文件
if (req.file && req.file.path) {
fs.unlinkSync(req.file.path);
}
console.log("====== 分析完成 ======");
console.timeEnd("analyze");
// // 更新用户剩余对话次数
// let userChatNum = await getQuota(userId);
// if(userChatNum!==-1){
// userChatNum = Math.max(0, userChatNum - 1); // 防止变成负数
// await setQuota(userId,userChatNum);
// console.log(`用户剩余AI 对话次数: ${userChatNum}`);
// }
}
catch (err) {
console.error("❌ 调用失败:", err.response?.data || err.message);
res.status(500).json({ code: 1, error: err.response?.data || err.message });
}
finally {
let userChatNum = await getQuota(userId);
if (userChatNum !== -1 && userChatNum !== undefined) {
userChatNum = Math.max(0, userChatNum - 1); // 防止变成负数
await setQuota(userId, userChatNum);
// 更新数据库
await pool.query(
"update sys_user set ai_chat_num = ? where user_id= ?",
[userChatNum, userId]
);
console.log("更新用户对话次数:", userId, userChatNum);
}
}
}); });
module.exports = app;
app.post("/stop", express.json(), (req, res) => { app.post("/stop", express.json(), (req, res) => {
const { requestId } = req.body; const { requestId } = req.body;
console.log("🔴 收到停止信号:", requestId); console.log("🔴 收到停止信号:", requestId);