From fc6bbb4bb1013ebf7d4bfa66fb191017de74b00b Mon Sep 17 00:00:00 2001 From: xiaobao <854914462@qq.com> Date: Wed, 17 Sep 2025 13:15:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E5=8E=86=E5=8F=B2=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=9F=BA=E4=BA=8Eui=5Fid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server2.js | 96 +++++++++++++++++++++++++----------------------------- 1 file changed, 44 insertions(+), 52 deletions(-) diff --git a/server2.js b/server2.js index 675442d..ba0e103 100755 --- a/server2.js +++ b/server2.js @@ -6,10 +6,11 @@ const fs = require("fs"); const path = require("path"); const OpenAI = require("openai"); const bodyParser = require("body-parser"); -const app = express(); -const port = 30008; -const mysql = require("mysql2/promise"); +const app = express(); +const port = 30009; + +const mysql = require("mysql2/promise"); // 初始化数据库连接池 const pool = mysql.createPool({ host: "1.95.137.212", @@ -41,51 +42,11 @@ app.use(express.json({ limit: "200mb" })); // 支持大 JSON app.use(express.urlencoded({ extended: true })); // 支持 form-data 字段 // 初始化混元客户端 -// const client = new OpenAI({ -// apiKey: "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", -// baseURL: "https://openrouter.ai/api/v1", -// timeout: 120000, -// }); - -// const client = new OpenAI({ -// baseURL:"https://openrouter.ai/api/v1", -// apiKey:"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", -// timeout: 120000 -// }); - -// const client = new OpenAI({ -// baseURL:"https://satabot.openai.azure.com/", -// apiKey:"DxvRiQWROzLTapiSHcK0FXKAAXDBlhci1XaqTTpr50sgUsAkmxfUJQQJ99BIACYeBjFXJ3w3AAABACOGZdHp", -// timeout: 120000 -// }); - - -//https://satabot.openai.azure.com/ -// const client = new OpenAI({ -// apiKey: "DxvRiQWROzLTapiSHcK0FXKAAXDBlhci1XaqTTpr50sgUsAkmxfUJQQJ99BIACYeBjFXJ3w3AAABACOGZdHp", // 推荐放环境变量 -// baseURL: "https://satabot.openai.azure.com/openai/deployments/gpt-4.1/chat/completions?api-version=2025-01-01-preview", -// defaultQuery: { "api-version": "2024-02-15-preview" }, -// defaultHeaders: { "api-key": process.env.AZURE_OPENAI_KEY }, -// timeout: 120000 -// }); - -// https://satabot.openai.azure.com/openai/deployments/gpt-4.1/ -// https://satabot.openai.azure.com/openai/deployments/gpt-5-mini/ -// 初始化客户端 const client = new OpenAI({ - apiKey: "DxvRiQWROzLTapiSHcK0FXKAAXDBlhci1XaqTTpr50sgUsAkmxfUJQQJ99BIACYeBjFXJ3w3AAABACOGZdHp", - baseURL: "https://satabot.openai.azure.com/openai/deployments/gpt-4.1", - defaultQuery: { "api-version": "2024-04-01-preview" }, - timeout: 120000 + apiKey: "sk-LVfG90qgdhf9kKQUucqBSLioxamDu7gBeW9boXqKOxIDJt7H", + baseURL: "https://api.hunyuan.cloud.tencent.com/v1", + timeout: 120000, }); -const client1 = new OpenAI({ - apiKey: "DxvRiQWROzLTapiSHcK0FXKAAXDBlhci1XaqTTpr50sgUsAkmxfUJQQJ99BIACYeBjFXJ3w3AAABACOGZdHp", - baseURL: "https://satabot.openai.azure.com/openai/deployments/gpt-5-mini/", - defaultQuery: { "api-version": "2024-04-01-preview" }, - timeout: 120000 -}); - - app.get("/test-db", async (req, res) => { try { @@ -100,6 +61,37 @@ app.get("/test-db", async (req, res) => { } }); +// 查询用户所有会话列表 +app.get("/sessions", async (req, res) => { + try { + const { userId } = req.query; + if (!userId) { + return res.status(400).json({ code: 1, error: "userId 必填" }); + } + + // 查该用户所有的会话,按最后活跃时间倒序 + const [rows] = await pool.query( + ` + SELECT session_id, MAX(created_at) AS last_time + FROM ai_chat_messages + WHERE user_id = ? + GROUP BY session_id + ORDER BY last_time DESC + `, + [userId] + ); + + res.json({ + code: 0, + userId, + sessions: rows.map((r) => r.session_id), // 只返回 session_id 列表 + }); + } catch (err) { + console.error("查询会话列表失败:", err); + res.status(500).json({ code: 1, error: err.message }); + } +}); + // 查询会话历史 app.get("/history", async (req, res) => { try { @@ -146,11 +138,11 @@ app.post("/analyze", upload.single("file"), async (req, res) => { // ------------------ 2️⃣ 图像分析 ------------------ const completion = await client.chat.completions.create({ - model: "gpt-4.1", + model: "hunyuan-vision", stream: true, - // do_scene: true, - // do_behavior: true, - // llm_model: "gpt-5-mini", + do_scene: true, + do_behavior: true, + llm_model: "hunyuan-turbos-latest", messages: [ { role: "system", @@ -193,8 +185,8 @@ app.post("/analyze", upload.single("file"), async (req, res) => { ); // ------------------ 3️⃣ LLM 分析 ------------------ - const llmStream = await client1.chat.completions.create({ - model: "gpt-5-mini", + const llmStream = await client.chat.completions.create({ + model: "hunyuan-turbos-latest", stream: true, messages: [ {