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 @@