commit by Kalman.CHENG ☆
This commit is contained in:
@@ -6,6 +6,7 @@ from starlette.requests import Request
|
||||
from utils.Database import *
|
||||
from auth.dependencies import get_current_user
|
||||
from utils.ParseRequest import get_request_num_param
|
||||
from utils.TranslateUtil import get_stage_map_by_id
|
||||
|
||||
# 创建一个路由实例,需要依赖get_current_user,登录后才能访问
|
||||
router = APIRouter(dependencies=[Depends(get_current_user)])
|
||||
@@ -37,7 +38,9 @@ async def get_stage_list():
|
||||
@router.get("/getSubjectList")
|
||||
async def get_subject_list(request: Request):
|
||||
stage_id = await get_request_num_param(request, "stage_id", True, True, None)
|
||||
stage_name = await get_stage_map_by_id(stage_id)
|
||||
# 先查询学科list
|
||||
select_subject_sql: str = "select subject_id, subject_name, icon from t_dm_subject where stage_id = " + str(stage_id) + " order by sort_id;"
|
||||
select_subject_sql: str = f"select subject_id, subject_name, icon, {stage_id} as stage_id, '{stage_name}' as stage_name from t_dm_subject where stage_id = {stage_id} order by sort_id;"
|
||||
print(select_subject_sql)
|
||||
subject_list = await find_by_sql(select_subject_sql,())
|
||||
return {"success": True, "message": "查询成功!", "data": {"subject_list": subject_list}}
|
@@ -88,14 +88,23 @@ async def get_question(request: Request):
|
||||
if question_type == 2:
|
||||
person_sql = f"AND person_id = '{person_id}'"
|
||||
# 数据库查询
|
||||
select_question_sql: str = f"SELECT * FROM t_ai_teaching_model_question WHERE is_deleted = 0 and bureau_id = '{bureau_id}' AND theme_id = {theme_id} AND question_type = {question_type} {person_sql} ORDER BY quote_count DESC"
|
||||
select_question_sql: str = f"SELECT * FROM t_ai_teaching_model_question WHERE is_deleted = 0 and bureau_id = '{bureau_id}' AND theme_id = {theme_id} AND question_type = {question_type} {person_sql} ORDER BY quote_count DESC, create_time DESC"
|
||||
print(select_question_sql)
|
||||
page = await get_page_data_by_sql(select_question_sql, page_number, page_size)
|
||||
return {"success": True, "message": "查询成功!", "data": page}
|
||||
|
||||
|
||||
|
||||
# 【TeachingModel-5】提问
|
||||
##########################################################################################
|
||||
# 功能:【TeachingModel-5】向RAG提问,返回RAG的回复
|
||||
# 作者:Kalman.CHENG ☆
|
||||
# 时间:2025-08-14
|
||||
# 备注:question_type=2时,question_id必填,其他情况question_id不填;
|
||||
# question_type(0:新增问题;1:常见问题;2:个人历史问题;):
|
||||
# 0【新增问题】:会保存成个人历史问题,并保存答案
|
||||
# 1【常见问题】:增加常见问题引用次数,会保存成个人历史问题,并保存答案
|
||||
# 2【个人历史问题】对应页面新增的历史问题回显后“重试”操作:会重新回答问题,并替换最新答案
|
||||
##########################################################################################
|
||||
@router.post("/sendQuestion")
|
||||
async def send_question(request: Request):
|
||||
# 获取参数
|
||||
@@ -103,22 +112,19 @@ async def send_question(request: Request):
|
||||
person_id = await get_request_str_param(request, "person_id", True, True)
|
||||
theme_id = await get_request_num_param(request, "theme_id", True, True, None)
|
||||
question = await get_request_str_param(request, "question", True, True)
|
||||
question_type = await get_request_num_param(request, "question_type", False, True, 0)
|
||||
question_type = await get_request_num_param(request, "question_type", True, True, None)
|
||||
question_id = await get_request_num_param(request, "question_id", False, True,0)
|
||||
|
||||
theme_object = await find_by_id("t_ai_teaching_model_theme", "id", theme_id)
|
||||
if theme_object is None:
|
||||
return {"success": False, "message": "主题不存在!"}
|
||||
# 处理theme的调用次数
|
||||
update_sql: str = f"UPDATE t_ai_teaching_model_theme SET quote_count = quote_count + 1, update_time = now() WHERE id = {theme_id}"
|
||||
await execute_sql(update_sql, ())
|
||||
|
||||
if question_type == 1:
|
||||
# 处理常见问题引用次数
|
||||
update_common_question_sql: str = f"update t_ai_teaching_model_question set quote_count = quote_count + 1 where question_type = 1 and theme_id = {theme_id} and question = '{question}' and is_deleted = 0"
|
||||
await execute_sql(update_common_question_sql, ())
|
||||
elif question_type == 2:
|
||||
# 处理个人历史问题引用次数
|
||||
update_person_question_sql: str = f"update t_ai_teaching_model_question set quote_count = quote_count + 1 where question_type = 2 and theme_id = {theme_id} and question = '{question}' and person_id = '{person_id}' and is_deleted = 0"
|
||||
await execute_sql(update_person_question_sql, ())
|
||||
else:
|
||||
# 新问题,保存个人历史问题
|
||||
if question_type == 2 and question_id == 0:
|
||||
return {"success": False, "message": "[question_type]=2时,[question_id]不允许为空!"}
|
||||
if question_type != 2:
|
||||
param = {}
|
||||
param["stage_id"] = int(theme_object["stage_id"])
|
||||
param["subject_id"] = int(theme_object["subject_id"])
|
||||
@@ -130,10 +136,10 @@ async def send_question(request: Request):
|
||||
param["person_id"] = person_id
|
||||
param["bureau_id"] = bureau_id
|
||||
question_id = await insert("t_ai_teaching_model_question", param)
|
||||
|
||||
# 处理theme的调用次数
|
||||
update_sql: str = f"UPDATE t_ai_teaching_model_theme SET quote_count = quote_count + 1, update_time = now() WHERE id = {theme_id}"
|
||||
await execute_sql(update_sql, ())
|
||||
if question_type == 1:
|
||||
# 处理常见问题引用次数
|
||||
update_common_question_sql: str = f"update t_ai_teaching_model_question set quote_count = quote_count + 1 where question_type = 1 and theme_id = {theme_id} and question = '{question}' and is_deleted = 0"
|
||||
await execute_sql(update_common_question_sql, ())
|
||||
|
||||
# 向rag提问
|
||||
topic = theme_object["short_name"]
|
||||
@@ -142,25 +148,29 @@ async def send_question(request: Request):
|
||||
prompt = prompt + "\n 2、资料中提供化学反应方程式的,一定要严格按提供的Latex公式输出,绝对不允许对Latex公式进行修改 !"
|
||||
prompt = prompt + "\n 3、如果资料中提供了图片的,一定要严格按照原文提供图片输出,绝对不能省略或不输出!"
|
||||
prompt = prompt + "\n 4、知识库中存在的问题,严格按知识库中的内容回答,不允许扩展!"
|
||||
prompt = prompt + "\n 5、如果问题与提供的知识库内容不符,则明确告诉未在知识库范围内提到!"
|
||||
prompt = prompt + "\n 5、如果问题与提供的知识库内容不符,则明确告诉未在知识库范围内提到,请这样告诉我:“暂时无法回答这个问题呢,我专注于【" + theme_object["theme_name"] + "】这方面知识,若需这方面帮助,请告知我更准确的信息吧~”"
|
||||
prompt = prompt + "\n 6、发现输出内容中包含Latex公式的,一定要检查是不是包含了$$或$的包含符号,不能让Latex无包含符号出现!"
|
||||
working_path = "./Topic/" + topic
|
||||
# if rag_type == "file":
|
||||
async def generate_response_stream(query: str, mode: str, user_prompt: str):
|
||||
try:
|
||||
result = ""
|
||||
rag = await initialize_rag(working_path)
|
||||
resp = await rag.aquery(
|
||||
query=query,
|
||||
param=QueryParam(mode=mode, stream=True, user_prompt=user_prompt, enable_rerank=True))
|
||||
|
||||
async for chunk in resp:
|
||||
if not chunk:
|
||||
continue
|
||||
yield f"data: {json.dumps({'reply': chunk})}\n\n"
|
||||
result += chunk
|
||||
print(chunk, end='', flush=True)
|
||||
except Exception as e:
|
||||
yield f"data: {json.dumps({'error': str(e)})}\n\n"
|
||||
finally:
|
||||
# 保存答案
|
||||
update_question_sql = f"update t_ai_teaching_model_question set question_answer = '{result}', update_time = now() where id = {question_id}"
|
||||
await execute_sql(update_question_sql, ())
|
||||
# 清理资源
|
||||
await rag.finalize_storages()
|
||||
|
||||
@@ -251,6 +261,32 @@ async def save_word(request: Request):
|
||||
|
||||
|
||||
|
||||
########################################
|
||||
# 【TeachingModel-7】保存个人历史问题答案
|
||||
# 作者:Kalman.CHENG ☆
|
||||
# 时间:2025-08-14
|
||||
# 备注:
|
||||
########################################
|
||||
@router.post("/saveAnswer")
|
||||
async def save_answer(request: Request):
|
||||
# 获取参数
|
||||
bureau_id = await get_request_str_param(request, "bureau_id", True, True)
|
||||
person_id = await get_request_str_param(request, "person_id", True, True)
|
||||
theme_id = await get_request_num_param(request, "theme_id", True, True, None)
|
||||
question = await get_request_str_param(request, "question", True, True)
|
||||
answer = await get_request_str_param(request, "answer", True, True)
|
||||
|
||||
# 验证是否存在个人历史问题
|
||||
select_question_sql: str = f"SELECT * FROM t_ai_teaching_model_question WHERE is_deleted = 0 and bureau_id = '{bureau_id}' AND theme_id = {theme_id} AND question_type = 2 AND question = '{question}' AND person_id = '{person_id}' order by create_time desc limit 1"
|
||||
print(select_question_sql)
|
||||
select_question_result = await find_by_sql(select_question_sql, ())
|
||||
if select_question_result is None: # 个人历史问题不存在
|
||||
return {"success": False, "message": "个人历史问题不存在!"}
|
||||
|
||||
|
||||
question_id = select_question_result[0].get("id")
|
||||
# 保存答案
|
||||
update_answer_sql: str = f"UPDATE t_ai_teaching_model_question SET question_answer = '{answer}', update_time = now() WHERE id = {question_id}"
|
||||
print(update_answer_sql)
|
||||
await execute_sql(update_answer_sql, ())
|
||||
# 结果返回
|
||||
return {"success": True, "message": "保存成功!"}
|
Reference in New Issue
Block a user