Instructions to use Falln87/clerk-memory with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use Falln87/clerk-memory with PEFT:
Task type is invalid.
- Notebooks
- Google Colab
- Kaggle
common.py: fix facts_of valid_only semantics
Browse files- clerk/common.py +12 -14
clerk/common.py
CHANGED
|
@@ -21,7 +21,7 @@ License: Apache-2.0
|
|
| 21 |
from __future__ import annotations
|
| 22 |
|
| 23 |
import json
|
| 24 |
-
from dataclasses import dataclass
|
| 25 |
from typing import Any, Dict, List, Optional, Tuple
|
| 26 |
|
| 27 |
LEDGER_BUDGET_DEFAULT = 24
|
|
@@ -85,7 +85,6 @@ def parse_ops(text: str) -> Tuple[List[Dict[str, Any]], str]:
|
|
| 85 |
"""
|
| 86 |
text = text.strip()
|
| 87 |
if text.startswith("```"):
|
| 88 |
-
# strip a markdown fence if the model adds one
|
| 89 |
text = text.strip("`")
|
| 90 |
if text.startswith("json"):
|
| 91 |
text = text[4:]
|
|
@@ -188,30 +187,29 @@ def salience(slot: Slot, now: int) -> float:
|
|
| 188 |
def enforce_budget(
|
| 189 |
ledger: List[Slot], ops: List[Dict[str, Any]], now: int
|
| 190 |
) -> Tuple[List[Slot], List[Dict[str, Any]]]:
|
| 191 |
-
"""
|
| 192 |
-
|
| 193 |
-
to the
|
| 194 |
ledger, _ = apply_ops(ledger, ops, now)
|
| 195 |
ops = list(ops)
|
| 196 |
occupied = [s for s in ledger if s.occupied]
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
(s for s in ledger if s.occupied), key=lambda s: salience(s, now)
|
| 201 |
-
)
|
| 202 |
ops.append({"op": "EVICT", "i": victim.id})
|
| 203 |
ledger[victim.id] = Slot(id=victim.id)
|
| 204 |
return ledger, ops
|
| 205 |
|
| 206 |
|
| 207 |
def facts_of(ledger: List[Slot], valid_only: bool = True) -> Dict[Tuple[str, str], str]:
|
| 208 |
-
"""Current fact map {(subject, predicate): object}.
|
|
|
|
|
|
|
|
|
|
| 209 |
out: Dict[Tuple[str, str], str] = {}
|
| 210 |
for s in ledger:
|
| 211 |
if s.occupied and (s.valid or not valid_only):
|
| 212 |
-
|
| 213 |
-
if s.valid:
|
| 214 |
-
out[key] = s.object
|
| 215 |
return out
|
| 216 |
|
| 217 |
|
|
|
|
| 21 |
from __future__ import annotations
|
| 22 |
|
| 23 |
import json
|
| 24 |
+
from dataclasses import dataclass
|
| 25 |
from typing import Any, Dict, List, Optional, Tuple
|
| 26 |
|
| 27 |
LEDGER_BUDGET_DEFAULT = 24
|
|
|
|
| 85 |
"""
|
| 86 |
text = text.strip()
|
| 87 |
if text.startswith("```"):
|
|
|
|
| 88 |
text = text.strip("`")
|
| 89 |
if text.startswith("json"):
|
| 90 |
text = text[4:]
|
|
|
|
| 187 |
def enforce_budget(
|
| 188 |
ledger: List[Slot], ops: List[Dict[str, Any]], now: int
|
| 189 |
) -> Tuple[List[Slot], List[Dict[str, Any]]]:
|
| 190 |
+
"""Apply ops, then evict lowest-salience occupied slots until the
|
| 191 |
+
occupied count fits the budget (== number of slots). Appends EVICT ops
|
| 192 |
+
to the program so the policy learns budget-aware behavior."""
|
| 193 |
ledger, _ = apply_ops(ledger, ops, now)
|
| 194 |
ops = list(ops)
|
| 195 |
occupied = [s for s in ledger if s.occupied]
|
| 196 |
+
for _ in range(max(0, len(occupied) - len(ledger))):
|
| 197 |
+
victim = min((s for s in ledger if s.occupied),
|
| 198 |
+
key=lambda s: salience(s, now))
|
|
|
|
|
|
|
| 199 |
ops.append({"op": "EVICT", "i": victim.id})
|
| 200 |
ledger[victim.id] = Slot(id=victim.id)
|
| 201 |
return ledger, ops
|
| 202 |
|
| 203 |
|
| 204 |
def facts_of(ledger: List[Slot], valid_only: bool = True) -> Dict[Tuple[str, str], str]:
|
| 205 |
+
"""Current fact map {(subject, predicate): object}.
|
| 206 |
+
|
| 207 |
+
valid_only=True keeps only active slots; False also returns tombstoned
|
| 208 |
+
slots (last-known value), which backs temporal questions."""
|
| 209 |
out: Dict[Tuple[str, str], str] = {}
|
| 210 |
for s in ledger:
|
| 211 |
if s.occupied and (s.valid or not valid_only):
|
| 212 |
+
out[(s.subject, s.predicate)] = s.object
|
|
|
|
|
|
|
| 213 |
return out
|
| 214 |
|
| 215 |
|