Spaces:
Build error
Build error
File size: 11,173 Bytes
eb1c19a 9c9bb30 eb1c19a 04a98a0 eb1c19a 04a98a0 9c9bb30 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 9c9bb30 eb1c19a 04a98a0 eb1c19a f6bfc13 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 9c9bb30 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 9c9bb30 eb1c19a 7de545c eb1c19a 9c9bb30 eb1c19a 9c9bb30 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a 04a98a0 eb1c19a f6bfc13 9c9bb30 | 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | """Headroom Memory - Simple, zero-config memory for AI applications.
Quick Start (No Docker Required):
from headroom.memory import Memory
# Create memory instance - works out of the box!
memory = Memory()
# Save memories
await memory.save("User prefers dark mode and uses Python", user_id="alice")
# Search memories
results = await memory.search("What programming language?", user_id="alice")
for r in results:
print(r.content, r.score)
Production Mode (with Docker):
# Start services: docker compose up -d qdrant neo4j
memory = Memory(backend="qdrant-neo4j")
# Same API, production-grade backends
await memory.save("User works at Netflix", user_id="alice")
Backends:
- "local" (default): SQLite + HNSW + InMemoryGraph. No setup required.
- "qdrant-neo4j": Qdrant + Neo4j. Requires Docker services.
Advanced Usage - LLM Wrapper:
from openai import OpenAI
from headroom.memory import with_memory
client = with_memory(OpenAI(), user_id="alice")
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "I prefer Python"}]
)
# Memory automatically extracted and stored!
Advanced Usage - Tool-based:
from openai import OpenAI
from headroom.memory import with_memory_tools, LocalBackend
client = with_memory_tools(
OpenAI(),
backend=LocalBackend(),
user_id="alice",
optimized=True, # LLM extracts facts/entities in ONE call
)
"""
# =============================================================================
# Configuration
# =============================================================================
# =============================================================================
# Graph adapters
# =============================================================================
from headroom.memory.adapters.graph import InMemoryGraphStore
# =============================================================================
# Backend implementations (lazy imports for optional dependencies)
# =============================================================================
# LocalBackend is always available (no optional dependencies)
from headroom.memory.backends.local import LocalBackend, LocalBackendConfig
# =============================================================================
# Memory Bridge (markdown <-> Headroom bidirectional sync)
# =============================================================================
from headroom.memory.bridge import ImportStats, MemoryBridge, SyncStats
from headroom.memory.bridge_config import BridgeConfig, MarkdownFormat
from headroom.memory.config import (
EmbedderBackend,
MemoryConfig,
StoreBackend,
TextBackend,
VectorBackend,
)
# =============================================================================
# Core orchestrator
# =============================================================================
from headroom.memory.core import HierarchicalMemory
# =============================================================================
# Simple API (recommended for most users)
# =============================================================================
from headroom.memory.easy import Memory, MemoryResult
# =============================================================================
# Factory
# =============================================================================
from headroom.memory.factory import create_memory_system
# =============================================================================
# Data models (internal)
# =============================================================================
from headroom.memory.models import Memory as MemoryModel
from headroom.memory.models import ScopeLevel
# =============================================================================
# Protocol interfaces (ports)
# =============================================================================
from headroom.memory.ports import (
# Core protocols
Embedder,
# Graph dataclasses
Entity,
# Graph protocol
GraphStore,
MemoryCache,
# Filter dataclasses
MemoryFilter,
# Memory search result
MemorySearchResult,
MemoryStore,
Relationship,
Subgraph,
TextFilter,
TextIndex,
# Search result dataclasses
TextSearchResult,
VectorFilter,
VectorIndex,
VectorSearchResult,
)
# =============================================================================
# Memory system orchestrator
# =============================================================================
from headroom.memory.system import MemoryBackend, MemorySystem
# =============================================================================
# Memory tools for LLM function calling
# =============================================================================
from headroom.memory.tools import (
MEMORY_TOOLS,
MEMORY_TOOLS_OPTIMIZED,
get_memory_tools,
get_memory_tools_optimized,
)
# =============================================================================
# Wrapper for LLM clients (main user-facing API)
# =============================================================================
from headroom.memory.wrapper import MemoryWrapper, with_memory
# =============================================================================
# Tool-based wrapper for LLM clients
# =============================================================================
from headroom.memory.wrapper_tools import MemoryToolsWrapper, with_memory_tools
# Lazy imports for optional backends to avoid ImportError if dependencies not installed
_Mem0Backend = None
_Mem0Config = None
_DirectMem0Adapter = None
_DirectMem0Config = None
def __getattr__(name: str) -> type:
"""Lazy import for optional backend components."""
global _Mem0Backend, _Mem0Config, _DirectMem0Adapter, _DirectMem0Config
if name == "Mem0Backend":
if _Mem0Backend is None:
from headroom.memory.backends.mem0 import Mem0Backend
_Mem0Backend = Mem0Backend
return _Mem0Backend
if name == "Mem0Config":
if _Mem0Config is None:
from headroom.memory.backends.mem0 import Mem0Config
_Mem0Config = Mem0Config
return _Mem0Config
if name == "DirectMem0Adapter":
if _DirectMem0Adapter is None:
from headroom.memory.backends.direct_mem0 import DirectMem0Adapter
_DirectMem0Adapter = DirectMem0Adapter
return _DirectMem0Adapter
if name == "DirectMem0Config":
if _DirectMem0Config is None:
from headroom.memory.backends.direct_mem0 import Mem0Config as DirectMem0Config
_DirectMem0Config = DirectMem0Config
return _DirectMem0Config
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
__all__ = [
# =========================================================================
# Simple API (recommended for most users)
# =========================================================================
"Memory", # Zero-config memory class
"MemoryResult", # Search result dataclass
# =========================================================================
# LLM Wrapper API
# =========================================================================
"with_memory",
"MemoryWrapper",
# Tool-based wrapper
"with_memory_tools",
"MemoryToolsWrapper",
# =========================================================================
# Core orchestrator
# =========================================================================
"HierarchicalMemory",
# =========================================================================
# Data models (internal)
# =========================================================================
"MemoryModel", # Internal memory model (renamed from Memory)
"ScopeLevel",
# =========================================================================
# Protocol interfaces (ports)
# =========================================================================
"MemoryStore",
"VectorIndex",
"TextIndex",
"Embedder",
"MemoryCache",
"GraphStore",
# =========================================================================
# Filter dataclasses
# =========================================================================
"MemoryFilter",
"VectorFilter",
"TextFilter",
# =========================================================================
# Search result dataclasses
# =========================================================================
"VectorSearchResult",
"TextSearchResult",
"MemorySearchResult",
# =========================================================================
# Graph dataclasses
# =========================================================================
"Entity",
"Relationship",
"Subgraph",
# =========================================================================
# Configuration
# =========================================================================
"MemoryConfig",
"StoreBackend",
"VectorBackend",
"TextBackend",
"EmbedderBackend",
# =========================================================================
# Factory
# =========================================================================
"create_memory_system",
# =========================================================================
# Memory tools for LLM function calling
# =========================================================================
"MEMORY_TOOLS",
"MEMORY_TOOLS_OPTIMIZED",
"get_memory_tools",
"get_memory_tools_optimized",
# =========================================================================
# Memory system orchestrator
# =========================================================================
"MemorySystem",
"MemoryBackend",
# =========================================================================
# Graph adapters
# =========================================================================
"InMemoryGraphStore",
# =========================================================================
# Backend implementations
# =========================================================================
# Local backend (always available)
"LocalBackend",
"LocalBackendConfig",
# Mem0 backend (optional dependencies - lazy loaded)
"Mem0Backend",
"Mem0Config",
# DirectMem0Adapter - optimized Mem0 adapter that bypasses internal LLM calls
# Use with optimized=True in with_memory_tools() for best performance
"DirectMem0Adapter",
"DirectMem0Config",
# =========================================================================
# Memory Bridge (markdown <-> Headroom bidirectional sync)
# =========================================================================
"MemoryBridge",
"BridgeConfig",
"MarkdownFormat",
"ImportStats",
"SyncStats",
]
|