feat(llm): 添加AI智能分析配置功能 新增LLM配置模块,支持通过阿里云DashScope API进行招标金额的智能提取。 配置包括API Key、Base URL、模型选择等,并提供启用开关。 前端界面增加“AI配置”标签页,包含状态展示、配置表单及测试连接功能。 后端增强parseDetailEnhanced方法,优先使用LLM提取金额,失败时降级至正则表达式。 同时实现LLM状态查询与连接测试接口,确保配置有效性。 配置文件中新增llm字段,默认关闭,支持安全存储API密钥。 ```
This commit is contained in:
160
public/app.js
160
public/app.js
@@ -801,6 +801,7 @@ function updateCustomCron() {
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
loadEmailConfig();
|
||||
loadSchedulerConfig();
|
||||
loadLLMConfig();
|
||||
|
||||
// 添加自定义时间输入框的事件监听
|
||||
const customHour = document.getElementById('customHour');
|
||||
@@ -959,3 +960,162 @@ function showSchedulerStatus(message, type) {
|
||||
}, 3000);
|
||||
}
|
||||
}
|
||||
|
||||
// ========== LLM 配置功能 ==========
|
||||
|
||||
// 加载 LLM 配置
|
||||
async function loadLLMConfig() {
|
||||
try {
|
||||
const response = await fetch(`${API_BASE}/config`);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success && data.data && data.data.llm) {
|
||||
const llmConfig = data.data.llm;
|
||||
|
||||
document.getElementById('llmEnabled').checked = llmConfig.enabled || false;
|
||||
document.getElementById('llmApiKey').value = llmConfig.apiKey || '';
|
||||
document.getElementById('llmBaseUrl').value = llmConfig.baseUrl || 'https://dashscope.aliyuncs.com/compatible-mode/v1';
|
||||
document.getElementById('llmModel').value = llmConfig.model || 'qwen-turbo';
|
||||
}
|
||||
|
||||
// 更新状态显示
|
||||
await updateLLMStatus();
|
||||
} catch (error) {
|
||||
console.error('加载 LLM 配置失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新 LLM 状态显示
|
||||
async function updateLLMStatus() {
|
||||
try {
|
||||
const response = await fetch(`${API_BASE}/llm/status`);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success && data.data) {
|
||||
const status = data.data;
|
||||
|
||||
// 更新运行状态
|
||||
let statusText, statusColor;
|
||||
if (status.enabled) {
|
||||
statusText = '✓ 已启用';
|
||||
statusColor = '#28a745';
|
||||
} else if (status.configured) {
|
||||
statusText = '○ 已配置但未启用';
|
||||
statusColor = '#ffc107';
|
||||
} else {
|
||||
statusText = '✗ 未配置';
|
||||
statusColor = '#dc3545';
|
||||
}
|
||||
document.getElementById('llmRunningStatus').innerHTML = `<span style="color: ${statusColor}">${statusText}</span>`;
|
||||
|
||||
// 更新模型名称
|
||||
document.getElementById('llmModelName').textContent = status.model || '-';
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取 LLM 状态失败:', error);
|
||||
document.getElementById('llmRunningStatus').innerHTML = '<span style="color: #dc3545">获取状态失败</span>';
|
||||
}
|
||||
}
|
||||
|
||||
// 保存 LLM 配置
|
||||
async function saveLLMConfig() {
|
||||
const llmConfig = {
|
||||
enabled: document.getElementById('llmEnabled').checked,
|
||||
apiKey: document.getElementById('llmApiKey').value,
|
||||
baseUrl: document.getElementById('llmBaseUrl').value || 'https://dashscope.aliyuncs.com/compatible-mode/v1',
|
||||
model: document.getElementById('llmModel').value || 'qwen-turbo'
|
||||
};
|
||||
|
||||
// 验证必填项
|
||||
if (llmConfig.enabled && !llmConfig.apiKey) {
|
||||
showLLMStatus('启用 AI 功能需要填写 API Key', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
showLLMStatus('正在保存配置...', 'info');
|
||||
|
||||
try {
|
||||
// 先获取现有配置
|
||||
const configResponse = await fetch(`${API_BASE}/config`);
|
||||
const configData = await configResponse.json();
|
||||
|
||||
if (!configData.success) {
|
||||
showLLMStatus('获取配置失败', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
// 合并配置
|
||||
const fullConfig = {
|
||||
...configData.data,
|
||||
llm: llmConfig
|
||||
};
|
||||
|
||||
// 保存配置
|
||||
const response = await fetch(`${API_BASE}/config`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(fullConfig)
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
showLLMStatus('配置已保存!', 'success');
|
||||
await updateLLMStatus();
|
||||
} else {
|
||||
showLLMStatus(`保存失败: ${data.error}`, 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showLLMStatus(`请求失败: ${error.message}`, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// 测试 LLM 连接
|
||||
async function testLLMConnection() {
|
||||
showLLMStatus('正在测试连接...', 'info');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE}/llm/test`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success && data.data.success) {
|
||||
showLLMStatus(`连接成功! 模型: ${data.data.model}, 响应: ${data.data.reply}`, 'success');
|
||||
} else {
|
||||
showLLMStatus(`连接失败: ${data.data?.error || data.error || '未知错误'}`, 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showLLMStatus(`请求失败: ${error.message}`, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// 显示 LLM 配置状态
|
||||
function showLLMStatus(message, type) {
|
||||
const statusDiv = document.getElementById('llmConfigStatus');
|
||||
const bgColors = {
|
||||
success: '#d4edda',
|
||||
error: '#f8d7da',
|
||||
info: '#d1ecf1'
|
||||
};
|
||||
const textColors = {
|
||||
success: '#155724',
|
||||
error: '#721c24',
|
||||
info: '#0c5460'
|
||||
};
|
||||
|
||||
statusDiv.innerHTML = `
|
||||
<div style="background: ${bgColors[type]}; color: ${textColors[type]}; padding: 15px; border-radius: 8px;">
|
||||
${message}
|
||||
</div>
|
||||
`;
|
||||
|
||||
// 5秒后自动隐藏成功消息
|
||||
if (type === 'success') {
|
||||
setTimeout(() => {
|
||||
statusDiv.innerHTML = '';
|
||||
}, 5000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,6 +344,7 @@
|
||||
<button class="tab" onclick="switchTab('report')">生成报告</button>
|
||||
<button class="tab" onclick="switchTab('scheduler')">定时任务</button>
|
||||
<button class="tab" onclick="switchTab('email')">邮件配置</button>
|
||||
<button class="tab" onclick="switchTab('llm')">AI配置</button>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
@@ -632,6 +633,103 @@
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- AI配置 -->
|
||||
<div id="llm" class="tab-content">
|
||||
<h2 style="margin-bottom: 20px; color: #667eea;">AI 智能分析配置</h2>
|
||||
<p style="color: #666; margin-bottom: 20px;">使用大语言模型智能提取招标金额,提高金额识别的准确性</p>
|
||||
|
||||
<!-- AI 状态 -->
|
||||
<div id="llmStatus" style="margin-bottom: 30px; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 8px;">
|
||||
<h3 style="margin-top: 0; margin-bottom: 15px;">AI 服务状态</h3>
|
||||
<div style="display: flex; gap: 30px; flex-wrap: wrap;">
|
||||
<div>
|
||||
<div style="opacity: 0.9; font-size: 14px;">服务状态</div>
|
||||
<div style="font-size: 20px; font-weight: bold; margin-top: 5px;" id="llmRunningStatus">加载中...</div>
|
||||
</div>
|
||||
<div>
|
||||
<div style="opacity: 0.9; font-size: 14px;">当前模型</div>
|
||||
<div style="font-size: 20px; font-weight: bold; margin-top: 5px;" id="llmModelName">-</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 配置表单 -->
|
||||
<div class="form-group">
|
||||
<div class="checkbox-wrapper" onclick="document.getElementById('llmEnabled').click();">
|
||||
<input type="checkbox" id="llmEnabled" onclick="event.stopPropagation();">
|
||||
<label for="llmEnabled">启用 AI 金额提取</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>API Key *</label>
|
||||
<input type="password" id="llmApiKey" placeholder="请输入阿里云 DashScope API Key">
|
||||
<small style="color: #666; display: block; margin-top: 5px;">
|
||||
<a href="https://dashscope.console.aliyun.com/apiKey" target="_blank" style="color: #667eea;">点击这里获取 API Key</a>
|
||||
</small>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>API 地址</label>
|
||||
<input type="text" id="llmBaseUrl" value="https://dashscope.aliyuncs.com/compatible-mode/v1" placeholder="https://dashscope.aliyuncs.com/compatible-mode/v1">
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>模型选择</label>
|
||||
<select id="llmModel" style="width: 100%; padding: 12px; border: 2px solid #e0e0e0; border-radius: 8px; font-size: 14px;">
|
||||
<option value="qwen-turbo">通义千问 Turbo (快速、低成本)</option>
|
||||
<option value="qwen-plus">通义千问 Plus (更准确)</option>
|
||||
<option value="qwen-max">通义千问 Max (最强)</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<button class="btn" onclick="saveLLMConfig()">保存配置</button>
|
||||
<button class="btn" onclick="testLLMConnection()" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);">测试连接</button>
|
||||
<button class="btn" onclick="loadLLMConfig()" style="background: #6c757d;">刷新状态</button>
|
||||
|
||||
<div id="llmConfigStatus" style="margin-top: 20px;"></div>
|
||||
|
||||
<div style="margin-top: 30px; padding: 20px; background: #e8f5e9; border-radius: 8px; border-left: 4px solid #4caf50;">
|
||||
<h3 style="margin-top: 0; color: #2e7d32;">功能说明</h3>
|
||||
<ul style="line-height: 1.8; color: #2e7d32;">
|
||||
<li><strong>智能提取:</strong> 使用大语言模型理解公告内容,准确提取预算金额</li>
|
||||
<li><strong>自动降级:</strong> 当 AI 服务不可用时,自动使用正则表达式提取</li>
|
||||
<li><strong>支持模型:</strong> 阿里云通义千问系列模型(qwen-turbo/plus/max)</li>
|
||||
<li><strong>计费说明:</strong> 按实际调用量计费,qwen-turbo 约 0.0008元/千tokens</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div style="margin-top: 20px; padding: 20px; background: #fff3cd; border-radius: 8px; border-left: 4px solid #ffc107;">
|
||||
<h3 style="margin-top: 0; color: #856404;">模型对比</h3>
|
||||
<table style="width: 100%; border-collapse: collapse; margin-top: 10px;">
|
||||
<tr style="border-bottom: 1px solid #ddd;">
|
||||
<th style="text-align: left; padding: 8px; color: #856404;">模型</th>
|
||||
<th style="text-align: left; padding: 8px; color: #856404;">速度</th>
|
||||
<th style="text-align: left; padding: 8px; color: #856404;">准确度</th>
|
||||
<th style="text-align: left; padding: 8px; color: #856404;">成本</th>
|
||||
</tr>
|
||||
<tr style="border-bottom: 1px solid #ddd;">
|
||||
<td style="padding: 8px;">qwen-turbo</td>
|
||||
<td style="padding: 8px;">最快</td>
|
||||
<td style="padding: 8px;">良好</td>
|
||||
<td style="padding: 8px;">最低</td>
|
||||
</tr>
|
||||
<tr style="border-bottom: 1px solid #ddd;">
|
||||
<td style="padding: 8px;">qwen-plus</td>
|
||||
<td style="padding: 8px;">较快</td>
|
||||
<td style="padding: 8px;">优秀</td>
|
||||
<td style="padding: 8px;">中等</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 8px;">qwen-max</td>
|
||||
<td style="padding: 8px;">较慢</td>
|
||||
<td style="padding: 8px;">最佳</td>
|
||||
<td style="padding: 8px;">较高</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user