统一 hash 函数

This commit is contained in:
Tian jianyong 2025-12-24 19:53:44 +08:00
parent e1b3c0cb65
commit 4bf037ade5
2 changed files with 14 additions and 20 deletions

View File

@ -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):

View File

@ -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()