04_note_tool_integration.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. """
  2. NoteTool 与 ContextBuilder 集成示例
  3. 展示如何将 NoteTool 与 ContextBuilder 集成,实现:
  4. 1. 长期项目追踪
  5. 2. 笔记检索与上下文注入
  6. 3. 基于历史笔记的连贯建议
  7. """
  8. from hello_agents import SimpleAgent, HelloAgentsLLM
  9. from hello_agents.context import ContextBuilder, ContextConfig, ContextPacket
  10. from hello_agents.tools import MemoryTool, RAGTool, NoteTool
  11. from hello_agents.core.message import Message
  12. from datetime import datetime
  13. from typing import List, Dict
  14. class ProjectAssistant(SimpleAgent):
  15. """长期项目助手,集成 NoteTool 和 ContextBuilder"""
  16. def __init__(self, name: str, project_name: str, **kwargs):
  17. # 配置 LLM
  18. from hello_agents.core.llm import HelloAgentsLLM
  19. llm = HelloAgentsLLM()
  20. super().__init__(name=name, llm=llm, **kwargs)
  21. self.project_name = project_name
  22. # 初始化工具
  23. # self.memory_tool = MemoryTool(user_id=project_name)
  24. # self.rag_tool = RAGTool(knowledge_base_path=f"./{project_name}_kb")
  25. self.note_tool = NoteTool(workspace=f"./{project_name}_notes")
  26. # 初始化上下文构建器
  27. self.context_builder = ContextBuilder(
  28. # memory_tool=self.memory_tool,
  29. # rag_tool=self.rag_tool,
  30. config=ContextConfig(max_tokens=4000)
  31. )
  32. self.conversation_history = []
  33. def run(self, user_input: str, note_as_action: bool = False) -> str:
  34. """运行助手,自动集成笔记"""
  35. # 1. 从 NoteTool 检索相关笔记
  36. relevant_notes = self._retrieve_relevant_notes(user_input)
  37. # 2. 将笔记转换为 ContextPacket
  38. note_packets = self._notes_to_packets(relevant_notes)
  39. # 3. 构建优化的上下文
  40. optimized_context = self.context_builder.build(
  41. user_query=user_input,
  42. conversation_history=self.conversation_history,
  43. system_instructions=self._build_system_instructions(),
  44. additional_packets=note_packets
  45. )
  46. # 4. 调用 LLM (以 messages 数组形式传入)
  47. messages = [
  48. {"role": "system", "content": optimized_context},
  49. {"role": "user", "content": user_input}
  50. ]
  51. response = self.llm.invoke(messages)
  52. # 5. 如果需要,将交互记录为笔记
  53. if note_as_action:
  54. self._save_as_note(user_input, response)
  55. # 6. 更新对话历史
  56. self._update_history(user_input, response)
  57. return response
  58. def _retrieve_relevant_notes(self, query: str, limit: int = 3) -> List[Dict]:
  59. """检索相关笔记"""
  60. try:
  61. # 优先检索 blocker 和 action 类型的笔记
  62. blockers_raw = self.note_tool.run({
  63. "action": "list",
  64. "note_type": "blocker",
  65. "limit": 2
  66. })
  67. # 通用搜索
  68. search_results_raw = self.note_tool.run({
  69. "action": "search",
  70. "query": query,
  71. "limit": limit
  72. })
  73. blockers = self._ensure_list_of_dicts(blockers_raw)
  74. search_results = self._ensure_list_of_dicts(search_results_raw)
  75. # 合并并去重
  76. all_notes = {}
  77. for note in blockers + search_results:
  78. if not isinstance(note, dict):
  79. continue
  80. note_id = (
  81. note.get("note_id")
  82. or note.get("id")
  83. or note.get("uuid")
  84. or note.get("title")
  85. or str(hash(str(note)))
  86. )
  87. all_notes[note_id] = note
  88. return list(all_notes.values())[:limit]
  89. except Exception as e:
  90. print(f"[WARNING] 笔记检索失败: {e}")
  91. return []
  92. def _ensure_list_of_dicts(self, data) -> List[Dict]:
  93. """将 NoteTool 返回规范化为字典列表"""
  94. import json
  95. if data is None:
  96. return []
  97. if isinstance(data, str):
  98. try:
  99. data = json.loads(data)
  100. except Exception:
  101. return []
  102. if isinstance(data, dict):
  103. # 兼容 {"items": [...]} 或单条记录
  104. if "items" in data and isinstance(data["items"], list):
  105. return [item for item in data["items"] if isinstance(item, dict)]
  106. return [data]
  107. if isinstance(data, list):
  108. return [item for item in data if isinstance(item, dict)]
  109. return []
  110. def _notes_to_packets(self, notes: List[Dict]) -> List[ContextPacket]:
  111. """将笔记转换为上下文包"""
  112. packets = []
  113. for note in notes:
  114. title = note.get("title", "")
  115. body = note.get("content", "")
  116. content = f"[笔记:{title}]\n{body}"
  117. # 安全解析时间戳
  118. ts = None
  119. for key in ("updated_at", "updatedAt", "time", "timestamp"):
  120. if key in note:
  121. ts = note.get(key)
  122. break
  123. parsed_ts = None
  124. if isinstance(ts, (int, float)):
  125. try:
  126. parsed_ts = datetime.fromtimestamp(ts)
  127. except Exception:
  128. parsed_ts = None
  129. elif isinstance(ts, str):
  130. try:
  131. parsed_ts = datetime.fromisoformat(ts)
  132. except Exception:
  133. parsed_ts = None
  134. if parsed_ts is None:
  135. parsed_ts = datetime.now()
  136. note_type = note.get("type") or note.get("note_type") or "note"
  137. note_id = (
  138. note.get("note_id")
  139. or note.get("id")
  140. or note.get("uuid")
  141. or title
  142. or str(hash(str(note)))
  143. )
  144. packets.append(ContextPacket(
  145. content=content,
  146. timestamp=parsed_ts,
  147. token_count=len(content) // 4, # 简单估算
  148. relevance_score=0.75, # 笔记具有较高相关性
  149. metadata={
  150. "type": "note",
  151. "note_type": note_type,
  152. "note_id": note_id
  153. }
  154. ))
  155. return packets
  156. def _save_as_note(self, user_input: str, response: str):
  157. """将交互保存为笔记"""
  158. try:
  159. # 判断应该保存为什么类型的笔记
  160. if "问题" in user_input or "阻塞" in user_input:
  161. note_type = "blocker"
  162. elif "计划" in user_input or "下一步" in user_input:
  163. note_type = "action"
  164. else:
  165. note_type = "conclusion"
  166. self.note_tool.run({
  167. "action": "create",
  168. "title": f"{user_input[:30]}...",
  169. "content": f"## 问题\n{user_input}\n\n## 分析\n{response}",
  170. "note_type": note_type,
  171. "tags": [self.project_name, "auto_generated"]
  172. })
  173. except Exception as e:
  174. print(f"[WARNING] 保存笔记失败: {e}")
  175. def _build_system_instructions(self) -> str:
  176. """构建系统指令"""
  177. return f"""你是 {self.project_name} 项目的长期助手。
  178. 你的职责:
  179. 1. 基于历史笔记提供连贯的建议
  180. 2. 追踪项目进展和待解决问题
  181. 3. 在回答时引用相关的历史笔记
  182. 4. 提供具体、可操作的下一步建议
  183. 注意:
  184. - 优先关注标记为 blocker 的问题
  185. - 在建议中说明依据来源(笔记、记忆或知识库)
  186. - 保持对项目整体进度的认识"""
  187. def _update_history(self, user_input: str, response: str):
  188. """更新对话历史"""
  189. self.conversation_history.append(
  190. Message(content=user_input, role="user", timestamp=datetime.now())
  191. )
  192. self.conversation_history.append(
  193. Message(content=response, role="assistant", timestamp=datetime.now())
  194. )
  195. # 限制历史长度
  196. if len(self.conversation_history) > 10:
  197. self.conversation_history = self.conversation_history[-10:]
  198. def main():
  199. print("=" * 80)
  200. print("NoteTool 与 ContextBuilder 集成示例")
  201. print("=" * 80 + "\n")
  202. # 使用示例
  203. assistant = ProjectAssistant(
  204. name="项目助手",
  205. project_name="data_pipeline_refactoring"
  206. )
  207. # 第一次交互:记录项目状态
  208. print("第一次交互:记录项目状态")
  209. response = assistant.run(
  210. "我们已经完成了数据模型层的重构,测试覆盖率达到85%。下一步计划重构业务逻辑层。",
  211. note_as_action=True
  212. )
  213. print(f"助手回答: {response}\n")
  214. # 第二次交互:提出问题
  215. print("第二次交互:提出问题")
  216. response = assistant.run(
  217. "在重构业务逻辑层时,我遇到了依赖版本冲突的问题,该如何解决?"
  218. )
  219. print(f"助手回答: {response}\n")
  220. # 查看笔记摘要
  221. print("查看笔记摘要:")
  222. summary = assistant.note_tool.run({"action": "summary"})
  223. import json
  224. print(json.dumps(summary, indent=2, ensure_ascii=False))
  225. print("\n" + "=" * 80)
  226. print("演示完成!")
  227. print("=" * 80)
  228. if __name__ == "__main__":
  229. main()