feat: 后台管理添加大模型接口配置功能

- 网站配置页面新增 LLM 配置区域
- 支持配置 API 地址、API Key、文本模型、视觉模型
- LLM 配置从 config.json 动态读取
- 不再使用硬编码的 LLM_CONFIG 常量
This commit is contained in:
2026-04-27 19:57:22 +08:00
parent 9525d56ffc
commit b40e890e2b
2 changed files with 70 additions and 6 deletions

34
app.py
View File

@@ -49,7 +49,7 @@ LLM_CONFIG = {
'vision_model': 'gpt-4-vision-preview', # 视觉模型(解析图片)
}
# 默认网站配置
# 默认网站配置包含LLM配置
DEFAULT_CONFIG = {
'site_name': 'ParamHub',
'site_subtitle': '参数百科',
@@ -58,12 +58,31 @@ DEFAULT_CONFIG = {
'copyright_year': '2024',
'contact_email': '',
'github_url': '',
# LLM配置
'llm_base_url': 'http://192.168.2.17:19007/v1',
'llm_api_key': '',
'llm_model': 'auto',
'llm_vision_model': 'gpt-4-vision-preview',
}
def get_llm_config():
"""获取LLM配置从config.json动态读取"""
config = load_config()
return {
'base_url': config.get('llm_base_url', DEFAULT_CONFIG['llm_base_url']),
'api_key': config.get('llm_api_key', DEFAULT_CONFIG['llm_api_key']),
'model': config.get('llm_model', DEFAULT_CONFIG['llm_model']),
'vision_model': config.get('llm_vision_model', DEFAULT_CONFIG['llm_vision_model']),
}
def load_config():
"""加载网站配置"""
if CONFIG_FILE.exists():
return json.loads(CONFIG_FILE.read_text(encoding='utf-8'))
loaded = json.loads(CONFIG_FILE.read_text(encoding='utf-8'))
# 合并默认配置(确保新字段存在)
result = DEFAULT_CONFIG.copy()
result.update(loaded)
return result
return DEFAULT_CONFIG.copy()
def save_config(config):
@@ -219,14 +238,17 @@ def parse_with_llm(text, category_type, images=None):
})
try:
# 使用视觉模型解析
model = LLM_CONFIG.get('vision_model', 'gpt-4-vision-preview') if images else LLM_CONFIG['model']
# 动态获取LLM配置
llm_config = get_llm_config()
# 使用视觉模型解析(如果有图片)
model = llm_config.get('vision_model', 'gpt-4-vision-preview') if images else llm_config['model']
response = requests.post(
f"{LLM_CONFIG['base_url']}/chat/completions",
f"{llm_config['base_url']}/chat/completions",
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {LLM_CONFIG['api_key']}"
"Authorization": f"Bearer {llm_config['api_key']}"
},
json={
"model": model,

View File

@@ -57,6 +57,8 @@
<h1 class="text-2xl font-bold text-gray-800 mb-6">网站配置</h1>
<div class="bg-white rounded-xl shadow-sm p-6">
<form id="configForm" class="space-y-6">
<!-- 网站基础配置 -->
<h3 class="text-lg font-semibold text-gray-800 border-b pb-2 mb-4"><i class="ri-global-line mr-2"></i>网站基础配置</h3>
<div class="grid grid-cols-2 gap-6">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">网站名称</label>
@@ -87,6 +89,33 @@
<label class="block text-sm font-medium text-gray-700 mb-2">页脚文字</label>
<textarea name="footer_text" id="config_footer_text" rows="3" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500" placeholder="网站底部的版权信息等"></textarea>
</div>
<!-- 大模型接口配置 -->
<h3 class="text-lg font-semibold text-gray-800 border-b pb-2 mb-4 mt-8"><i class="ri-robot-line mr-2"></i>大模型接口配置(用于智能解析)</h3>
<div class="bg-blue-50 rounded-lg p-4 mb-4">
<p class="text-sm text-blue-700"><i class="ri-information-line mr-1"></i>配置用于智能解析产品参数的大模型API接口。文本解析使用普通模型图片解析使用视觉模型。</p>
</div>
<div class="grid grid-cols-2 gap-6">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">API地址</label>
<input type="url" name="llm_base_url" id="config_llm_base_url" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500" placeholder="http://192.168.2.17:19007/v1">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">API Key</label>
<input type="text" name="llm_api_key" id="config_llm_api_key" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500" placeholder="留空则不验证">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">文本解析模型</label>
<input type="text" name="llm_model" id="config_llm_model" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500" placeholder="auto">
<p class="text-xs text-gray-500 mt-1">用于解析文本数据,如 deepseek-v3、qwen3.5 等</p>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">图片解析模型(视觉模型)</label>
<input type="text" name="llm_vision_model" id="config_llm_vision_model" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500" placeholder="gpt-4-vision-preview">
<p class="text-xs text-gray-500 mt-1">用于解析图片,如 Qwen/Qwen2-VL-72B-Instruct、gpt-4-vision-preview 等</p>
</div>
</div>
<div class="flex justify-end gap-4">
<button type="button" onclick="loadSiteConfig()" class="px-4 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300"><i class="ri-refresh-line mr-1"></i>重置</button>
<button type="button" onclick="saveSiteConfig()" class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"><i class="ri-save-line mr-1"></i>保存配置</button>
@@ -553,6 +582,7 @@
const res = await fetch('/api/config');
const config = await res.json();
// 网站基础配置
document.getElementById('config_site_name').value = config.site_name || '';
document.getElementById('config_site_subtitle').value = config.site_subtitle || '';
document.getElementById('config_icp_number').value = config.icp_number || '';
@@ -560,11 +590,18 @@
document.getElementById('config_github_url').value = config.github_url || '';
document.getElementById('config_copyright_year').value = config.copyright_year || '';
document.getElementById('config_footer_text').value = config.footer_text || '';
// LLM配置
document.getElementById('config_llm_base_url').value = config.llm_base_url || 'http://192.168.2.17:19007/v1';
document.getElementById('config_llm_api_key').value = config.llm_api_key || '';
document.getElementById('config_llm_model').value = config.llm_model || 'auto';
document.getElementById('config_llm_vision_model').value = config.llm_vision_model || 'gpt-4-vision-preview';
}
// 保存网站配置
async function saveSiteConfig() {
const config = {
// 网站基础配置
site_name: document.getElementById('config_site_name').value,
site_subtitle: document.getElementById('config_site_subtitle').value,
icp_number: document.getElementById('config_icp_number').value,
@@ -572,6 +609,11 @@
github_url: document.getElementById('config_github_url').value,
copyright_year: document.getElementById('config_copyright_year').value,
footer_text: document.getElementById('config_footer_text').value,
// LLM配置
llm_base_url: document.getElementById('config_llm_base_url').value,
llm_api_key: document.getElementById('config_llm_api_key').value,
llm_model: document.getElementById('config_llm_model').value,
llm_vision_model: document.getElementById('config_llm_vision_model').value,
};
try {