Janiecc/chat-memory-refinery
关于
一个隐私优先的聊天记忆提炼工具,将 ChatGPT、Claude 等聊天导出转换为可审计、结构化的长期记忆候选。A privacy-first toolkit that turns chat exports from ChatGPT, Claude, and other platforms into auditable, structured long-term memory candidates.
安全与隐私实测星标增长
- 24小时
- +1
- 7天
- 正在收集历史
- 15天
- 正在收集历史
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文chat-memory-refinery
一个隐私优先、离线优先的参考工具,用来把聊天导出整理成可审计、结构化的长期记忆候选。
当前内置解析器支持官方 ChatGPT 对话 JSON / ZIP 导出。整体按两段式 Agent 流水线设计:前面可以插入一个可选的 Router / Classifier Agent,先判断哪些聊天值得进入记忆提炼、应该走哪类处理;后面由 Extractor Agent(提取 Agent) 真正读取内容并产出结构化长期记忆候选。Extractor 和存储端(Sink)都有明确可替换接口;Router 位置也被保留为可扩展阶段。后续可以继续适配 Claude 等其他聊天平台、模型服务和记忆后端,但这些平台目前尚未内置支持。
中文说明
它能做什么
- 解析 ChatGPT 导出中当前有效的对话分支。
- 为每条候选保留稳定的来源引用。
- 可选地先交给 Router / Classifier Agent 做低成本筛选、分类和路由;小规模数据也可以直接跳过这一层。
- 再交给 Extractor Agent / 模型提取模块,由它真正读取内容并产出一个或多个结构化长期记忆候选。
- 用本地校验层检查原文证据片段、字段类型和审核状态。
- 输出 tentative / pending-review 状态的记忆候选,并默认写入离线 JSONL。
这个参考实现不会把模型输出直接当成已确认事实,默认不发起网络请求,也不会自动把候选写入长期记忆。
架构
聊天导出
↓
本地解析器
↓
来源任务 + provenance
↓
[可选] Router / Classifier Agent
↓
Extractor Agent / MemoryExtractorProvider
↓
本地证据与字段校验
↓
Memory Candidate
↓
MemorySink
├─ 本地 JSONL
└─ 你自己的后端适配器
这里故意留了 两个 Agent 插槽:
- Router / Classifier Agent:负责先筛选、分类和路由,例如判断一段聊天有没有长期记忆价值、是否值得交给下一层处理。它适合用更便宜、更快的模型,也可以在数据量小时完全跳过。
- Extractor Agent:负责真正读取通过筛选的内容,把它提炼成一个或多个结构化 Memory Candidate,并附上可核对的原文证据。
当前公开基线只实现了最小离线路径,默认直接从 Parser 进入 Extractor;Router / Classifier 这一层是架构上保留的可选扩展点,当前没有内置具体实现。
快速开始
要求:Python 3.10+。运行时只使用 Python 标准库。
python3 -m unittest discover -s tests -p "test_public_kit.py"
python3 -m public_kit \
--input public_kit/examples/synthetic-export.json
python3 -m public_kit \
--input public_kit/examples/synthetic-export.json \
--config public_kit/config.example.json \
--mock \
--output /tmp/memory-candidates-example.jsonl
默认模式是 dry run:只统计来源任务,不写出候选。
使用 --mock 时,会调用确定性的本地 mock 提取器并导出待审核候选,全程不发起网络请求。
处理真实聊天导出时,请把输入文件和生成的 JSONL 放在 Git 仓库之外。默认 JSONL Sink 会拒绝把候选写进 Git checkout,并以 0600 权限创建新文件,也不会静默覆盖已有导出。
候选数据结构
Schema 位于:
public_kit/schema/memory-candidate.schema.json
候选会保留:
- 稳定的
candidate_id title和body- 可选的 project / context / variant
- type 与 revision relation
- 原文中的精确
evidence_quote - conversation / message / node 来源引用
- 可选 event time 与 confidence
- review status
所有提取器输出都会被强制设为:
confirmation = tentative
status = pending_review
Candidate ID 用来辅助幂等,不等同于语义去重:同一个稳定来源、同一份规范化候选输入会得到同一个 ID;同一条来源消息拆出的多个原子候选则会得到不同 ID。
两类可替换 Agent
这个流水线区分两类职责,不把所有判断都塞进一个模型里:
1. Router / Classifier Agent(可选)
它负责“先决定要不要继续处理”:对聊天片段做筛选、分类和路由,把明显不值得进入长期记忆提炼的内容提前挡掉。这样大规模聊天导出时,可以让更便宜、更快的 Agent 先做粗筛,把更昂贵的提取调用留给真正值得处理的内容。
当前 public baseline 没有内置 Router 实现,因此默认直接跳过这一层;这里是为后续 Provider / Agent 适配预留的扩展位置。
2. Extractor Agent
Extractor 不是普通文本处理函数,而是记忆流水线里真正负责“提炼”的 AI Agent / 模型模块:它读取一条聊天来源任务,判断其中具体有哪些值得长期保留的信息,并返回一个或多个结构化 Memory Candidate。之后再由本地校验层检查 evidence quote、字段类型和审核状态。
代码里的 MemoryExtractorProvider 就是这个 Agent 的统一接口。你可以自己实现它,也可以向 OpenAICompatibleExtractor 注入兼容 client;其他模型或 Agent 框架也可以通过适配器接入,而不用改候选数据契约和 Sink。
当前仓库只内置离线 MockExtractorProvider 作为演示,因此 clone 下来不会自动调用任何真实 AI。核心代码不携带 API 凭据、不默认选择任何付费 Provider,也不会因为一份配置文件就自动开启网络访问。生产环境应自行补充明确的网络授权、敏感数据处理、预算限制、重试与日志策略。
可替换存储端
MemorySink 定义:
ensure_project(name)
create_memory(candidate)
update_memory(candidate)
JsonlSink 是默认离线实现。你可以把同一套接口映射到自己认证过的 API、数据库或其他记忆后端。
安全边界
ZIP 输入会限制:
- archive entry 数量
- 单个 export member 的解压后大小
- export 总 payload 大小
- 可疑压缩比
解析器不会把 ZIP 成员直接解压到磁盘。
以下情况会 fail closed:
- 来源 ID 冲突
- 候选字段类型非法
- 缺失或伪造 evidence quote
- 重复 candidate ID
- 同一 source ID 对应不同内容
这是一个紧凑的参考实现,不是完整生产级记忆系统。当前刻意不包含:
- 多模态导出解析
- Provider 预算记账
- 语义去重
- checkpoint / resume
- 生产级 routing policy
平台支持
- ChatGPT 导出解析:已支持
- Claude 等其他聊天平台:接口可扩展,尚未内置实现
- Router / Classifier Agent:架构可扩展,当前未内置实现
- Extractor Agent / Provider:可替换
- 记忆后端:可替换
来源与致谢
本仓库代码为独立实现。设计和导出格式层面的参考包括:
- AI Chat Memory Migrator — MIT
- Serein — MIT
详细来源说明见:
CODE_PROVENANCE.mdTHIRD_PARTY_NOTICES.md
License
MIT,见 LICENSE。
English
A privacy-first, offline-first reference toolkit for turning chat exports into auditable, structured memory candidates.
The current reference parser supports official ChatGPT conversation JSON/ZIP exports. The architecture reserves two agent stages: an optional Router / Classifier Agent for low-cost filtering and routing, followed by an Extractor Agent that turns selected chat content into structured long-term memory candidates. The extractor and sink use explicit pluggable interfaces, while the router stage is reserved as an optional extension point. Other chat exporters, model providers, agent frameworks, and memory backends can be added without rewriting the core contract.
What it does
- Parses the active branch of a ChatGPT conversation export.
- Keeps stable source references for every candidate task.
- Optionally passes source tasks through a Router / Classifier Agent for low-cost filtering, classification, and routing.
- Passes selected tasks to an Extractor Agent / model module that produces one or more structured long-term memory candidates.
- Validates exact evidence quotations, field types, and review state locally.
- Emits tentative / pending-review candidates and writes to offline JSONL by default.
The reference implementation does not treat model output as a verified fact, does not make network requests by default, and does not auto-admit candidates into long-term memory.
Architecture
chat export
↓
local parser
↓
source tasks + provenance
↓
[optional] Router / Classifier Agent
↓
Extractor Agent / MemoryExtractorProvider
↓
local evidence + field validation
↓
Memory Candidate
↓
MemorySink
├─ local JSONL
└─ your own backend adapter
The design deliberately leaves two agent slots:
- Router / Classifier Agent: filters, classifies, and routes source tasks before extraction. It can use a cheaper/faster model, and small workloads may skip it entirely.
- Extractor Agent: reads selected content and produces one or more structured Memory Candidates with source-grounded evidence.
The current public baseline implements the minimal offline path and goes directly from Parser to Extractor. The Router / Classifier stage is an architectural extension point and has no bundled implementation yet.
Quick start
Requirements: Python 3.10+. Runtime dependencies: Python standard library only.
python3 -m unittest discover -s tests -p "test_public_kit.py"
python3 -m public_kit \
--input public_kit/examples/synthetic-export.json
python3 -m public_kit \
--input public_kit/examples/synthetic-export.json \
--config public_kit/config.example.json \
--mock \
--output /tmp/memory-candidates-example.jsonl
Default mode is a dry run: it counts source tasks and writes nothing.
--mock uses a deterministic local echo extractor and exports review
candidates. No network request occurs.
For real exports, keep input files and generated JSONL outside Git
repositories. The JSONL sink refuses output paths inside a Git checkout,
creates files with mode 0600, and never silently overwrites an existing
export.
Candidate contract
See public_kit/schema/memory-candidate.schema.json.
Candidates preserve stable candidate identity, source references, exact evidence quotes, project/namespace metadata, type, optional context/variant/event time, revision relation, confidence, and review status.
All extractor output is forced to:
confirmation = tentative
status = pending_review
Candidate IDs are an idempotency aid, not semantic deduplication: identical normalized candidate input from the same stable source reproduces the same ID, while multiple atomic candidates from one source receive distinct IDs.
Two replaceable agent roles
The pipeline separates routing from extraction instead of forcing one model to do everything.
1. Router / Classifier Agent (optional)
This stage decides whether a source task should continue through the memory pipeline, and may classify or route it before extraction. On large exports, a cheaper/faster agent can reject low-value material early so stronger extraction calls are reserved for content worth processing.
The current public baseline does not bundle a Router implementation; it skips this stage by default and keeps it as an extension point for future providers or agent frameworks.
2. Extractor Agent
The extractor is not just a text-processing helper. It is the AI agent / model component that performs the actual memory refinement: it reads one source task, identifies durable information worth keeping, and returns one or more structured Memory Candidates. A local validation layer then checks evidence quotes, field types, and review state.
MemoryExtractorProvider is the common interface for that agent. Implement it
directly, inject a compatible client into OpenAICompatibleExtractor, or add an
adapter for another model provider or agent framework without changing the
candidate contract or sink.
Only the offline MockExtractorProvider is bundled for demonstration, so a
fresh clone never calls a real AI service automatically. The core does not ship
API credentials, does not select a paid provider, and does not enable network
access by configuration alone. Production integrations should add their own
explicit authorization, sensitive-data handling, budget limits, retries, and
logging policy.
Replaceable sink
MemorySink defines:
ensure_project(name)
create_memory(candidate)
update_memory(candidate)
JsonlSink is the default offline implementation. A backend adapter can map
the same contract to an authenticated API or database.
Safety boundaries
ZIP input is bounded by archive entry count, per-export-member uncompressed size, total export payload size, and suspicious compression ratio. The parser does not extract archive members to disk.
Malformed source IDs, invalid candidate fields, missing evidence quotations, duplicate candidate IDs, and conflicting reused source IDs fail closed.
This is a compact reference implementation, not a complete production memory system. It intentionally does not include multimodal export parsing, provider budget accounting, semantic deduplication, checkpointing, or a production routing policy.
Platform support
- ChatGPT export parser: included
- Other chat platforms: adapter-friendly, but not implemented yet
- Router / classifier agents: extensible; no bundled implementation yet
- Extractor agent / providers: pluggable
- Memory backends: pluggable
Provenance and acknowledgements
The implementation is independently written. Design and export-format ideas were informed by:
- AI Chat Memory Migrator — MIT
- Serein — MIT
See CODE_PROVENANCE.md and THIRD_PARTY_NOTICES.md for details.
License
MIT. See LICENSE.
探索相关项目
加载中…