Spaces:
Running on Zero
Running on Zero
Download controlplane/graph/nodes/semantic_cache.py from Pawan2605/controlplane-ai: direct link, hf CLI and curl.
- Browser
- Download file 1.41 kB
-
https://huggingface.co/spaces/Pawan2605/controlplane-ai/resolve/main/controlplane/graph/nodes/semantic_cache.py
- Command line
-
hf download hf://spaces/Pawan2605/controlplane-ai/controlplane/graph/nodes/semantic_cache.py
-
curl -L -o semantic_cache.py https://huggingface.co/spaces/Pawan2605/controlplane-ai/resolve/main/controlplane/graph/nodes/semantic_cache.py
1.41 kB
| """Semantic cache node. Runs after guardrails, before the RAG router.""" | |
| from __future__ import annotations | |
| from typing import Any, Dict | |
| from controlplane.cache import get_cache | |
| from controlplane.observability import traceable_node | |
| from controlplane.state import Stage | |
| def semantic_cache_node(state: Dict[str, Any]) -> Dict[str, Any]: | |
| query = state.get("guarded_query") or state.get("original_query", "") | |
| forced = (state.get("forced_kb") or "").strip() | |
| hit = get_cache(state.get("cache_ns")).lookup(query) # per-user namespace | |
| # Same pipeline for normal and manual mode. Only difference: if a KB was | |
| # forced, a cache entry that came from a DIFFERENT KB must not be served. | |
| if hit is not None and forced and (hit.meta or {}).get("selected_kb") not in (None, "", forced): | |
| hit = None | |
| if hit is None: | |
| return {"stage": Stage.CACHE, "stages_visited": [Stage.CACHE], "cache_hit": False} | |
| return { | |
| "stage": Stage.CACHE, | |
| "stages_visited": [Stage.CACHE], | |
| "cache_hit": True, | |
| "cache_similarity": round(hit.similarity, 4), | |
| "cached_answer": hit.answer, | |
| "cached_meta": hit.meta, | |
| "selected_kb": hit.meta.get("selected_kb"), | |
| "final_decision": "cache", | |
| } | |
| def route_after_cache(state: Dict[str, Any]) -> str: | |
| return "hit" if state.get("cache_hit") else "miss" | |