版本:v2.1.0
新增:SafeMemory 安全接口、来源追踪、边界检查
from memorycoreclaw import Memory
# 基础接口
mem = Memory(db_path="memory.db")
# 安全接口(推荐)
from memorycoreclaw import SafeMemory
safe_mem = SafeMemory(db_path="memory.db")from memorycoreclaw import Memory
mem = Memory(db_path=None, session_id=None)from memorycoreclaw import SafeMemory
mem = SafeMemory(db_path="memory.db")安全特性:
- 连接管理(自动关闭)
- 事务保护(自动提交/回滚)
- 边界检查(自动修正无效参数)
- 核心记忆保护(防误删)
- 来源标记(追踪记忆来源)
Store a fact in memory.
mem.remember(
content: str,
importance: float = 0.5,
category: str = None,
emotion: str = None,
tags: List[str] = None,
# v2.1.0 新增
source: str = "user", # user/llm/document/system
source_confidence: float = 1.0
) -> intReturns: Fact ID
Example:
# 基础用法
fid = mem.remember("Python 3.11 released", importance=0.7)
# v2.1.0: 带来源标记
fid = mem.remember(
"用户偏好简洁回复",
importance=0.85,
category="preference",
source="user",
source_confidence=1.0
)Search and retrieve memories.
mem.recall(
query: str,
limit: int = 10,
min_importance: float = 0.0
) -> List[Dict]v2.1.0 边界检查:
limit < 0→ 自动修正为10limit is None→ 自动修正为10query is None→ 自动修正为""
Returns: List of matching memories
Example:
results = mem.recall("Python", limit=5)
for r in results:
print(r['content'], r['importance'], r.get('source'))Store a lesson learned.
mem.learn(
action: str,
context: str = None,
outcome: str = "neutral", # positive, negative, neutral
insight: str = None,
importance: float = 0.7
) -> intReturns: Experience ID
Example:
eid = mem.learn(
action="Skipped backup before update",
context="Production server",
outcome="negative",
insight="Always backup before updates",
importance=0.9
)Delete a memory with protection.
mem.delete(
memory_id: int,
force: bool = False
) -> Dict核心记忆保护:
- 重要性 ≥ 0.9 的记忆需要
force=True才能删除 - 返回警告信息而不是直接删除
Returns: Dict with success, message, and optionally warning
Example:
# 普通记忆可直接删除
result = mem.delete(123)
# 核心记忆需要确认
result = mem.delete(456) # 返回 warning
# {'success': False, 'warning': 'Core memory requires force=True'}
result = mem.delete(456, force=True) # 强制删除Check database health.
mem.health_check() -> DictReturns:
{
'status': 'healthy' | 'warning' | 'error',
'stats': {
'facts': int,
'experiences': int,
'relations': int,
'orphaned_relations': int
},
'issues': List[str]
}Retrieve all lessons.
mem.get_lessons(limit: int = 100) -> List[Dict]Delete a memory by ID.
mem.delete(memory_id: int, memory_type: str = 'fact') -> boolCreate a relation between entities.
mem.relate(
from_entity: str,
relation_type: str,
to_entity: str,
evidence: str = None
) -> intExample:
mem.relate("Alice", "works_at", "TechCorp")
mem.relate("Alice", "knows", "Bob")Get all relations for an entity.
mem.get_relations(entity: str) -> List[Dict]Returns:
[
{'from': 'Alice', 'relation': 'works_at', 'to': 'TechCorp'},
{'from': 'Alice', 'relation': 'knows', 'to': 'Bob'}
]Get association network starting from an entity.
mem.associate(
entity: str,
depth: int = 2
) -> DictReturns:
{
'center': 'Alice',
'associations': [
{'from': 'Alice', 'relation': 'works_at', 'to': 'TechCorp', 'level': 1},
{'from': 'Alice', 'relation': 'knows', 'to': 'Bob', 'level': 1},
{'from': 'Bob', 'relation': 'works_at', 'to': 'StartupInc', 'level': 2}
]
}Infer relation type between entities.
mem.infer_relation(
from_entity: str,
to_entity: str
) -> Tuple[str, float]Returns: (relation_type, confidence)
from memorycoreclaw import Context
ctx = Context(
location: str = None,
people: List[str] = None,
emotion: str = None,
activity: str = None,
channel: str = None
)Bind a memory to a context.
mem.bind_context(
memory_type: str, # 'fact' or 'experience'
memory_id: int,
context: Context
) -> intRecall memories matching a context.
mem.recall_by_context(
location: str = None,
people: List[str] = None,
emotion: str = None,
activity: str = None
) -> List[Dict]Example:
results = mem.recall_by_context(people=["Alice"])Store temporary data.
mem.hold(
key: str,
value: Any,
priority: float = 0.5,
ttl_seconds: int = None
)Example:
mem.hold("task", "Processing", priority=0.9, ttl_seconds=3600)Get temporary data.
mem.retrieve(key: str) -> AnyClear all temporary data.
mem.clear_working_memory()Get current strength of a memory.
mem.get_memory_strength(
memory_type: str,
memory_id: int
) -> DictReturns:
{
'initial_strength': 0.7,
'current_strength': 0.65,
'last_accessed': '2024-01-15T10:30:00',
'access_count': 3
}Calculate retention rate.
mem.calculate_retention(
days: float,
strength: float = 0.7
) -> floatReturns: Retention ratio (0-1)
Get system statistics.
mem.get_stats() -> DictReturns:
{
'facts': 48,
'experiences': 21,
'relations': 26,
'entities': 15,
'working_memory': {'used': 3, 'capacity': 9}
}Export all data to JSON.
mem.export_json() -> DictGenerate knowledge graph HTML.
mem.visualize(output_path: str = None) -> strReturns: HTML file path
from memorycoreclaw import MemoryLayer
MemoryLayer.CORE # importance >= 0.9
MemoryLayer.IMPORTANT # 0.7 <= importance < 0.9
MemoryLayer.NORMAL # 0.5 <= importance < 0.7
MemoryLayer.MINOR # importance < 0.5from memorycoreclaw import STANDARD_RELATIONS
# 28 standard relation types
['works_at', 'works_in', 'manages', 'collaborates_with',
'knows', 'friend_of', 'uses', 'depends_on', ...]from memorycoreclaw import MemoryError
try:
mem.remember("")
except MemoryError as e:
print(f"Error: {e}")