Loading
Loading
A ~25-line BaseMemory subclass. Confidence flags can be surfaced to the prompt; the BaseMemory interface drops them in transit.
from langchain.memory import BaseMemory
from atlaso import Memory
class AtlasoMemory(BaseMemory):
def __init__(self, user_id: str, key: str = "context"):
self.user = Memory().for_user(user_id)
self.key = key
@property
def memory_variables(self) -> list[str]:
return [self.key]
def load_memory_variables(self, inputs: dict) -> dict:
q = inputs.get("input", "")
hits = self.user.recall(q, limit=8)
confident = [h.content for h in hits if h.is_confident]
return {self.key: "\n".join(confident)}
def save_context(self, inputs: dict, outputs: dict) -> None:
self.user.add(inputs.get("input", ""))
def clear(self) -> None:
raise NotImplementedError("Atlaso deposits are immutable.")from langchain.prompts import PromptTemplate
def build_context(query: str, user) -> str:
hits = user.recall(query, limit=8)
lines = [f"VERDICT: {hits.explain()}"]
for h in hits:
marker = "WARN" if h.has_disagreement else ("OK" if h.is_confident else "?")
lines.append(f" {marker} {h.content}")
return "\n".join(lines)
prompt = PromptTemplate(
input_variables=["context", "input"],
template="Memory:\n{context}\n\nUser: {input}\nAssistant:",
)Was this page helpful?