From b40e890e2bb69a550f64a2dc2061a4e4467c91f8 Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Mon, 27 Apr 2026 19:57:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=8E=E5=8F=B0=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=A7=E6=A8=A1=E5=9E=8B=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 网站配置页面新增 LLM 配置区域 - 支持配置 API 地址、API Key、文本模型、视觉模型 - LLM 配置从 config.json 动态读取 - 不再使用硬编码的 LLM_CONFIG 常量 --- app.py | 34 ++++++++++++++++++++++++++++------ templates/admin.html | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index c1ec895..6c329c7 100644 --- a/app.py +++ b/app.py @@ -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, diff --git a/templates/admin.html b/templates/admin.html index 0acba2c..cc8a32a 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -57,6 +57,8 @@