Skip to content

yaab.memory

Long-term memory services and the MemoryManager.

yaab.memory

Long-term, cross-session memory with vector retrieval.

Retrieval uses the Rust-accelerated cosine/top-k from :mod:yaab._core (with a pure-Python fallback), so memory lookups stay cheap as the store grows. The embedder is pluggable: pass any Callable[[str], list[float]]. A tiny deterministic hashing embedder ships for offline use and tests.

MemoryRecord

Bases: BaseModel

A stored memory with its embedding and arbitrary metadata.

MemoryService

Bases: Protocol

Pluggable long-term memory backend.

InMemoryVectorMemory

A simple in-process vector store over :func:yaab._core.top_k.

MemoryExtractor

Distill durable memories from a session's messages via one LLM call.

Parameters

model: Any :class:~yaab.models.base.ModelProvider (or a model-name string, resolved lazily via yaab.models.resolve_model). Used once per :meth:extract to produce the JSON array of memory statements. roles: Which message roles to include in the transcript handed to the model. Defaults to user + assistant turns (system/tool noise is dropped). max_memories: Safety cap on the number of memories returned, so a misbehaving model cannot flood the store. None disables the cap.

extract async

extract(messages: list[Message]) -> list[str]

Return a list of distilled memory statements for messages.

Makes a single model call and tolerantly parses the response (markdown fences allowed). Non-string items and a non-list payload are dropped so a malformed response degrades to [] rather than raising — ingestion should never crash a run.

hashing_embedder

hashing_embedder(dim: int = 64) -> Embedder

A deterministic, dependency-free embedder for offline use and tests.

Hashes tokens into a fixed-width bag-of-words vector and L2-normalizes it. Good enough for wiring/tests; swap in a real embedding model for production.

default_embedder

default_embedder() -> Embedder

Pick a sensible default embedder.

Auto-upgrades to a real :class:LiteLLMEmbedder when litellm is installed and an embedding-provider key is in the environment (explicit opt-in by configuration); otherwise falls back to the deterministic hashing stub and logs a one-time warning that semantic recall will be weak. Construction is cheap and offline — the real embedder only calls the provider when used.

resolve_embedder

resolve_embedder(embedder: Embedder | str | None = None) -> Embedder

Normalize an embedder argument: None -> default, str -> a LiteLLM model-name shorthand, callable -> passthrough.