From 4bf037ade54a97d68f259c2fe35b685a10c109ac Mon Sep 17 00:00:00 2001 From: Tian jianyong <11429339@qq.com> Date: Wed, 24 Dec 2025 19:53:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=20hash=20=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ragflow_controller.py | 14 ++++--------- .../utils/semantic_cache_service.py | 20 +++++++++---------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py b/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py index b562442..96afdea 100644 --- a/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py +++ b/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py @@ -22,13 +22,7 @@ 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.semantic_cache_service import get_semantic_cache_service, lookup_question, store_qa_pair, get_question_hash from utils.static_qa_service import get_static_qa_service @@ -36,7 +30,7 @@ async def _async_store_qa(chat_id: str, question: str, answer: str, redis) -> No """ 异步存储问答对到语义缓存 """ - store_hash = _get_question_hash(question) + 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) @@ -157,7 +151,7 @@ async def converse_with_chat_assistant( # ========== 2. RAG历史缓存查找 ========== logger.info(f'[SemanticCache] 准备执行RAG历史缓存查找 | redis={redis is not None} | chat_id={converse_params.chat_id}') if redis: - lookup_hash = _get_question_hash(converse_params.question) + 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, @@ -202,7 +196,7 @@ async def converse_with_chat_assistant( result = RAGFlowService.converse_with_chat_assistant_services(converse_params) cache_question = cleaned_question if style_removed else converse_params.question - store_hash = _get_question_hash(cache_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): diff --git a/ruoyi-fastapi-backend/utils/semantic_cache_service.py b/ruoyi-fastapi-backend/utils/semantic_cache_service.py index 0f28e2c..12637ce 100644 --- a/ruoyi-fastapi-backend/utils/semantic_cache_service.py +++ b/ruoyi-fastapi-backend/utils/semantic_cache_service.py @@ -87,14 +87,6 @@ class SemanticCacheService: """构建缓存键""" return f"{self.CACHE_PREFIX}:{chat_id}:{question_hash}" - def _hash_question(self, question: str) -> str: - """对问题进行哈希,生成唯一标识""" - # 标准化问题文本(去除多余空格、统一标点) - normalized = self._normalize_question(question) - hash_value = hashlib.md5(normalized.encode('utf-8')).hexdigest()[:16] - logger.info(f"[SemanticCache] Hash计算 | 原始={question} | 标准化={normalized} | hash={hash_value}") - return hash_value - def _normalize_question(self, question: str) -> str: """标准化问题文本""" match_service = get_match_service() @@ -134,7 +126,8 @@ class SemanticCacheService: try: # 1. 精确匹配 - question_hash = self._hash_question(question) + normalized = question.lower().strip() + question_hash = hashlib.md5(normalized.encode('utf-8')).hexdigest()[:16] exact_key = self._build_cache_key(chat_id, question_hash) logger.info(f"[SemanticCache] 精确查找 | chat_id={chat_id} | question={question} | hash={question_hash} | key={exact_key}") @@ -227,7 +220,8 @@ class SemanticCacheService: return False # 构建缓存条目 - question_hash = self._hash_question(question) + normalized = question.lower().strip() + question_hash = hashlib.md5(normalized.encode('utf-8')).hexdigest()[:16] cache_key = self._build_cache_key(chat_id, question_hash) logger.info(f"[SemanticCache] 存储缓存 | chat_id={chat_id} | question={question} | hash={question_hash} | key={cache_key}") @@ -435,6 +429,12 @@ async def store_qa_pair(chat_id: str, question: str, answer: str, redis_client=N return await service.store(chat_id, question, answer, redis_client) +def get_question_hash(question: str) -> str: + """计算问题的hash值(公共函数,确保存储和查找一致)""" + normalized = question.lower().strip() + return hashlib.md5(normalized.encode('utf-8')).hexdigest()[:16] + + async def clear_cache(chat_id: str = None, redis_client=None) -> bool: """清除缓存""" service = get_semantic_cache_service()