# Bionic Memory

> 仿生记忆系统：预测预警、经验管理、智能建议。Use when user mentions '记忆', '教训', '预警', '经验', '写日记', '查询记忆', '安全提醒', or needs intelligent memory management and risk prediction.

- Skill: `konglong87/bionic-memory` (Agent Skill, multi-file: 12 files)
- Install (CLI): `npx skillmds@latest add konglong87/bionic-memory`
- Raw SKILL.md: https://api.skillmd.com/api/skills/konglong87/bionic-memory/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: konglong87 (https://skillmd.com/u/konglong87)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/konglong87/bionic-memory

---


# 仿生记忆系统

## Overview

基于人类认知的智能记忆系统，能够记录经验教训、预测风险、主动预警。系统模拟人类的短期记忆、情节记忆、长期记忆和世界知识，通过多维度重要性评估和记忆衰减机制，实现智能化的知识管理和风险预防。

**核心特性**:
- ✅ 6层记忆架构（短期、情节、长期、世界知识、经验教训、流水账）
- ✅ 智能重要性评估（多维度评分 + 强化 + 衰减）
- ✅ 图关系引擎（实体关系 + 知觉联合启动）
- ✅ 预测预警系统（场景检测 + 教训匹配 + 智能建议）

## 使用方式

```bash
/bionic-memory write [内容]      # 写入记忆（自动提取、评估、存储）
/bionic-memory query [关键词]    # 查询记忆
/bionic-memory warning [场景]    # 生成预警
/bionic-memory stats             # 查看统计
```

## Parameters

- `action`: write | query | warning | stats
- `content`: 具体内容
- `options`: 可选参数（JSON格式）

## 核心功能

### 1. 记忆录入

**功能**: 自动提取实体、关系、情节，评估重要性，提取教训规则

**示例**:
```
用户: 今天下雨开车差点追尾，幸好及时刹车

系统处理:
1. AI提取: 实体（下雨、开车、追尾）、情节、关系
2. 评估: 重要性 -6.4 (负面, 中等)
3. 存储: 情节记忆 ep-xxx
4. 提取教训: IF weather=下雨 AND action=开车 THEN 减速慢行
5. 建立关系: 下雨-相关-开车
6. 更新标签: #下雨 #开车 #安全

输出: ✅ 记忆已保存，ID: ep-xxx，重要性: -6.4
```

### 2. 智能查询

**功能**: 多维度查询、关联查询、图遍历查找相关记忆

**示例**:
```
用户: /bionic-memory query 开车

输出:
找到 3 条相关记忆：
【1】2026-03-18 | 下雨天开车差点追尾 | 重要性: -6.4
【2】2025-12-20 | 弯道超车差点撞车 | 重要性: -5.8
【3】2024-08-10 | 疲劳驾驶撞护栏 | 重要性: -4.5

关联实体: 开车、下雨、弯道、疲劳驾驶
```

### 3. 预测预警

**功能**: 实时场景检测，匹配经验教训，生成智能建议

**示例**:
```
用户: 今天下雨，我要开车去接孩子

系统检测:
1. 场景检测: weather=下雨, action=开车
2. 匹配教训: 找到2条相关教训
3. 风险评估: P1级（安全风险）
4. 生成预警:

⚠️⚠️ 【雨天开车安全提醒】
风险等级: P1

建议:
1. 雨天路滑，减速慢行，增加跟车距离 (重要性: 7.5)
2. 车速控制在60km/h以下 (重要性: 6.5)

相关记忆:
- 2026-03-18: 下雨天开车差点追尾
- 2020-07-15: 雨天追尾事故
```

### 4. 记忆管理

**功能**: 修改重要性、查看统计、人工干预

**示例**:
```
用户: /bionic-memory stats

输出:
📊 记忆系统统计
━━━━━━━━━━━━━━━━━━
总记忆数: 245条
- 情节记忆: 198条
- 经验教训: 47条
- 世界知识: 86条

重要性分布:
- 高重要性（≥7）: 32条
- 中重要性（3-7）: 145条
- 低重要性（<3）: 68条
```

## 核心算法

### 重要性评估

```python
基础分 = intensity×0.3 + duration×0.3 + danger×0.3 + scarcity×0.1
重要性 = 基础分 × (positive ? +1 : -1)
```

### 记忆强化

```python
new_importance = MIN(10, |old| + trigger_count×0.3 + log(trigger_count+1)×0.2)
```

