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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user