v1.1.0 管理后台+对话增强:/admin全数据CRUD与站点配置;快捷语句点击自动提交;Markdown回答渲染;参考资讯折叠链接;实体识别标记与快速查看卡片(entity_linker)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""NBA球迷大全 - Flask 服务入口(API + 前端静态页)"""
|
||||
"""NBA球迷大全 - Flask 服务入口(API + 前端静态页 + 管理后台)"""
|
||||
import logging
|
||||
import os
|
||||
|
||||
@@ -10,6 +10,8 @@ from db import init_db, table_count, query_one
|
||||
import tools
|
||||
import chat
|
||||
import vector_store
|
||||
import entity_linker
|
||||
import admin as admin_mod
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
||||
log = logging.getLogger("app")
|
||||
@@ -24,6 +26,11 @@ def index():
|
||||
return send_from_directory(STATIC_DIR, "index.html")
|
||||
|
||||
|
||||
@app.route("/admin")
|
||||
def admin_page():
|
||||
return send_from_directory(STATIC_DIR, "admin.html")
|
||||
|
||||
|
||||
@app.route("/static/<path:path>")
|
||||
def static_files(path):
|
||||
return send_from_directory(STATIC_DIR, path)
|
||||
@@ -42,6 +49,12 @@ def suggestions():
|
||||
return jsonify(chat.suggest_questions())
|
||||
|
||||
|
||||
@app.route("/api/boot")
|
||||
def boot():
|
||||
"""对话界面启动信息:开场白 + 快捷问题(管理后台可配置)"""
|
||||
return jsonify(chat.boot_info())
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ 对话
|
||||
@app.route("/api/chat", methods=["POST"])
|
||||
def api_chat():
|
||||
@@ -51,8 +64,14 @@ def api_chat():
|
||||
if not message:
|
||||
return jsonify({"error": "消息不能为空"}), 400
|
||||
try:
|
||||
reply, sources, used_tools = chat.chat_once(message, history)
|
||||
return jsonify({"reply": reply, "sources": sources, "used_tools": used_tools})
|
||||
reply, sources, used_tools, news_refs = chat.chat_once(message, history)
|
||||
# 实体识别程序:扫描回答,标记球队/球员/人物/比赛 + 快速查看卡片
|
||||
game_refs = [it for s in sources
|
||||
if s.get("tool") in ("search_games", "get_game_detail")
|
||||
for it in s.get("items", [])]
|
||||
spans, cards = entity_linker.link_entities(reply, game_refs=game_refs)
|
||||
return jsonify({"reply": reply, "sources": sources, "used_tools": used_tools,
|
||||
"news_refs": news_refs, "entities": spans, "cards": cards})
|
||||
except Exception as e:
|
||||
log.exception("chat error")
|
||||
return jsonify({"error": f"服务异常: {e}"}), 500
|
||||
@@ -168,7 +187,74 @@ def api_person(pid):
|
||||
return jsonify(p)
|
||||
|
||||
|
||||
# ================================================================== 管理后台
|
||||
@app.route("/api/admin/login", methods=["POST"])
|
||||
def admin_login():
|
||||
return admin_mod.login()
|
||||
|
||||
|
||||
@app.route("/api/admin/logout", methods=["POST"])
|
||||
@admin_mod.require_admin
|
||||
def admin_logout():
|
||||
return admin_mod.logout()
|
||||
|
||||
|
||||
@app.route("/api/admin/stats")
|
||||
@admin_mod.require_admin
|
||||
def admin_stats():
|
||||
return admin_mod.stats()
|
||||
|
||||
|
||||
@app.route("/api/admin/config")
|
||||
@admin_mod.require_admin
|
||||
def admin_config_get():
|
||||
return admin_mod.get_config_api()
|
||||
|
||||
|
||||
@app.route("/api/admin/config", methods=["PUT"])
|
||||
@admin_mod.require_admin
|
||||
def admin_config_put():
|
||||
return admin_mod.update_config()
|
||||
|
||||
|
||||
@app.route("/api/admin/suggestions/reset", methods=["POST"])
|
||||
@admin_mod.require_admin
|
||||
def admin_suggestions_reset():
|
||||
return admin_mod.reset_suggestions()
|
||||
|
||||
|
||||
@app.route("/api/admin/<table>")
|
||||
@admin_mod.require_admin
|
||||
def admin_list(table):
|
||||
return admin_mod.list_rows(table)
|
||||
|
||||
|
||||
@app.route("/api/admin/<table>", methods=["POST"])
|
||||
@admin_mod.require_admin
|
||||
def admin_create(table):
|
||||
return admin_mod.create_row(table)
|
||||
|
||||
|
||||
@app.route("/api/admin/<table>/<int:rid>")
|
||||
@admin_mod.require_admin
|
||||
def admin_get(table, rid):
|
||||
return admin_mod.get_row(table, rid)
|
||||
|
||||
|
||||
@app.route("/api/admin/<table>/<int:rid>", methods=["PUT"])
|
||||
@admin_mod.require_admin
|
||||
def admin_update(table, rid):
|
||||
return admin_mod.update_row(table, rid)
|
||||
|
||||
|
||||
@app.route("/api/admin/<table>/<int:rid>", methods=["DELETE"])
|
||||
@admin_mod.require_admin
|
||||
def admin_delete(table, rid):
|
||||
return admin_mod.delete_row(table, rid)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
init_db()
|
||||
admin_mod.init_defaults()
|
||||
log.info("%s 启动于 http://%s:%s", SERVICE_NAME, SERVICE_HOST, SERVICE_PORT)
|
||||
app.run(host=SERVICE_HOST, port=SERVICE_PORT, threaded=True)
|
||||
Reference in New Issue
Block a user