37 lines
867 B
Python
37 lines
867 B
Python
from flask import Blueprint, jsonify, request
|
|
|
|
from ..qa import top_questions, get_question_by_id
|
|
from ..rag import search_rag_full
|
|
|
|
bp = Blueprint("faq", __name__, url_prefix="/api/faq")
|
|
|
|
|
|
@bp.get("/top")
|
|
def get_top():
|
|
return jsonify({"items": top_questions()})
|
|
|
|
|
|
@bp.post("/search")
|
|
def search():
|
|
data = request.get_json(force=True, silent=True) or {}
|
|
query = (data.get("query") or "").strip()
|
|
if not query:
|
|
items = top_questions()
|
|
else:
|
|
items = search_rag_full(query, limit=10)
|
|
return jsonify({"items": items})
|
|
|
|
|
|
@bp.get("/item/<int:qid>")
|
|
def get_item(qid: int):
|
|
item = get_question_by_id(qid)
|
|
if not item:
|
|
return jsonify({"error": "not found"}), 404
|
|
return jsonify(item)
|
|
|
|
|
|
@bp.get("/random")
|
|
def random_items():
|
|
# 保留接口,但返回 top 列表
|
|
return jsonify({"items": top_questions()})
|