File size: 4,192 Bytes
c2446d5 | 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 | """Per-task λ΅λ³ μΊμ.
μ±μ μλ²μ λ΅μ ν λ² μ μ‘νλλΌλ μ μ νμΈΒ·λλ²κΉ
μ μν΄ λ€μ λλ €μΌ ν λκ°
μλ€. ν λ¬Έμ λ§λ€ LLM νΈμΆμ΄ λΉμΈλ―λ‘ task_id λ¨μλ‘ λ΅μ λμ€ν¬μ 보κ΄ν΄
μ¬μ€ν μ μΊμλ λ¬Έμ λ μ€ν΅νλ€. ν λ¬Έμ μμ μ£½μΌλ©΄ μ μ²΄κ° λ©μΆλ λΆμ λ¬Έμ λ
ν¨κ» μν β λ€μ μ€ν λ κ·Έ λ¬Έμ λ§ λ€μ νλ©΄ λλ€.
μ μ₯ μμΉ: .cache/answers.json (μ΄λ―Έ λλ ν°λ¦¬ μ‘΄μ¬).
νμ: {task_id: {"question": str, "answer": str}}
μμμ μ°κΈ°: μμ νμΌ β os.replace λ‘ λμμ°κΈ°/ν¬λμ μμ .
μΊμ 무ν¨νλ νμΌ μ§μ μμ (rm .cache/answers.json) λλ clear_cache() μ¬μ©.
"""
import json
import os
import tempfile
from pathlib import Path
_CACHE_PATH = Path(".cache") / "answers.json"
def load_cache() -> dict:
"""λμ€ν¬μμ μΊμ λ‘λ. νμΌ μκ±°λ κΉ¨μ§λ©΄ λΉ dict."""
if not _CACHE_PATH.exists():
return {}
try:
return json.loads(_CACHE_PATH.read_text(encoding="utf-8"))
except Exception as e:
print(f"Warning: cache load failed ({e}); starting empty.")
return {}
def save_answer(task_id: str, question: str, answer: str) -> None:
"""task_id λ΅μ μΊμμ μΆκ°νκ³ μμμ μΌλ‘ μ μ₯.
AGENT_ERROR κ²°κ³Όλ μΊμ μ ν¨ β μ¬μ€ν μ λ€μ μλν΄μΌ νλ νλͺ©μ΄λΌ."""
if not task_id or is_retryable_answer(answer):
return
cache = load_cache()
cache[task_id] = {"question": question, "answer": answer}
_CACHE_PATH.parent.mkdir(parents=True, exist_ok=True)
# κ°μ λλ ν°λ¦¬μ μμ νμΌ β os.replace λ‘ OS-level atomic rename.
fd, tmp = tempfile.mkstemp(prefix="answers.", suffix=".tmp", dir=str(_CACHE_PATH.parent))
try:
with os.fdopen(fd, "w", encoding="utf-8") as f:
json.dump(cache, f, ensure_ascii=False, indent=2)
os.replace(tmp, _CACHE_PATH)
except Exception:
# μ€ν¨ μ μμ νμΌ μ 리 ν μ¬λ°μ.
try:
os.unlink(tmp)
except OSError:
pass
raise
def get_cached_answer(task_id: str, cache: dict | None = None) -> str | None:
"""μΊμμμ task_idμ λ΅μ κΊΌλ΄κ±°λ None.
cache μΈμλ₯Ό μ£Όλ©΄ λ§€λ² λμ€ν¬ μ¬λ‘λ© μ ν¨(루νμμ μ μ©)."""
if cache is None:
cache = load_cache()
entry = cache.get(task_id)
if entry and isinstance(entry, dict):
return entry.get("answer")
return None
def is_retryable_answer(answer: str | None) -> bool:
"""λ€μ μ€νμμ λ€μ μλν΄μΌ ν λ΅λ³μΈμ§ νλ³."""
if answer is None:
return True
a = str(answer).strip()
if not a:
return True
upper = a.upper()
return (
upper == "UNKNOWN"
or upper == "UNK"
or upper.startswith("AGENT_ERROR:")
or upper.startswith("AGENT ERROR:")
or "CANNOT ANSWER" in upper
or "NO FINAL ANSWER" in upper
)
def clear_cache() -> None:
"""μλ νΈμΆμ©. μλ νΈμΆ μ ν¨."""
if _CACHE_PATH.exists():
_CACHE_PATH.unlink()
def invalidate_tasks(task_ids) -> int:
"""μ£Όμ΄μ§ task_id λͺ©λ‘λ§ μΊμμμ μ κ±°νκ³ μμμ μΌλ‘ μ μ₯.
κ°μ λ¨κ³λ§λ€ "μ€λ΅μ΄μλ task_idλ§ μ¬μλ" νκΈ° μν ν¬νΌ.
μ λ΅μΌλ‘ μΊμλ νλͺ©μ λ³΄μ‘΄ν΄ ν ν°μ μλΌλ©΄μ, λ³κ²½ ν¨κ³Όλ§ κΉ¨λμ΄ μΈ‘μ .
Returns:
μ€μ λ‘ μ κ±°λ νλͺ© μ.
"""
cache = load_cache()
removed = 0
for tid in task_ids:
if tid in cache:
del cache[tid]
removed += 1
if removed == 0:
return 0
_CACHE_PATH.parent.mkdir(parents=True, exist_ok=True)
fd, tmp = tempfile.mkstemp(prefix="answers.", suffix=".tmp", dir=str(_CACHE_PATH.parent))
try:
with os.fdopen(fd, "w", encoding="utf-8") as f:
json.dump(cache, f, ensure_ascii=False, indent=2)
os.replace(tmp, _CACHE_PATH)
except Exception:
try:
os.unlink(tmp)
except OSError:
pass
raise
return removed
|