init
This commit is contained in:
@@ -1,53 +1,28 @@
|
||||
import hashlib
|
||||
import json
|
||||
from typing import Any, Dict, Optional
|
||||
from typing import Any, Dict
|
||||
|
||||
from config import Config
|
||||
from services.cache import NoopCache, RedisCache
|
||||
from services.ragflow_client import RagflowClient, extract_table_name
|
||||
|
||||
|
||||
class TemplateMatcher:
|
||||
"""模板匹配器:RAGFlow + Redis 缓存"""
|
||||
"""模板匹配器:RAGFlow 检索"""
|
||||
|
||||
def __init__(self):
|
||||
self._ragflow = RagflowClient()
|
||||
self._cache = self._init_cache()
|
||||
cfg = Config.get_section("ragflow")
|
||||
self._cache_ttl = int(cfg.get("cache_ttl", 600))
|
||||
self._dataset_id = (cfg.get("table_retrieval_dataset_id") or "").strip()
|
||||
|
||||
def _init_cache(self):
|
||||
cfg = Config.get_section("redis")
|
||||
enabled = str(cfg.get("enabled", "false")).lower() in ("1", "true", "yes")
|
||||
if not enabled:
|
||||
return NoopCache()
|
||||
|
||||
url = cfg.get("url")
|
||||
db = int(cfg.get("db", 0))
|
||||
if not url:
|
||||
return NoopCache()
|
||||
|
||||
try:
|
||||
return RedisCache(url=url, db=db)
|
||||
except Exception:
|
||||
return NoopCache()
|
||||
|
||||
@staticmethod
|
||||
def _cache_key(text: str) -> str:
|
||||
return "ragflow:table:" + hashlib.sha256(text.encode("utf-8")).hexdigest()
|
||||
def _validate(self) -> None:
|
||||
if not self._dataset_id:
|
||||
raise RuntimeError("未配置 ragflow.table_retrieval_dataset_id,无法进行表名检索")
|
||||
|
||||
def match(self, normalized_text: str) -> Dict[str, Any]:
|
||||
"""返回匹配的表名与原始响应"""
|
||||
key = self._cache_key(normalized_text)
|
||||
cached = self._cache.get(key)
|
||||
if cached:
|
||||
return json.loads(cached)
|
||||
self._validate()
|
||||
try:
|
||||
response = self._ragflow.retrieve(normalized_text, top_k=3)
|
||||
response = self._ragflow.retrieve(normalized_text, top_k=3, dataset_id=self._dataset_id)
|
||||
except Exception as e:
|
||||
result = {"table_name": None, "raw": {"error": str(e)}}
|
||||
self._cache.set(key, json.dumps(result, ensure_ascii=False), self._cache_ttl)
|
||||
return result
|
||||
return {"table_name": None, "raw": {"error": str(e)}}
|
||||
|
||||
candidates = []
|
||||
data = response.get("data") if isinstance(response, dict) else None
|
||||
@@ -58,7 +33,15 @@ class TemplateMatcher:
|
||||
candidates.append(table_name)
|
||||
|
||||
matched = candidates[0] if candidates else None
|
||||
result = {"table_name": matched, "raw": response}
|
||||
return {"table_name": matched, "raw": response}
|
||||
|
||||
self._cache.set(key, json.dumps(result, ensure_ascii=False), self._cache_ttl)
|
||||
return result
|
||||
|
||||
_GLOBAL_TEMPLATE_MATCHER: TemplateMatcher | None = None
|
||||
|
||||
|
||||
def get_template_matcher() -> TemplateMatcher:
|
||||
"""获取全局 TemplateMatcher(单例)"""
|
||||
global _GLOBAL_TEMPLATE_MATCHER
|
||||
if _GLOBAL_TEMPLATE_MATCHER is None:
|
||||
_GLOBAL_TEMPLATE_MATCHER = TemplateMatcher()
|
||||
return _GLOBAL_TEMPLATE_MATCHER
|
||||
|
||||
Reference in New Issue
Block a user