diff --git a/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py b/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py index f0381f5..d697cff 100644 --- a/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py +++ b/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py @@ -23,6 +23,12 @@ from module_admin.entity.vo.ragflow_vo import ( from utils.log_util import logger from utils.response_util import ResponseUtil from utils.semantic_cache_service import get_semantic_cache_service, lookup_question, store_qa_pair + +def _get_question_hash(question: str) -> str: + """计算问题的hash值""" + import hashlib + normalized = question.lower().strip() + return hashlib.md5(normalized.encode('utf-8')).hexdigest()[:16] from utils.static_qa_service import get_static_qa_service @@ -30,6 +36,8 @@ async def _async_store_qa(chat_id: str, question: str, answer: str, redis) -> No """ 异步存储问答对到语义缓存 """ + store_hash = _get_question_hash(question) + logger.info(f"[SemanticCache] 存储QA | chat_id={chat_id} | question={question} | hash={store_hash} | answer_length={len(answer)}") try: await store_qa_pair(chat_id, question, answer, redis) except Exception as e: @@ -155,7 +163,8 @@ async def converse_with_chat_assistant( # ========== 2. RAG历史缓存查找 ========== if redis: - logger.info(f'[SemanticCache] 开始查找 | chat_id={converse_params.chat_id} | question={converse_params.question} | threshold=0.60') + lookup_hash = _get_question_hash(converse_params.question) + logger.info(f'[SemanticCache] 开始查找 | chat_id={converse_params.chat_id} | question={converse_params.question} | hash={lookup_hash} | threshold=0.60') cache_result = await lookup_question( converse_params.chat_id, converse_params.question, @@ -220,6 +229,8 @@ async def converse_with_chat_assistant( if converse_params.stream: # 注意:存储时使用清理后的问题(与查找时保持一致) cache_question = cleaned_question if style_removed else converse_params.question + store_hash = _get_question_hash(cache_question) + logger.info(f'[RAG_CACHE] 准备存储 | chat_id={converse_params.chat_id} | question={cache_question} | hash={store_hash}') # 创建缓存存储回调函数 async def make_cache_store(chat_id: str, question: str):