Download evaluation/src/odu_eval/cache.py from qc316/odubench: direct link, hf CLI and curl.
- Browser
- Download file 1.84 kB
-
https://huggingface.co/datasets/qc316/odubench/resolve/main/evaluation/src/odu_eval/cache.py
- Command line
-
hf download hf://datasets/qc316/odubench/evaluation/src/odu_eval/cache.py
-
curl -L -o cache.py https://huggingface.co/datasets/qc316/odubench/resolve/main/evaluation/src/odu_eval/cache.py
1.84 kB
| """Replayable raw judge/repair answers, isolated by semantic and connection identity.""" | |
| from __future__ import annotations | |
| import json | |
| import threading | |
| from pathlib import Path | |
| from .io import digest, write_json | |
| class CachedCall: | |
| def __init__(self, root: Path, service, *, offline=False, identity=None): | |
| self.root, self.service, self.offline = root, service, offline | |
| self.identity = identity or service.identity() | |
| self.hits = self.misses = 0 | |
| self.errors = [] | |
| self.lock = threading.Lock() | |
| self.request_locks = {} | |
| def __call__(self, system_prompt: str, user_message: str, **ignored) -> str: | |
| request_hash = digest({"identity": self.identity, "system": system_prompt, "user": user_message}) | |
| path = self.root / (request_hash + ".json") | |
| with self.lock: | |
| request_lock = self.request_locks.setdefault(request_hash, threading.Lock()) | |
| with request_lock: | |
| if path.is_file(): | |
| saved = json.loads(path.read_text(encoding="utf-8")) | |
| if saved.get("request_hash") != request_hash or not isinstance(saved.get("response"), str): | |
| raise RuntimeError("Invalid cached response") | |
| self.hits += 1 | |
| return saved["response"] | |
| self.misses += 1 | |
| if self.offline: | |
| self.errors.append("cache_miss") | |
| raise RuntimeError("Offline cache miss; no request was sent") | |
| try: | |
| answer = self.service.call_text(system_prompt=system_prompt, user_message=user_message) | |
| except Exception: | |
| self.errors.append("api_error") | |
| raise | |
| write_json(path, {"request_hash": request_hash, "identity": self.identity, "response": answer}) | |
| return answer | |