feat(llm): 添加AI智能分析配置功能 新增LLM配置模块,支持通过阿里云DashScope API进行招标金额的智能提取。 配置包括API Key、Base URL、模型选择等,并提供启用开关。 前端界面增加“AI配置”标签页,包含状态展示、配置表单及测试连接功能。 后端增强parseDetailEnhanced方法,优先使用LLM提取金额,失败时降级至正则表达式。 同时实现LLM状态查询与连接测试接口,确保配置有效性。 配置文件中新增llm字段,默认关闭,支持安全存储API密钥。 ```
This commit is contained in:
@@ -6,6 +6,7 @@ import * as cheerio from 'cheerio';
|
||||
import iconv from 'iconv-lite';
|
||||
import { sendReportEmail } from './emailService.js';
|
||||
import { initScheduler, runTaskNow, reloadScheduler, getSchedulerStatus } from './scheduler.js';
|
||||
import { extractBudgetWithLLM, testLLMConnection, getLLMStatus, isLLMEnabled } from './llmService.js';
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 5000;
|
||||
@@ -321,7 +322,7 @@ function parseDetail(html) {
|
||||
};
|
||||
}
|
||||
|
||||
// 增强版parseDetail,支持PDF解析
|
||||
// 增强版parseDetail,支持PDF解析和LLM金额提取
|
||||
async function parseDetailEnhanced(html, pageUrl) {
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
@@ -359,8 +360,25 @@ async function parseDetailEnhanced(html, pageUrl) {
|
||||
content = htmlDetail.content;
|
||||
}
|
||||
|
||||
// 使用现有的extractBudget函数提取金额
|
||||
const budget = extractBudget(content);
|
||||
// 提取金额:优先使用 LLM,失败则降级到正则表达式
|
||||
let budget = null;
|
||||
if (isLLMEnabled()) {
|
||||
console.log('使用 LLM 提取金额...');
|
||||
budget = await extractBudgetWithLLM(content);
|
||||
if (budget) {
|
||||
console.log(`LLM 提取成功: ${budget.amount} ${budget.unit}`);
|
||||
} else {
|
||||
console.log('LLM 提取失败,降级到正则表达式');
|
||||
}
|
||||
}
|
||||
|
||||
// 如果 LLM 未启用或提取失败,使用正则表达式
|
||||
if (!budget) {
|
||||
budget = extractBudget(content);
|
||||
if (budget) {
|
||||
budget.source = 'regex'; // 标记来源
|
||||
}
|
||||
}
|
||||
|
||||
// 获取其他基本信息(标题、发布时间等)
|
||||
const basicInfo = parseDetail(html);
|
||||
@@ -749,10 +767,13 @@ app.get('/api/config', async (req, res) => {
|
||||
const configContent = readFileSync(configPath, 'utf-8');
|
||||
const config = JSON.parse(configContent);
|
||||
|
||||
// 不返回敏感信息(密码)
|
||||
// 不返回敏感信息(密码和API Key)
|
||||
if (config.email && config.email.smtpPass) {
|
||||
config.email.smtpPass = '***已配置***';
|
||||
}
|
||||
if (config.llm && config.llm.apiKey) {
|
||||
config.llm.apiKey = '***已配置***';
|
||||
}
|
||||
|
||||
res.json({ success: true, data: config });
|
||||
} catch (error) {
|
||||
@@ -774,11 +795,18 @@ app.post('/api/config', async (req, res) => {
|
||||
|
||||
const newConfig = req.body;
|
||||
|
||||
// 读取旧配置以保留敏感信息
|
||||
const oldConfigContent = readFileSync(configPath, 'utf-8');
|
||||
const oldConfig = JSON.parse(oldConfigContent);
|
||||
|
||||
// 如果密码字段是占位符,保留原密码
|
||||
if (newConfig.email && newConfig.email.smtpPass === '***已配置***') {
|
||||
const oldConfigContent = readFileSync(configPath, 'utf-8');
|
||||
const oldConfig = JSON.parse(oldConfigContent);
|
||||
newConfig.email.smtpPass = oldConfig.email.smtpPass;
|
||||
newConfig.email.smtpPass = oldConfig.email?.smtpPass || '';
|
||||
}
|
||||
|
||||
// 如果 LLM API Key 是占位符,保留原 API Key
|
||||
if (newConfig.llm && newConfig.llm.apiKey === '***已配置***') {
|
||||
newConfig.llm.apiKey = oldConfig.llm?.apiKey || '';
|
||||
}
|
||||
|
||||
// 保存配置
|
||||
@@ -793,6 +821,26 @@ app.post('/api/config', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// LLM 状态接口
|
||||
app.get('/api/llm/status', async (req, res) => {
|
||||
try {
|
||||
const status = getLLMStatus();
|
||||
res.json({ success: true, data: status });
|
||||
} catch (error) {
|
||||
res.status(500).json({ success: false, error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// LLM 连接测试接口
|
||||
app.post('/api/llm/test', async (req, res) => {
|
||||
try {
|
||||
const result = await testLLMConnection();
|
||||
res.json({ success: result.success, data: result });
|
||||
} catch (error) {
|
||||
res.status(500).json({ success: false, error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取定时任务状态
|
||||
app.get('/api/scheduler/status', async (req, res) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user