### 记忆衰减

```python
clarity = importance × e^(-λ×days_passed)
```

### 图遍历

使用SQLite递归CTE实现BFS遍历，查找关联实体

## 数据结构

### 情节记忆
```json
{
  "id": "ep-20260318-001",
  "timestamp": "2026-03-18",
  "location": "北京",
  "weather": "下雨",
  "event": "开车差点追尾",
  "importance": {
    "initial": -6.4,
    "current": -6.4,
    "nature": "negative"
  },
  "impact": {
    "intensity": 7,
    "duration": 5,
    "danger": 8,
    "scarcity": 4
  }
}
```

### 经验教训
```json
{
  "id": "lesson-001",
  "rule_condition": {
    "weather": "下雨",
    "action": "开车"
  },
  "rule_action": "雨天路滑，减速慢行",
  "importance": 7.5,
  "permanent": true
}
```

## 技术栈

- **存储**: SQLite (嵌入式)
- **图引擎**: SQLite + 递归CTE
- **评估器**: Python数学算法
- **AI提取**: Claude / 本地降级方案

## 文件结构

```
skills/bionic-memory/
├── SKILL.md                    # 本文件
├── engine/
│   ├── sqlite_storage.py       # 存储层
│   ├── sqlite_graph.py         # 图引擎
│   ├── evaluator.py            # 评估器
│   ├── extractor.py            # AI提取器
│   └── warning_engine.py       # 预警引擎
└── memory-data/
    ├── memory.db               # 数据库
    ├── config.yaml             # 配置
    └── world-knowledge/        # 知识库
```

## 使用示例

### Python API

```python
from skills.bionic-memory.engine.sqlite_storage import SQLiteStorage
from skills.bionic-memory.engine.evaluator import ImportanceEvaluator
from skills.bionic-memory.engine.extractor import AIExtractor
from skills.bionic-memory.engine.warning_engine import WarningEngine

# 初始化
storage = SQLiteStorage()
evaluator = ImportanceEvaluator()
extractor = AIExtractor()
warning_engine = WarningEngine()

# 1. 写入记忆
user_input = "今天下雨开车差点追尾"
extracted = extractor.ai_extract_all(user_input)
importance = evaluator.evaluate_importance(extracted['episode'])
extracted['episode']['importance'] = importance
episode_id = storage.save_episode(extracted['episode'])

# 2. 查询记忆
episodes = storage.query_episodes({'importance_min': 5})

# 3. 生成预警
scenario = warning_engine.detect_scenario("下雨天开车")
lessons = storage.match_lessons({'weather': '下雨', 'action': '开车'})
warning = warning_engine.generate_warning(scenario, lessons)

print(warning_engine.format_warning_message(warning))
```

## 注意事项

1. **首次使用**: 需要运行 `scripts/init_database.py` 初始化数据库
2. **配置文件**: 在 `memory-data/config.yaml` 中调整参数
3. **知识库**: 在 `memory-data/world-knowledge/` 中添加行业知识
4. **备份**: 定期备份 `memory-data/memory.db`

## 扩展开发

### 添加新行业知识

```yaml
# memory-data/world-knowledge/industries/medical.yaml
category: medical
industry: "医疗/卫生"
knowledge:
  - id: wk-medical-001
    title: "药物配伍禁忌"
    content: "..."
    keywords: ["药物", "配伍", "禁忌"]
    importance: 9.5
```

### 调整评估参数

```yaml
# memory-data/config.yaml
memory_system:
  importance:
    weights:
      intensity: 0.35    # 增加影响强度权重
      duration: 0.25
      danger: 0.30
      scarcity: 0.10
```

## 性能指标

- 查询响应: <100ms (1000条记录)
- 图遍历: <500ms (3层深度)
- 重要性评估: <10ms
- 内存占用: <50MB

## 版本信息

**当前版本**: v1.0.0-alpha
**更新日期**: 2026-03-18
**维护团队**: 恐龙创新部

## 已知限制

1. AI提取需要Claude环境，否则使用降级方案
2. 图遍历深度建议≤3层
3. 大量数据时需定期清理低清晰度记忆

## 路线图

**v1.1** (计划中):
- 完整AI提取集成
- 多用户支持
- 云端同步

**v2.0** (未来):
- 向量语义检索
- 多模态记忆（图片、音频）
- 协作记忆功能

---

**立即开始**: 运行 `python3 test_integration.py` 查看完整演示！
