GodsDevProject's picture
Upload 20 files
5830944 verified
raw
history blame
1.24 kB
import time
from typing import Dict, Any, List
from core.faiss_vector import FaissIndex
_TTL = 300 # seconds
_cache: Dict[str, Any] = {}
_faiss = None
def _now():
return int(time.time())
def _get_index():
global _faiss
if _faiss is None:
_faiss = FaissIndex()
return _faiss
def cache_get(key):
v = _cache.get(key)
if not v:
return None
ts, data = v
if _now() - ts > _TTL:
_cache.pop(key, None)
return None
return data
def cache_set(key, data: List[dict]):
_cache[key] = (_now(), data)
# add snippets to FAISS for local semantic recall
texts = [d.get("snippet","") for d in data if d.get("snippet")]
if texts:
try:
_get_index().add(texts)
except Exception:
pass
def dedupe(results: List[dict]) -> List[dict]:
seen = set()
out = []
for r in results:
h = hash((r.get("source"), r.get("url"), r.get("snippet")))
if h not in seen:
seen.add(h)
out.append(r)
return out
def source_counts(results: List[dict]) -> Dict[str,int]:
counts = {}
for r in results:
s = r.get("source","Unknown")
counts[s] = counts.get(s, 0) + 1
return counts