Add batch parallelism for LLM classification and refactor API
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
const express = require("express");
|
||||
const { getDB } = require("../db");
|
||||
const { runAutoReconciliation } = require("../services/matcher");
|
||||
const { classifyDeals } = require("../services/llm");
|
||||
const { classifyDeals, classifyAllDeals } = require("../services/llm");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -135,6 +135,9 @@ router.get("/needs-classify", (req, res) => {
|
||||
`SELECT id FROM deals WHERE category IS NULL OR category NOT IN (${ph})`,
|
||||
)
|
||||
.all(...cats);
|
||||
console.log(
|
||||
`[Reco] GET /transactions/needs-classify — 返回 ${stale.length} 个待分类 deal`,
|
||||
);
|
||||
res.json({ ids: stale.map((r) => r.id), total: stale.length });
|
||||
});
|
||||
|
||||
@@ -205,29 +208,40 @@ router.post("/reconcile", (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
// POST /api/transactions/classify-all — 重分类 deals
|
||||
router.post("/classify-all", async (req, res) => {
|
||||
const db = getDB();
|
||||
const cats = db
|
||||
.prepare("SELECT name FROM categories")
|
||||
.all()
|
||||
.map((r) => r.name);
|
||||
if (!cats.length) return res.json({ classified: 0 });
|
||||
const ph = cats.map(() => "?").join(",");
|
||||
const stale = db
|
||||
.prepare(
|
||||
`SELECT id FROM deals WHERE category IS NULL OR category NOT IN (${ph})`,
|
||||
)
|
||||
.all(...cats);
|
||||
if (!stale.length) return res.json({ classified: 0, total: 0 });
|
||||
const ids = stale.map((r) => r.id),
|
||||
B = 50;
|
||||
let n = 0;
|
||||
for (let i = 0; i < ids.length; i += B) {
|
||||
const r = await classifyDeals(ids.slice(i, i + B));
|
||||
n += r.classified;
|
||||
// POST /api/transactions/classify — 对指定 deal 批量分类
|
||||
router.post("/classify", async (req, res) => {
|
||||
const { ids } = req.body;
|
||||
console.log(
|
||||
`[Reco] POST /transactions/classify — 收到 ${ids ? ids.length : 0} 个ID`,
|
||||
);
|
||||
if (!ids || !Array.isArray(ids) || ids.length === 0) {
|
||||
return res.status(400).json({ error: "请提供交易ID列表" });
|
||||
}
|
||||
try {
|
||||
const result = await classifyDeals(ids);
|
||||
console.log(
|
||||
`[Reco] POST /transactions/classify — 完成, classified: ${result.classified}, source: ${result.source}`,
|
||||
);
|
||||
res.json({ success: true, ...result });
|
||||
} catch (err) {
|
||||
console.error("[Reco] 分类失败:", err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/transactions/classify-all — AI分类全部(并行)
|
||||
router.post("/classify-all", async (req, res) => {
|
||||
console.log(`[Reco] POST /transactions/classify-all — 开始`);
|
||||
try {
|
||||
const result = await classifyAllDeals();
|
||||
console.log(
|
||||
`[Reco] POST /transactions/classify-all — 完成, classified: ${result.classified}, total: ${result.total}, source: ${result.source}`,
|
||||
);
|
||||
res.json({ success: true, ...result });
|
||||
} catch (err) {
|
||||
console.error("[Reco] 分类全部失败:", err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
res.json({ success: true, classified: n, total: ids.length });
|
||||
});
|
||||
|
||||
// GET /api/transactions/stats/summary - 获取汇总统计
|
||||
|
||||
Reference in New Issue
Block a user