feat: 添加配置管理功能和修复搜索问题

新增:
- 系统设置页面 (/settings) - 支持动态配置LLM、索引、文档处理参数
- 配置API - 保存配置、测试LLM连接
- 前端JS交互文件 - 搜索、文档管理功能

修复:
- 首页搜索框无法正常工作的问题(缺少main.js)
- 服务支持动态读取配置(无需重启生效)

改进:
- LLM/索引/文档配置支持热更新
- 添加测试LLM连接功能
This commit is contained in:
2026-04-09 12:54:31 +08:00
parent 4fb4d61877
commit 8c7a99d83f
6 changed files with 712 additions and 24 deletions

103
app.py
View File

@@ -13,6 +13,38 @@ from config import *
from models import db, Document, DocumentChunk, InvertedIndex, QueryLog, IndexStats
from services import DocumentIndexer, SearchEngine, RAGGenerator
# ==================== 配置文件路径 ====================
CONFIG_FILE = os.path.join(os.path.dirname(__file__), 'config.py')
USER_CONFIG_FILE = os.path.join(os.path.dirname(__file__), 'user_config.json')
def load_user_config():
"""加载用户配置"""
if os.path.exists(USER_CONFIG_FILE):
with open(USER_CONFIG_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
return {}
def save_user_config(config_type, config_data):
"""保存用户配置"""
user_config = load_user_config()
user_config[config_type] = config_data
with open(USER_CONFIG_FILE, 'w', encoding='utf-8') as f:
json.dump(user_config, f, ensure_ascii=False, indent=2)
def get_effective_config():
"""获取有效配置(用户配置覆盖默认配置)"""
user_config = load_user_config()
return {
'llm': {**LLM_CONFIG, **user_config.get('llm', {})},
'index': {**INDEX_CONFIG, **user_config.get('index', {})},
'doc': {**DOC_CONFIG, **user_config.get('doc', {})}
}
# ==================== 创建应用 ====================
app = Flask(__name__)
app.config['SECRET_KEY'] = SECRET_KEY
@@ -66,6 +98,13 @@ def search_page():
return render_template('search.html')
@app.route('/settings')
def settings_page():
"""设置页"""
config = get_effective_config()
return render_template('settings.html', config=config)
# ==================== API路由 ====================
# === 文档管理 ===
@@ -315,6 +354,70 @@ def api_log_feedback(log_id):
return jsonify({'success': True})
# === 配置管理 ===
@app.route('/api/config', methods=['GET'])
def api_get_config():
"""获取当前配置"""
return jsonify(get_effective_config())
@app.route('/api/config/llm', methods=['POST'])
def api_save_llm_config():
"""保存LLM配置"""
data = request.json
save_user_config('llm', data)
return jsonify({'success': True})
@app.route('/api/config/index', methods=['POST'])
def api_save_index_config():
"""保存索引配置"""
data = request.json
save_user_config('index', data)
return jsonify({'success': True})
@app.route('/api/config/doc', methods=['POST'])
def api_save_doc_config():
"""保存文档配置"""
data = request.json
save_user_config('doc', data)
return jsonify({'success': True})
@app.route('/api/config/test', methods=['POST'])
def api_test_config():
"""测试LLM连接"""
config = get_effective_config()
llm_config = config['llm']
try:
from openai import OpenAI
client = OpenAI(
api_key=llm_config['api_key'],
base_url=llm_config['api_base'],
)
# 发送简单测试请求
response = client.chat.completions.create(
model=llm_config['model'],
messages=[{"role": "user", "content": "Hello"}],
max_tokens=10
)
return jsonify({
'success': True,
'model': llm_config['model'],
'response': response.choices[0].message.content
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
})
# ==================== 启动 ====================
if __name__ == '__main__':
init_app()