加入历史查询基于ui_id

This commit is contained in:
xiaobao
2025-09-17 13:15:15 +08:00
parent 5f9a2bc78c
commit fc6bbb4bb1

View File

@@ -6,10 +6,11 @@ const fs = require("fs");
const path = require("path"); const path = require("path");
const OpenAI = require("openai"); const OpenAI = require("openai");
const bodyParser = require("body-parser"); 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({ const pool = mysql.createPool({
host: "1.95.137.212", host: "1.95.137.212",
@@ -41,51 +42,11 @@ app.use(express.json({ limit: "200mb" })); // 支持大 JSON
app.use(express.urlencoded({ extended: true })); // 支持 form-data 字段 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({ const client = new OpenAI({
apiKey: "DxvRiQWROzLTapiSHcK0FXKAAXDBlhci1XaqTTpr50sgUsAkmxfUJQQJ99BIACYeBjFXJ3w3AAABACOGZdHp", apiKey: "sk-LVfG90qgdhf9kKQUucqBSLioxamDu7gBeW9boXqKOxIDJt7H",
baseURL: "https://satabot.openai.azure.com/openai/deployments/gpt-4.1", baseURL: "https://api.hunyuan.cloud.tencent.com/v1",
defaultQuery: { "api-version": "2024-04-01-preview" }, timeout: 120000,
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) => { app.get("/test-db", async (req, res) => {
try { 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) => { app.get("/history", async (req, res) => {
try { try {
@@ -146,11 +138,11 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
// ------------------ 2️⃣ 图像分析 ------------------ // ------------------ 2️⃣ 图像分析 ------------------
const completion = await client.chat.completions.create({ const completion = await client.chat.completions.create({
model: "gpt-4.1", model: "hunyuan-vision",
stream: true, stream: true,
// do_scene: true, do_scene: true,
// do_behavior: true, do_behavior: true,
// llm_model: "gpt-5-mini", llm_model: "hunyuan-turbos-latest",
messages: [ messages: [
{ {
role: "system", role: "system",
@@ -193,8 +185,8 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
); );
// ------------------ 3️⃣ LLM 分析 ------------------ // ------------------ 3️⃣ LLM 分析 ------------------
const llmStream = await client1.chat.completions.create({ const llmStream = await client.chat.completions.create({
model: "gpt-5-mini", model: "hunyuan-turbos-latest",
stream: true, stream: true,
messages: [ messages: [
{ {