Spaces:
Sleeping
Sleeping
File size: 8,575 Bytes
f0d100b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | """
Cortex RAG β Retrieval Cache (Redis, Phase 4)
What gets cached
βββββββββββββββββ
The output of the full retrieval pipeline β after RRF fusion and
cross-encoder reranking β is serialised and stored in Redis with a
configurable TTL (default 1 hour).
Cache key: SHA-256 of (query.lower().strip() + str(top_k))
This means the same query with different capitalisation or trailing
spaces hits the same cache entry, which is almost always correct for RAG.
What does NOT get cached
βββββββββββββββββββββββββ
CRAG evaluation and generation are NOT cached. The CRAG grade depends
on the current state of the knowledge base (which changes after ingestion),
and generation is fast enough (streaming) that caching it adds complexity
without meaningful latency savings.
Graceful degradation
βββββββββββββββββββββ
If Redis is unreachable on startup, the cache silently disables itself
and logs a warning. Every query falls through to the live retrieval
pipeline unchanged. No exceptions surface to the user.
This means you can develop without Redis running locally and only enable
it in production (Railway, Render) where Redis add-ons are available.
"""
from __future__ import annotations
import hashlib
import json
import logging
from typing import Optional
from retrieval.dense import RetrievedChunk
from retrieval.orchestrator import MultiStrategyRetriever, RetrievalResult
from retrieval.router import QueryIntent, RoutingDecision
logger = logging.getLogger(__name__)
def _make_cache_key(query: str, top_k: int) -> str:
raw = f"{query.lower().strip()}:{top_k}"
return "cortex:retrieval:" + hashlib.sha256(raw.encode()).hexdigest()[:24]
def _serialise_result(result: RetrievalResult) -> str:
"""JSON-serialise a RetrievalResult for Redis storage."""
return json.dumps({
"chunks": [
{
"chunk_id": c.chunk_id,
"doc_id": c.doc_id,
"source": c.source,
"title": c.title,
"text": c.text,
"parent_text": c.parent_text,
"chunk_index": c.chunk_index,
"score": c.score,
"retriever": c.retriever,
}
for c in result.chunks
],
"decision": {
"intent": result.decision.intent.value,
"strategies": result.decision.strategies,
"confidence": result.decision.confidence,
"reasoning": result.decision.reasoning,
},
"retriever_hits": result.retriever_hits,
})
def _deserialise_result(raw: str) -> RetrievalResult:
"""Reconstruct a RetrievalResult from its JSON representation."""
data = json.loads(raw)
chunks = [
RetrievedChunk(
chunk_id=c["chunk_id"],
doc_id=c["doc_id"],
source=c["source"],
title=c["title"],
text=c["text"],
parent_text=c["parent_text"],
chunk_index=c["chunk_index"],
score=c["score"],
retriever=c["retriever"],
)
for c in data["chunks"]
]
d = data["decision"]
decision = RoutingDecision(
intent=QueryIntent(d["intent"]),
strategies=d["strategies"],
confidence=d["confidence"],
reasoning=d["reasoning"],
)
return RetrievalResult(
chunks=chunks,
decision=decision,
retriever_hits=data.get("retriever_hits", {}),
)
class CachedRetriever:
"""
Drop-in wrapper around MultiStrategyRetriever that adds Redis caching.
Usage (replaces MultiStrategyRetriever in api/main.py):
retriever = CachedRetriever(MultiStrategyRetriever(...))
result = retriever.retrieve(query)
print(retriever.cache_stats()) # {"hits": 3, "misses": 7, "enabled": True}
"""
def __init__(
self,
inner: MultiStrategyRetriever,
ttl_seconds: Optional[int] = None,
) -> None:
self._inner = inner
self._redis = self._connect_redis()
self._ttl = ttl_seconds or self._default_ttl()
self._hits = 0
self._misses = 0
# ββ Public API (matches MultiStrategyRetriever interface) ββ
def retrieve(
self,
query: str,
top_k_candidates: Optional[int] = None,
final_top_k: Optional[int] = None,
) -> RetrievalResult:
"""
Retrieve with cache. Falls through to live retrieval on miss or error.
"""
from config import get_settings
cfg = get_settings()
k = final_top_k or cfg.final_top_k
key = _make_cache_key(query, k)
# ββ Cache lookup βββββββββββββββββββββββββββββββββββββββ
if self._redis:
try:
cached = self._redis.get(key)
if cached:
self._hits += 1
logger.debug("Cache HIT for query: %sβ¦", query[:40])
result = _deserialise_result(cached)
result.from_cache = True
return result
except Exception as exc:
logger.warning("Redis GET failed: %s β falling through.", exc)
# ββ Cache miss: live retrieval βββββββββββββββββββββββββ
self._misses += 1
logger.debug("Cache MISS for query: %sβ¦", query[:40])
result = self._inner.retrieve(query, top_k_candidates, final_top_k)
result.from_cache = False
# ββ Write to cache βββββββββββββββββββββββββββββββββββββ
if self._redis and not result.empty:
try:
self._redis.setex(key, self._ttl, _serialise_result(result))
except Exception as exc:
logger.warning("Redis SET failed: %s", exc)
return result
def invalidate(self, query: str, top_k: int) -> bool:
"""Manually invalidate a cache entry (e.g. after re-ingestion)."""
if not self._redis:
return False
try:
return bool(self._redis.delete(_make_cache_key(query, top_k)))
except Exception:
return False
def flush_all(self) -> int:
"""Delete all Cortex cache keys. Returns count deleted."""
if not self._redis:
return 0
try:
keys = self._redis.keys("cortex:retrieval:*")
if keys:
return self._redis.delete(*keys)
return 0
except Exception:
return 0
def cache_stats(self) -> dict:
total = self._hits + self._misses
return {
"enabled": self._redis is not None,
"hits": self._hits,
"misses": self._misses,
"hit_rate": round(self._hits / total, 3) if total else 0.0,
"ttl_s": self._ttl,
}
# ββ Pass-through for orchestrator methods ββββββββββββββββββ
def index_chunks(self, chunks: list) -> int:
return self._inner.index_chunks(chunks)
def build_graph(self, chunks: list) -> dict:
return self._inner.build_graph(chunks)
@property
def graph_builder(self):
return self._inner.graph_builder
# ββ Redis connection βββββββββββββββββββββββββββββββββββββββ
@staticmethod
def _connect_redis():
from config import get_settings
cfg = get_settings()
url = getattr(cfg, "redis_url", "redis://localhost:6379")
try:
import redis # type: ignore
client = redis.from_url(url, socket_connect_timeout=2, decode_responses=True)
client.ping()
logger.info("Redis cache connected at %s", url)
return client
except ImportError:
logger.info("redis-py not installed β cache disabled. pip install redis")
return None
except Exception as exc:
logger.warning("Redis unavailable (%s) β cache disabled.", exc)
return None
@staticmethod
def _default_ttl() -> int:
from config import get_settings
return getattr(get_settings(), "cache_ttl_seconds", 3600)
|