feat: RAG experience library — retrieve successful flows as few-shot#5
Merged
Conversation
把跑通的任务流程沉淀成经验库;新任务用「用户提示词」做混合检索
(dense 向量 + BM25 关键词 → RRF 融合 → 相关性闸门),把最相似的成功
经验作为 few-shot 注入 system_prompt,让 agent 少绕圈、用更少步数完成。
新增 app/experience/ 五件套:
- embedding.py: DashScope text-embedding-v4 + L2 归一化
- store.py: Recipe + RecipeStore(jsonl 唯一真相源 + os.replace 原子重写 +
重建 Faiss IndexFlatIP/BM25 + asyncio 锁 + numpy 余弦去重,同任务留步数少的)
- retriever.py: dense+BM25 → RRF → 相关性闸门 + 降级(dense 失败仅 BM25)
- recorder.py: 轨迹压缩转录 + distill prompt(步骤泛化+本例括注、踩坑对策走
独立 tips)+ 程序化补全 tools_used/step_count
- __init__.py: is_enabled/retrieve_recipes/distill_recipe/format_injection/store
接线:
- app/agent/manus.py: 覆写 run() 注入经验(跑完恢复 system_prompt 防累积);
覆写 _handle_special_tool 捕获 terminate 状态
- main.py: 跑完交互确认入口(success 才弹 [y/N],非 TTY 跳过保存)
- app/config.py: ExperienceSettings;config.example.toml: [experience] 段
- requirements.txt: faiss-cpu / rank-bm25 / jieba
- .gitignore: 锚定 /experience/ 仅忽略根级运行时数据,不波及 app/experience/
测试:tests/test_experience_{store,retriever,recorder}.py(16 例,离线可跑)
四层验证:环境自检 / 离线冒烟 / 真实端到端(注入+落库)/ 关闭开关回归。 含可直接运行的离线冒烟脚本与七条判定速查表。 2026-06-30 真实运行已印证:1024 维向量落库、相似任务命中注入、闸门拦截无关任务。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概述
为 manus-gui 新增 RAG 经验库:把跑通的任务流程沉淀为高层经验,新任务用「用户提示词」检索最相似的成功经验,作为 few-shot 注入 prompt,让 agent 少绕圈、用更少步数完成。整套特性是可配置、可干净下线的旁路增益,关掉开关后对主流程零侵入。
本 PR 含两个提交:
feat: RAG 经验库实现(app/experience/五件套 + 集成点 + 单测)docs: 验证指南docs/rag-experience-verification.md架构
app/experience/embedding.pytext-embedding-v4向量化,L2 归一化app/experience/store.pyRecipe+RecipeStore:内存权威列表 + jsonl 原子重写 + 重建 Faiss/BM25,asyncio 锁串行化写app/experience/retriever.pyapp/experience/recorder.pyapp/experience/__init__.pyis_enabled/retrieve_recipes/distill_recipe/format_injection/store单例集成接缝:注入在
Manus.run()(agent 层,非交互,跑完恢复 system_prompt 防累积);确认 + 落库在main.py(CLI 层,交互[y/N])—— agent 保持纯净不耦合 console。降级:faiss / rank-bm25 / jieba 任一缺失都不中断,对应通路 best-effort 降级(缺 faiss → numpy 点积;缺 jieba/bm25 → 仅 dense)。
关键设计决策
min_score=0.35,防止给无关任务硬塞经验。测试
单元测试(16 例,离线,monkeypatch embed/LLM):
覆盖 CRUD / 去重 / RRF 融合 / 相关性闸门 / 降级 / 提炼 JSON 解析。
真实端到端验证(2026-06-30 已实跑印证,详见
docs/rag-experience-verification.md):[y/N]确认 →✅ 经验已保存经验库已加载:1 条+🧠 已注入 1 条历史成功经验enabled=false)后既不注入也不弹保存询问、无报错 —— 可干净下线关闭方式
运行时数据
experience/recipes.jsonl已被.gitignore忽略,不入库。