fix(exam): 正确返回文件未索引错误
This commit is contained in:
@@ -19,6 +19,7 @@ import os
|
||||
|
||||
# 导入考试管理模块
|
||||
from exam_pkg.manager import (
|
||||
check_file_indexed,
|
||||
generate_questions_from_file,
|
||||
grade_answers,
|
||||
)
|
||||
@@ -29,7 +30,7 @@ from auth.gateway import (
|
||||
)
|
||||
|
||||
# 导入统一响应格式
|
||||
from core.status_codes import EXAM_SUCCESS, GRADE_SUCCESS, EXAM_ERROR, GRADE_ERROR, BAD_REQUEST, UNAUTHORIZED, FORBIDDEN, NO_CONTENT, LLM_ERROR
|
||||
from core.status_codes import EXAM_SUCCESS, GRADE_SUCCESS, EXAM_ERROR, GRADE_ERROR, BAD_REQUEST, UNAUTHORIZED, FORBIDDEN, NO_CONTENT, LLM_ERROR, FILE_NOT_INDEXED
|
||||
from api.response_utils import success_response, error_response
|
||||
|
||||
# 合法题型
|
||||
@@ -81,6 +82,38 @@ def validate_exclude_stems(exclude_stems) -> str:
|
||||
exam_bp = Blueprint('exam', __name__)
|
||||
|
||||
|
||||
def _generation_failure_response(
|
||||
error_code: str,
|
||||
message: str,
|
||||
request_id=None,
|
||||
file_status=None,
|
||||
chunk_count: int = 0,
|
||||
):
|
||||
"""将出题内部失败统一转换为 HTTP 业务错误。"""
|
||||
data = {
|
||||
"request_id": request_id,
|
||||
"file_status": file_status,
|
||||
"chunk_count": chunk_count,
|
||||
}
|
||||
|
||||
if error_code == "FILE_NOT_INDEXED":
|
||||
return error_response(
|
||||
"FILE_NOT_INDEXED",
|
||||
FILE_NOT_INDEXED,
|
||||
message or "文件尚未完成向量化",
|
||||
http_status=409,
|
||||
data=data,
|
||||
)
|
||||
|
||||
return error_response(
|
||||
"EXAM_ERROR",
|
||||
EXAM_ERROR,
|
||||
message or "检查文件状态失败",
|
||||
http_status=500,
|
||||
data=data,
|
||||
)
|
||||
|
||||
|
||||
# ==================== 出题 API ====================
|
||||
|
||||
@exam_bp.route('/generate', methods=['POST'])
|
||||
@@ -192,6 +225,15 @@ def api_generate_questions():
|
||||
exclude_stems=data.get('exclude_stems')
|
||||
)
|
||||
|
||||
if not result.get('success', True):
|
||||
return _generation_failure_response(
|
||||
result.get('error_code'),
|
||||
result.get('message'),
|
||||
request_id=result.get('request_id'),
|
||||
file_status=result.get('file_status'),
|
||||
chunk_count=result.get('chunk_count', 0),
|
||||
)
|
||||
|
||||
return success_response(data=result, status_code=EXAM_SUCCESS, message="出题成功")
|
||||
|
||||
except Exception as e:
|
||||
@@ -263,6 +305,22 @@ def api_generate_smart():
|
||||
):
|
||||
return error_response("FORBIDDEN", FORBIDDEN, "权限不足", http_status=403)
|
||||
|
||||
# 智能出题必须在 AI 分析前确认文件已完成索引,避免无效模型调用。
|
||||
file_status = check_file_indexed(file_path, collection)
|
||||
if not file_status.get('indexed'):
|
||||
is_not_indexed = file_status.get('status') == 'not_found'
|
||||
return _generation_failure_response(
|
||||
"FILE_NOT_INDEXED" if is_not_indexed else "STATUS_CHECK_ERROR",
|
||||
(
|
||||
f"文件未向量化: {file_status.get('message', '')}"
|
||||
if is_not_indexed
|
||||
else f"检查文件状态失败: {file_status.get('message', '')}"
|
||||
),
|
||||
request_id=data.get('request_id'),
|
||||
file_status=file_status.get('status'),
|
||||
chunk_count=file_status.get('chunk_count', 0),
|
||||
)
|
||||
|
||||
# 1. 调用 AI 分析文件,获取推荐的题型和数量
|
||||
from exam_pkg.manager import analyze_file_for_exam
|
||||
ai_analysis = analyze_file_for_exam(
|
||||
@@ -286,6 +344,15 @@ def api_generate_smart():
|
||||
exclude_stems=data.get('exclude_stems')
|
||||
)
|
||||
|
||||
if not result.get('success', True):
|
||||
return _generation_failure_response(
|
||||
result.get('error_code'),
|
||||
result.get('message'),
|
||||
request_id=result.get('request_id'),
|
||||
file_status=result.get('file_status'),
|
||||
chunk_count=result.get('chunk_count', 0),
|
||||
)
|
||||
|
||||
# 3. 在返回结果中添加 AI 分析信息
|
||||
result['ai_analysis'] = ai_analysis
|
||||
|
||||
|
||||
Reference in New Issue
Block a user